Advertisement
mosaid

myanime.py

Feb 29th, 2020
375
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 7.48 KB | None | 0 0
  1. #!/usr/bin/python
  2.  
  3. import sqlite3
  4. import sys
  5. import os
  6. import inspect
  7.  
  8.  
  9. def eprint(*args, **kwargs):
  10.     print(*args, file=sys.stderr, **kwargs)
  11.  
  12.  
  13. def connectDB():
  14.     try:
  15.         path = os.path.dirname(os.path.realpath(__file__)) + "/myanime.db"
  16.         con = sqlite3.connect(path)
  17.         return con
  18.     except sqlite3.Error as error:
  19.         eprint("@%s: %s" % (inspect.stack()[0][3], error))
  20.  
  21.  
  22. def createDB():
  23.     con = connectDB()
  24.     c = con.cursor()
  25.     c.execute("""CREATE TABLE IF NOT EXISTS Animes(
  26.        "id" INTEGER PRIMARY KEY ,
  27.        "url" TEXT  NOT NULL UNIQUE,
  28.        "title" TEXT NOT NULL UNIQUE,
  29.        "status" TEXT DEFAULT "ongoing" ,
  30.        "rank" INTEGER,
  31.        "removed" INTEGER DEFAULT 0
  32.    );""")
  33.     c.execute("""CREATE TABLE IF NOT EXISTS LIST(
  34.        "id" INTEGER PRIMARY KEY ,
  35.        "title" TEXT UNIQUE,
  36.        "link" TEXT,
  37.        "etitle" TEXT,
  38.        "date" TEXT,
  39.        "episode" INTEGER,
  40.        "adate" TEXT,
  41.        "entitle" TEXT,
  42.        "ongoing" INTEGER DEFAULT 1
  43.    );""")
  44.     con.commit()
  45.     c.close()
  46.     con.close()
  47.  
  48.  
  49. def addAnime(url, title):
  50.     try:
  51.         con = connectDB()
  52.         c = con.cursor()
  53.         c.execute("""
  54.        INSERT OR IGNORE INTO Animes (url,title)
  55.        VALUES ("{u}","{t}") """ .format(u=url, t=title))
  56.         con.commit()
  57.         print(c.lastrowid)
  58.         c.close()
  59.     except sqlite3.Error as error:
  60.         eprint("@%s: %s" % (inspect.stack()[0][3], error))
  61.     finally:
  62.         if (con):
  63.             con.close()
  64.  
  65.  
  66. def setStatus(title, status):
  67.     value=0
  68.     if status == "ongoing":
  69.         value=1
  70.     try:
  71.         con = connectDB()
  72.         c = con.cursor()
  73.         c.execute("""
  74.                  UPDATE Animes set status="{s}"
  75.                  WHERE title="{t}" """ .format(s=status, t=title))
  76.         c.execute("""
  77.                  UPDATE LIST set ongoing="{v}"
  78.                  WHERE title="{t}" """ .format(v=value, t=title))
  79.         con.commit()
  80.         c.close()
  81.     except sqlite3.Error as error:
  82.         eprint("@%s: %s" % (inspect.stack()[0][3], error))
  83.     finally:
  84.         if (con):
  85.             con.close()
  86.  
  87.  
  88. def setRank(title, rank):
  89.     try:
  90.         con = connectDB()
  91.         c = con.cursor()
  92.         c.execute("""
  93.                  UPDATE Animes set rank="{s}"
  94.                  WHERE title="{t}" """ .format(s=rank, t=title))
  95.         con.commit()
  96.         c.close()
  97.     except sqlite3.Error as error:
  98.         eprint("@%s: %s" % (inspect.stack()[0][3], error))
  99.     finally:
  100.         if (con):
  101.             con.close()
  102.  
  103.  
  104. def setAD(title, adate):
  105.     try:
  106.         con = connectDB()
  107.         c = con.cursor()
  108.         c.execute(""" UPDATE LIST SET adate = "{ad}"
  109.                      WHERE title = "{t}"
  110.                  """ .format(t=title, ad=adate))
  111.         con.commit()
  112.         c.close()
  113.     except sqlite3.Error as error:
  114.         eprint("@%s: %s" % (inspect.stack()[0][3], error))
  115.     finally:
  116.         if (con):
  117.             con.close()
  118.  
  119.  
  120. def setWL(title, elink, etitle, edate, episode):
  121.     try:
  122.         con = connectDB()
  123.         c = con.cursor()
  124.         c.execute(""" INSERT INTO LIST
  125.                    (title,link,etitle,date,episode)
  126.                    VALUES("{t}","{el}","{et}","{d}","{ep}")
  127.                    ON CONFLICT (title) DO UPDATE SET
  128.                    link = "{el}", etitle = "{et}", date = "{d}",
  129.                    episode = "{ep}"
  130.        """ .format(t=title, el=elink, et=etitle, d=edate, ep=episode))
  131.         con.commit()
  132.         c.close()
  133.     except sqlite3.Error as error:
  134.         eprint("@%s: %s" % (inspect.stack()[0][3], error))
  135.     finally:
  136.         if (con):
  137.             con.close()
  138.  
  139.  
  140. def getALL():
  141.     try:
  142.         con = connectDB()
  143.         c = con.cursor()
  144.         c.execute(""" SELECT status,rank,title from Animes
  145.                  WHERE removed = 0 ORDER BY id ASC""")
  146.         rows = c.fetchall()
  147.         for row in rows:
  148.             print(' '.join([str(i) for i in row]))
  149.         c.close()
  150.     except sqlite3.Error as error:
  151.         eprint("@%s: %s" % (inspect.stack()[0][3], error))
  152.     finally:
  153.         if (con):
  154.             con.close()
  155.  
  156.  
  157. def set1():
  158.     try:
  159.         con = connectDB()
  160.         c = con.cursor()
  161.         c.execute(""" UPDATE Animes SET status = "ongoing" """)
  162.         con.commit()
  163.         c.close()
  164.     except sqlite3.Error as error:
  165.         eprint("@%s: %s" % (inspect.stack()[0][3], error))
  166.     finally:
  167.         if (con):
  168.             con.close()
  169.  
  170.  
  171. def updateWL():
  172.     try:
  173.         con = connectDB()
  174.         c = con.cursor()
  175.         c.execute("""
  176.            INSERT OR IGNORE INTO LIST (title)
  177.            SELECT Animes.title from Animes
  178.            WHERE status = "ongoing" AND removed = 0
  179.            """)
  180.         c.execute("""
  181.                   UPDATE LIST SET ongoing = "0" WHERE title IN (
  182.                    SELECT Animes.title from Animes
  183.                    WHERE status = "finished" AND removed = 0
  184.                  )
  185.                  """)
  186.         con.commit()
  187.         c.close()
  188.     except sqlite3.Error as error:
  189.         eprint("@%s: %s" % (inspect.stack()[0][3], error))
  190.     finally:
  191.         if (con):
  192.             con.close()
  193.  
  194.  
  195. def getWL():
  196.     updateWL()
  197.     try:
  198.         con = connectDB()
  199.         c = con.cursor()
  200.         c.execute(""" SELECT * from LIST
  201.                  WHERE ongoing = "1"
  202.                  ORDER BY datetime(date) DESC""")
  203.         rows = c.fetchall()
  204.         for row in rows:
  205.             print('|'.join([str(i) for i in row]))
  206.         c.close()
  207.     except sqlite3.Error as error:
  208.         eprint("@%s: %s" % (inspect.stack()[0][3], error))
  209.     finally:
  210.         if (con):
  211.             con.close()
  212.  
  213.  
  214. def setEnTitle(title,entitle):
  215.     try:
  216.         con = connectDB()
  217.         c = con.cursor()
  218.         c.execute(""" UPDATE LIST
  219.                  SET entitle = "{et}"
  220.                  WHERE title="{t}"
  221.                  """ .format(t=title, et=entitle))
  222.         con.commit()
  223.         c.close()
  224.     except sqlite3.Error as error:
  225.         eprint("@%s: %s" % (inspect.stack()[0][3], error))
  226.     finally:
  227.         if (con):
  228.             con.close()
  229.  
  230.  
  231. def setRemove(title):
  232.     try:
  233.         con = connectDB()
  234.         c = con.cursor()
  235.         c.execute(""" UPDATE Animes
  236.                      SET removed = 1
  237.                      WHERE title="{t}"
  238.                  """ .format(t=title))
  239.         con.commit()
  240.         c.close()
  241.     except sqlite3.Error as error:
  242.         eprint("@%s: %s" % (inspect.stack()[0][3], error))
  243.     finally:
  244.         if (con):
  245.             con.close()
  246.  
  247.  
  248. def getURL(title):
  249.     try:
  250.         con = connectDB()
  251.         c = con.cursor()
  252.         c.execute(""" SELECT url from Animes WHERE title="{t}"
  253.                  """ .format(t=title))
  254.         row = c.fetchone()
  255.         if row:
  256.             print(row[0])
  257.         c.close()
  258.     except sqlite3.Error as error:
  259.         eprint("@%s: %s" % (inspect.stack()[0][3], error))
  260.     finally:
  261.         if (con):
  262.             con.close()
  263.  
  264.  
  265. def myfuncSwitch(arg):
  266.     cmd = arg[1]
  267.     switcher = {
  268.         "add": addAnime,
  269.         "sets": setStatus,
  270.         "setr": setRank,
  271.         "wadd": setWL,
  272.         "all": getALL,
  273.         "url": getURL,
  274.         "wl": getWL,
  275.         "rm": setRemove,
  276.         "s1": set1,
  277.         "sad": setAD,
  278.         "seten": setEnTitle
  279.     }
  280.     func = switcher.get(cmd)
  281.     func(*arg[2:])
  282.  
  283.  
  284. def main():
  285.     createDB()
  286.     myfuncSwitch(sys.argv)
  287.  
  288.  
  289. if __name__ == "__main__":
  290.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement