]> git.armaanb.net Git - phrases.git/commitdiff
functionalized things
authorArmaan Bhojwani <code@armaanb.net>
Sat, 19 Dec 2020 16:54:36 +0000 (11:54 -0500)
committerArmaan Bhojwani <code@armaanb.net>
Sat, 19 Dec 2020 16:54:36 +0000 (11:54 -0500)
extract.py
phrases.py

index 8f8877a8fd51968751022a80bc2272edb120de49..833594da483748f0608acaa5c143e88249d007e9 100755 (executable)
@@ -4,27 +4,23 @@
 
 import argparse
 import sqlite3
 
 import argparse
 import sqlite3
-import sys
 import requests
 from bs4 import BeautifulSoup
 
 import requests
 from bs4 import BeautifulSoup
 
-def main(args=sys.argv[1:]):
-    # Argument parsing
+def parse_args():
     parser = argparse.ArgumentParser(
         description="Generate SQLite db of Latin famous phrases from Wikipedia.")
     parser.add_argument("-o", "--output",
                        default="phrases.db",
                        help="set custom output file location")
     parser = argparse.ArgumentParser(
         description="Generate SQLite db of Latin famous phrases from Wikipedia.")
     parser.add_argument("-o", "--output",
                        default="phrases.db",
                        help="set custom output file location")
-    args = parser.parse_args()
+    return parser.parse_args()
 
 
-    url = ("""https://en.wikipedia.org/w/index.php?title=List_of_Latin_phrases_(
-          full)&oldid=986793908""")
+def get_html(url):
     print("downloading webpage")
     print("downloading webpage")
-    soup = BeautifulSoup(requests.get(url).content, "html.parser")
+    return BeautifulSoup(requests.get(url).content, "html.parser")
 
 
+def prep_database(c):
     print("prepping database")
     print("prepping database")
-    conn = sqlite3.connect(args.output)
-    c = conn.cursor()
     c.execute("DROP TABLE IF EXISTS phrases")
     c.execute("""CREATE TABLE phrases(
               id INTEGER,
     c.execute("DROP TABLE IF EXISTS phrases")
     c.execute("""CREATE TABLE phrases(
               id INTEGER,
@@ -33,29 +29,36 @@ def main(args=sys.argv[1:]):
               notes TEXT,
               length INTEGER)""")
 
               notes TEXT,
               length INTEGER)""")
 
-    i = 0 # For the phrase id
-
-    # iterate through the tables in the page
-    list_table = soup.find_all("table", attrs={"class":"wikitable"})
+def fill_database(list_table, c, conn):
+    i = 0 # phrase id
     print("iterating through tables")
     for table in list_table:
         for row in table.tbody.find_all("tr", recursive=False):
             cell = row.find_all("td", recursive=False)
             if len(cell) > 2:
                 print(i, end="\r")
     print("iterating through tables")
     for table in list_table:
         for row in table.tbody.find_all("tr", recursive=False):
             cell = row.find_all("td", recursive=False)
             if len(cell) > 2:
                 print(i, end="\r")
+
                 latin = (cell[0].get_text(" ", strip=True)).rstrip()
                 english = (cell[1].get_text(" ", strip=True)).rstrip()
                 notes = (cell[2].get_text(" ", strip=True)).rstrip()
     
                 latin = (cell[0].get_text(" ", strip=True)).rstrip()
                 english = (cell[1].get_text(" ", strip=True)).rstrip()
                 notes = (cell[2].get_text(" ", strip=True)).rstrip()
     
-                c.execute("""INSERT INTO phrases (id, latin, english, notes, length)
-                          VALUES(?, ?, ?, ?, ?)""", (i, latin, english, notes, len(latin)))
+                c.execute("""INSERT INTO phrases
+                         (id, latin, english, notes, length)
+                         VALUES(?, ?, ?, ?, ?)""",
+                         (i, latin, english, notes, len(latin)))
                 conn.commit()
                 conn.commit()
-
             i = i + 1
 
             i = i + 1
 
-    print("closing database")
-    c.close()
-    conn.close()
+def get_tables():
+    url = ("""https://en.wikipedia.org/w/index.php?title=List_of_Latin_phrases_(
+          full)&oldid=986793908""")
+    return get_html(url).find_all("table", attrs={"class":"wikitable"})
+
+def main(args):
+    conn = sqlite3.connect(args.output)
+    c = conn.cursor()
+    prep_database(c)
+    fill_database(get_tables(), c, conn)
 
 if __name__ == "__main__":
 
 if __name__ == "__main__":
-    main()
+    main(parse_args())
index 46db7829c2abe8dabf482c6387545846978f69c4..28ae138386be41e4e3258239f4f76d505f089f59 100755 (executable)
@@ -8,8 +8,7 @@ import sqlite3
 import sys
 import os.path
 
 import sys
 import os.path
 
-def main(args=sys.argv[1:]):
-    # Argument parsing
+def parse_args():
     parser = argparse.ArgumentParser(
         description="Latin famous phrases in the terminal.")
     parser.add_argument("-i", "--id",
     parser = argparse.ArgumentParser(
         description="Latin famous phrases in the terminal.")
     parser.add_argument("-i", "--id",
@@ -40,27 +39,9 @@ def main(args=sys.argv[1:]):
     parser.add_argument("-o", "--open",
                         type=int,
                         help="specify the id of a specific phrase to print.")
     parser.add_argument("-o", "--open",
                         type=int,
                         help="specify the id of a specific phrase to print.")
-    args = parser.parse_args()
+    return parser.parse_args()
 
 
-    # find phrase file
-    if args.file:
-        phrase_file = args.file
-    if os.path.isfile("phrases.db"):
-        phrase_file = "phrases.db"
-    elif os.path.isfile("/usr/local/share/phrases/phrases.db"):
-        phrase_file = "/usr/local/share/phrases/phrases.db"
-    else:
-        sys.exit("cannot find the phrase database!")
-    
-    conn = sqlite3.connect(phrase_file)
-    c = conn.cursor()
-    c.execute("SELECT * FROM phrases WHERE length <= (?) AND length >= (?)",
-              (args.max, args.min))
-    data = c.fetchall()
-    rown = randint(0, len(data))
-    row = list(data[rown]) 
-
-    # Output as specified in flags
+def output(args, row, numx):
     if not (args.id
             or args.latin
             or args.english
     if not (args.id
             or args.latin
             or args.english
@@ -78,8 +59,25 @@ def main(args=sys.argv[1:]):
         if args.notes:
             print(row[3])
         if args.num:
         if args.notes:
             print(row[3])
         if args.num:
-            print(len(data))
+            print(numx)
         sys.exit(0)
 
         sys.exit(0)
 
+def find_file(args):
+    if args.file:
+        return args.file
+    if os.path.isfile("phrases.db"):
+        return "phrases.db"
+    elif os.path.isfile("/usr/local/share/phrases/phrases.db"):
+        return "/usr/local/share/phrases/phrases.db"
+    else:
+        sys.exit("cannot find the phrase database!")
+
+def main(args):
+    c = sqlite3.connect(find_file(args)).cursor()
+    c.execute("SELECT * FROM phrases WHERE length <= (?) AND length >= (?)",
+              (args.max, args.min))
+    data = c.fetchall()
+    output(args, list(data[randint(0, len(data))]), len(data))
+
 if __name__ == "__main__":
 if __name__ == "__main__":
-    main()
+    main(parse_args())