Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/python
- import sqlite3
- import sys
- import os
- import inspect
- def eprint(*args, **kwargs):
- print(*args, file=sys.stderr, **kwargs)
- def connectDB():
- try:
- path = os.path.dirname(os.path.realpath(__file__)) + "/myanime.db"
- con = sqlite3.connect(path)
- return con
- except sqlite3.Error as error:
- eprint("@%s: %s" % (inspect.stack()[0][3], error))
- def createDB():
- con = connectDB()
- c = con.cursor()
- c.execute("""CREATE TABLE IF NOT EXISTS Animes(
- "id" INTEGER PRIMARY KEY ,
- "url" TEXT NOT NULL UNIQUE,
- "title" TEXT NOT NULL UNIQUE,
- "status" TEXT DEFAULT "ongoing" ,
- "rank" INTEGER,
- "removed" INTEGER DEFAULT 0
- );""")
- c.execute("""CREATE TABLE IF NOT EXISTS LIST(
- "id" INTEGER PRIMARY KEY ,
- "title" TEXT UNIQUE,
- "link" TEXT,
- "etitle" TEXT,
- "date" TEXT,
- "episode" INTEGER,
- "adate" TEXT,
- "entitle" TEXT,
- "ongoing" INTEGER DEFAULT 1
- );""")
- con.commit()
- c.close()
- con.close()
- def addAnime(url, title):
- try:
- con = connectDB()
- c = con.cursor()
- c.execute("""
- INSERT OR IGNORE INTO Animes (url,title)
- VALUES ("{u}","{t}") """ .format(u=url, t=title))
- con.commit()
- print(c.lastrowid)
- c.close()
- except sqlite3.Error as error:
- eprint("@%s: %s" % (inspect.stack()[0][3], error))
- finally:
- if (con):
- con.close()
- def setStatus(title, status):
- value=0
- if status == "ongoing":
- value=1
- try:
- con = connectDB()
- c = con.cursor()
- c.execute("""
- UPDATE Animes set status="{s}"
- WHERE title="{t}" """ .format(s=status, t=title))
- c.execute("""
- UPDATE LIST set ongoing="{v}"
- WHERE title="{t}" """ .format(v=value, t=title))
- con.commit()
- c.close()
- except sqlite3.Error as error:
- eprint("@%s: %s" % (inspect.stack()[0][3], error))
- finally:
- if (con):
- con.close()
- def setRank(title, rank):
- try:
- con = connectDB()
- c = con.cursor()
- c.execute("""
- UPDATE Animes set rank="{s}"
- WHERE title="{t}" """ .format(s=rank, t=title))
- con.commit()
- c.close()
- except sqlite3.Error as error:
- eprint("@%s: %s" % (inspect.stack()[0][3], error))
- finally:
- if (con):
- con.close()
- def setAD(title, adate):
- try:
- con = connectDB()
- c = con.cursor()
- c.execute(""" UPDATE LIST SET adate = "{ad}"
- WHERE title = "{t}"
- """ .format(t=title, ad=adate))
- con.commit()
- c.close()
- except sqlite3.Error as error:
- eprint("@%s: %s" % (inspect.stack()[0][3], error))
- finally:
- if (con):
- con.close()
- def setWL(title, elink, etitle, edate, episode):
- try:
- con = connectDB()
- c = con.cursor()
- c.execute(""" INSERT INTO LIST
- (title,link,etitle,date,episode)
- VALUES("{t}","{el}","{et}","{d}","{ep}")
- ON CONFLICT (title) DO UPDATE SET
- link = "{el}", etitle = "{et}", date = "{d}",
- episode = "{ep}"
- """ .format(t=title, el=elink, et=etitle, d=edate, ep=episode))
- con.commit()
- c.close()
- except sqlite3.Error as error:
- eprint("@%s: %s" % (inspect.stack()[0][3], error))
- finally:
- if (con):
- con.close()
- def getALL():
- try:
- con = connectDB()
- c = con.cursor()
- c.execute(""" SELECT status,rank,title from Animes
- WHERE removed = 0 ORDER BY id ASC""")
- rows = c.fetchall()
- for row in rows:
- print(' '.join([str(i) for i in row]))
- c.close()
- except sqlite3.Error as error:
- eprint("@%s: %s" % (inspect.stack()[0][3], error))
- finally:
- if (con):
- con.close()
- def set1():
- try:
- con = connectDB()
- c = con.cursor()
- c.execute(""" UPDATE Animes SET status = "ongoing" """)
- con.commit()
- c.close()
- except sqlite3.Error as error:
- eprint("@%s: %s" % (inspect.stack()[0][3], error))
- finally:
- if (con):
- con.close()
- def updateWL():
- try:
- con = connectDB()
- c = con.cursor()
- c.execute("""
- INSERT OR IGNORE INTO LIST (title)
- SELECT Animes.title from Animes
- WHERE status = "ongoing" AND removed = 0
- """)
- c.execute("""
- UPDATE LIST SET ongoing = "0" WHERE title IN (
- SELECT Animes.title from Animes
- WHERE status = "finished" AND removed = 0
- )
- """)
- con.commit()
- c.close()
- except sqlite3.Error as error:
- eprint("@%s: %s" % (inspect.stack()[0][3], error))
- finally:
- if (con):
- con.close()
- def getWL():
- updateWL()
- try:
- con = connectDB()
- c = con.cursor()
- c.execute(""" SELECT * from LIST
- WHERE ongoing = "1"
- ORDER BY datetime(date) DESC""")
- rows = c.fetchall()
- for row in rows:
- print('|'.join([str(i) for i in row]))
- c.close()
- except sqlite3.Error as error:
- eprint("@%s: %s" % (inspect.stack()[0][3], error))
- finally:
- if (con):
- con.close()
- def setEnTitle(title,entitle):
- try:
- con = connectDB()
- c = con.cursor()
- c.execute(""" UPDATE LIST
- SET entitle = "{et}"
- WHERE title="{t}"
- """ .format(t=title, et=entitle))
- con.commit()
- c.close()
- except sqlite3.Error as error:
- eprint("@%s: %s" % (inspect.stack()[0][3], error))
- finally:
- if (con):
- con.close()
- def setRemove(title):
- try:
- con = connectDB()
- c = con.cursor()
- c.execute(""" UPDATE Animes
- SET removed = 1
- WHERE title="{t}"
- """ .format(t=title))
- con.commit()
- c.close()
- except sqlite3.Error as error:
- eprint("@%s: %s" % (inspect.stack()[0][3], error))
- finally:
- if (con):
- con.close()
- def getURL(title):
- try:
- con = connectDB()
- c = con.cursor()
- c.execute(""" SELECT url from Animes WHERE title="{t}"
- """ .format(t=title))
- row = c.fetchone()
- if row:
- print(row[0])
- c.close()
- except sqlite3.Error as error:
- eprint("@%s: %s" % (inspect.stack()[0][3], error))
- finally:
- if (con):
- con.close()
- def myfuncSwitch(arg):
- cmd = arg[1]
- switcher = {
- "add": addAnime,
- "sets": setStatus,
- "setr": setRank,
- "wadd": setWL,
- "all": getALL,
- "url": getURL,
- "wl": getWL,
- "rm": setRemove,
- "s1": set1,
- "sad": setAD,
- "seten": setEnTitle
- }
- func = switcher.get(cmd)
- func(*arg[2:])
- def main():
- createDB()
- myfuncSwitch(sys.argv)
- if __name__ == "__main__":
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement