Advertisement
Guest User

Untitled

a guest
Jul 22nd, 2018
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 16.94 KB | None | 0 0
  1. #-*- coding: utf-8 -*-
  2. # https://github.com/Kodi-vStream/venom-xbmc-addons
  3. #Venom.
  4. from resources.lib.handler.inputParameterHandler import cInputParameterHandler
  5. from resources.lib.handler.outputParameterHandler import cOutputParameterHandler
  6. from resources.lib.comaddon import dialog, VSlog, xbmc
  7.  
  8. import urllib
  9.  
  10. import xbmcvfs
  11.  
  12.  
  13. SITE_IDENTIFIER = 'cDb'
  14. SITE_NAME = 'DB'
  15.  
  16.  
  17. try:
  18. from sqlite3 import dbapi2 as sqlite
  19. VSlog('SQLITE 3 as DB engine')
  20. except:
  21. from pysqlite2 import dbapi2 as sqlite
  22. VSlog('SQLITE 2 as DB engine')
  23.  
  24.  
  25. class cDb:
  26.  
  27. #os.path.join(self.__oCache,'vstream.db').decode("utf-8")
  28. DB = "special://userdata/addon_data/plugin.video.vstream/vstream.db"
  29. #important seul xbmcvfs peux lire le special
  30. REALDB = xbmc.translatePath(DB).decode("utf-8")
  31. DIALOG = dialog()
  32.  
  33. def __init__(self):
  34. print 'reponse sqlite %s' % sqlite
  35.  
  36.  
  37. try:
  38. #if not os.path.exists(self.cache):
  39. if not xbmcvfs.exists(self.DB):
  40. self.db = sqlite.connect(self.REALDB)
  41. self.db.row_factory = sqlite.Row
  42. self.dbcur = self.db.cursor()
  43. self._create_tables()
  44. except:
  45. VSlog('erreur: Impossible d ecrire sur %s' % self.REALDB )
  46. pass
  47.  
  48. try:
  49. self.db = sqlite.connect(self.REALDB)
  50. self.db.row_factory = sqlite.Row
  51. self.dbcur = self.db.cursor()
  52. except:
  53. VSlog('erreur: Impossible de ce connecter sur %s' % self.REALDB )
  54. pass
  55.  
  56.  
  57.  
  58.  
  59. def __del__(self):
  60. ''' Cleanup db when object destroyed '''
  61. try:
  62. self.dbcur.close()
  63. self.dbcon.close()
  64. except: pass
  65.  
  66. def _create_tables(self):
  67.  
  68. sql_create2 = "DROP TABLE history"
  69.  
  70. ''' Create table '''
  71. sql_create = "CREATE TABLE IF NOT EXISTS history ("" addon_id integer PRIMARY KEY AUTOINCREMENT, ""title TEXT, ""disp TEXT, ""icone TEXT, ""isfolder TEXT, ""level TEXT, ""lastwatched TIMESTAMP "", ""UNIQUE(title)"");"
  72. self.dbcur.execute(sql_create)
  73.  
  74. sql_create = "CREATE TABLE IF NOT EXISTS resume ("" addon_id integer PRIMARY KEY AUTOINCREMENT, ""title TEXT, ""hoster TEXT, ""point TEXT, ""UNIQUE(title, hoster)"");"
  75. self.dbcur.execute(sql_create)
  76.  
  77. sql_create = "CREATE TABLE IF NOT EXISTS watched ("" addon_id integer PRIMARY KEY AUTOINCREMENT, ""title TEXT, ""site TEXT, ""UNIQUE(title, site)"");"
  78. self.dbcur.execute(sql_create)
  79.  
  80. sql_create = "CREATE TABLE IF NOT EXISTS favorite ("" addon_id integer PRIMARY KEY AUTOINCREMENT, ""title TEXT, ""siteurl TEXT, ""site TEXT, ""fav TEXT, ""cat TEXT, ""icon TEXT, ""fanart TEXT, ""UNIQUE(title, site)"");"
  81. self.dbcur.execute(sql_create)
  82.  
  83. #sql_create = "DROP TABLE download"
  84. #self.dbcur.execute(sql_create)
  85.  
  86. sql_create = "CREATE TABLE IF NOT EXISTS download ("" addon_id integer PRIMARY KEY AUTOINCREMENT, ""title TEXT, ""url TEXT, ""path TEXT, ""cat TEXT, ""icon TEXT, ""size TEXT,""totalsize TEXT, ""status TEXT, ""UNIQUE(title, path)"");"
  87. self.dbcur.execute(sql_create)
  88.  
  89. VSlog('Table initialized')
  90.  
  91. #Ne pas utiliser cette fonction pour les chemins
  92. def str_conv(self, data):
  93. if isinstance(data, str):
  94. # Must be encoded in UTF-8
  95. data = data.decode('utf8')
  96.  
  97. import unicodedata
  98. data = unicodedata.normalize('NFKD', data).encode('ascii','ignore')
  99. data = data.decode('string-escape') #ATTENTION : provoque des bugs pour les chemins a cause du caractere '/'
  100.  
  101. return data
  102.  
  103. def insert_history(self, meta):
  104.  
  105. #title = urllib.unquote(meta['title']).decode('ascii', 'ignore')
  106. title = self.str_conv(urllib.unquote(meta['title']))
  107. disp = meta['disp']
  108. icon = 'icon.png'
  109.  
  110. try:
  111. ex = "INSERT INTO history (title, disp, icone) VALUES (?, ?, ?)"
  112. self.dbcur.execute(ex, (title,disp,icon))
  113. self.db.commit()
  114. VSlog('SQL INSERT history Successfully')
  115. except Exception, e:
  116. if 'UNIQUE constraint failed' in e.message:
  117. ex = "UPDATE history set title = '%s', disp = '%s', icone= '%s' WHERE title = '%s'" % (title, disp, icon, title)
  118. self.dbcur.execute(ex)
  119. self.db.commit()
  120. VSlog('SQL UPDATE history Successfully')
  121. VSlog('SQL ERROR INSERT')
  122. pass
  123. self.db.close()
  124.  
  125. def insert_resume(self, meta):
  126. title = self.str_conv(meta['title'])
  127. site = urllib.quote_plus(meta['site'])
  128. #hoster = meta['hoster']
  129. point = meta['point']
  130. ex = "DELETE FROM resume WHERE hoster = '%s'" % (site)
  131. self.dbcur.execute(ex)
  132. ex = "INSERT INTO resume (title, hoster, point) VALUES (?, ?, ?)"
  133. self.dbcur.execute(ex, (title,site,point))
  134.  
  135. try:
  136. self.db.commit()
  137. VSlog('SQL INSERT resume Successfully')
  138. except Exception, e:
  139. #print ('************* Error attempting to insert into %s cache table: %s ' % (table, e))
  140. VSlog('SQL ERROR INSERT')
  141. pass
  142. self.db.close()
  143.  
  144. def insert_watched(self, meta):
  145.  
  146. title = self.str_conv(meta['title'])
  147. site = urllib.quote_plus(meta['site'])
  148. ex = "INSERT INTO watched (title, site) VALUES (?, ?)"
  149. self.dbcur.execute(ex, (title,site))
  150. try:
  151. self.db.commit()
  152. VSlog('SQL INSERT watched Successfully')
  153. except Exception, e:
  154. #print ('************* Error attempting to insert into %s cache table: %s ' % (table, e))
  155. VSlog('SQL ERROR INSERT')
  156. pass
  157. self.db.close()
  158.  
  159. def get_history(self):
  160.  
  161. sql_select = "SELECT * FROM history"
  162.  
  163. try:
  164. self.dbcur.execute(sql_select)
  165. #matchedrow = self.dbcur.fetchone()
  166. matchedrow = self.dbcur.fetchall()
  167. return matchedrow
  168. except Exception, e:
  169. VSlog('SQL ERROR EXECUTE')
  170. return None
  171. self.dbcur.close()
  172.  
  173. def get_resume(self, meta):
  174. title = self.str_conv(meta['title'])
  175. site = urllib.quote_plus(meta['site'])
  176.  
  177. sql_select = "SELECT * FROM resume WHERE hoster = '%s'" % (site)
  178.  
  179. try:
  180. self.dbcur.execute(sql_select)
  181. #matchedrow = self.dbcur.fetchone()
  182. matchedrow = self.dbcur.fetchall()
  183. return matchedrow
  184. except Exception, e:
  185. VSlog('SQL ERROR EXECUTE')
  186. return None
  187. self.dbcur.close()
  188.  
  189. def get_watched(self, meta):
  190. count = 0
  191. site = urllib.quote_plus(meta['site'])
  192. sql_select = "SELECT * FROM watched WHERE site = '%s'" % (site)
  193.  
  194. try:
  195. self.dbcur.execute(sql_select)
  196. #matchedrow = self.dbcur.fetchone()
  197. matchedrow = self.dbcur.fetchall()
  198.  
  199. if matchedrow:
  200. count = 1
  201. else:
  202. count = 0
  203. return count
  204. except Exception, e:
  205. VSlog('SQL ERROR EXECUTE')
  206. return None
  207. self.dbcur.close()
  208.  
  209. def del_history(self):
  210.  
  211. oInputParameterHandler = cInputParameterHandler()
  212. if (oInputParameterHandler.exist('searchtext')):
  213. sql_delete = "DELETE FROM history WHERE title = '%s'" % (oInputParameterHandler.getValue('searchtext'))
  214. else:
  215. sql_delete = "DELETE FROM history;"
  216.  
  217. try:
  218. self.dbcur.execute(sql_delete)
  219. self.db.commit()
  220. self.DIALOG.VSinfo('Historique supprime')
  221. xbmc.executebuiltin("Container.Refresh")
  222. return False, False
  223. except Exception, e:
  224. VSlog('SQL ERROR DELETE')
  225. return False, False
  226. self.dbcur.close()
  227.  
  228.  
  229. def del_watched(self, meta):
  230. site = urllib.quote_plus(meta['site'])
  231. sql_select = "DELETE FROM watched WHERE site = '%s'" % (site)
  232.  
  233. try:
  234. self.dbcur.execute(sql_select)
  235. self.db.commit()
  236. return False, False
  237. except Exception, e:
  238. VSlog('SQL ERROR EXECUTE')
  239. return False, False
  240. self.dbcur.close()
  241.  
  242. def del_resume(self, meta):
  243. site = urllib.quote_plus(meta['site'])
  244.  
  245. sql_select = "DELETE FROM resume WHERE hoster = '%s'" % (site)
  246.  
  247. try:
  248. self.dbcur.execute(sql_select)
  249. self.db.commit()
  250. return False, False
  251. except Exception, e:
  252. VSlog('SQL ERROR EXECUTE')
  253. return False, False
  254. self.dbcur.close()
  255.  
  256.  
  257.  
  258. #***********************************
  259. # Favoris fonctions
  260. #***********************************
  261.  
  262. def insert_favorite(self, meta):
  263.  
  264. title = self.str_conv(meta['title'])
  265. siteurl = urllib.quote_plus(meta['siteurl'])
  266.  
  267. try:
  268. sIcon = meta['icon'].decode('UTF-8')
  269. except:
  270. sIcon = meta['icon']
  271.  
  272.  
  273. try:
  274. ex = "INSERT INTO favorite (title, siteurl, site, fav, cat, icon, fanart) VALUES (?, ?, ?, ?, ?, ?, ?)"
  275. self.dbcur.execute(ex, (title,siteurl, meta['site'],meta['fav'],meta['cat'],sIcon,meta['fanart']))
  276.  
  277. self.db.commit()
  278.  
  279. self.DIALOG.VSinfo('Enregistré avec succés', meta['title'])
  280. VSlog('SQL INSERT favorite Successfully')
  281. except Exception, e:
  282. if 'UNIQUE constraint failed' in e.message:
  283. self.DIALOG.VSinfo('Marque-page deja present', meta['title'])
  284. VSlog('SQL ERROR INSERT')
  285. pass
  286. self.db.close()
  287.  
  288. def get_favorite(self):
  289.  
  290. sql_select = "SELECT * FROM favorite"
  291.  
  292. try:
  293. self.dbcur.execute(sql_select)
  294. #matchedrow = self.dbcur.fetchone()
  295. matchedrow = self.dbcur.fetchall()
  296. return matchedrow
  297. except Exception, e:
  298. VSlog('SQL ERROR EXECUTE')
  299. return None
  300. self.dbcur.close()
  301.  
  302. def del_favorite(self):
  303.  
  304. oInputParameterHandler = cInputParameterHandler()
  305.  
  306. if (oInputParameterHandler.exist('sCat')):
  307. sql_delete = "DELETE FROM favorite WHERE cat = '%s'" % (oInputParameterHandler.getValue('sCat'))
  308.  
  309. if(oInputParameterHandler.exist('sMovieTitle')):
  310.  
  311. siteUrl = oInputParameterHandler.getValue('siteUrl')
  312. sMovieTitle = oInputParameterHandler.getValue('sMovieTitle')
  313. siteUrl = urllib.quote_plus(siteUrl)
  314. title = self.str_conv(sMovieTitle)
  315. title = title.replace("'", r"''")
  316. sql_delete = "DELETE FROM favorite WHERE siteurl = '%s' AND title = '%s'" % (siteUrl,title)
  317.  
  318. if(oInputParameterHandler.exist('sAll')):
  319. sql_delete = "DELETE FROM favorite;"
  320.  
  321. try:
  322. self.dbcur.execute(sql_delete)
  323. self.db.commit()
  324. self.DIALOG.VSinfo('Favoris supprimé')
  325. xbmc.executebuiltin("Container.Refresh")
  326. return False, False
  327. except Exception, e:
  328. VSlog('SQL ERROR EXECUTE')
  329. return False, False
  330. self.dbcur.close()
  331.  
  332. #non utiliser ?
  333.  
  334. # def writeFavourites(self):
  335.  
  336. # oInputParameterHandler = cInputParameterHandler()
  337. # sTitle = oInputParameterHandler.getValue('sTitle')
  338. # sId = oInputParameterHandler.getValue('sId')
  339. # sUrl = oInputParameterHandler.getValue('siteUrl')
  340. # sFav = oInputParameterHandler.getValue('sFav')
  341.  
  342. # if (oInputParameterHandler.exist('sCat')):
  343. # sCat = oInputParameterHandler.getValue('sCat')
  344. # else:
  345. # sCat = '5'
  346.  
  347. # sUrl = urllib.quote_plus(sUrl)
  348. # fav_db = self.__sFile
  349. # watched = {}
  350. # if not os.path.exists(fav_db):
  351. # file(fav_db, "w").write("%r" % watched)
  352.  
  353. # if os.path.exists(fav_db):
  354. # watched = eval(open(fav_db).read() )
  355. # watched[sUrl] = watched.get(sUrl) or []
  356.  
  357. # #add to watched
  358. # if not watched[sUrl]:
  359. # #list = [sFav, sUrl];
  360. # watched[sUrl].append(sFav)
  361. # watched[sUrl].append(sId)
  362. # watched[sUrl].append(sTitle)
  363. # watched[sUrl].append(sCat)
  364. # else:
  365. # watched[sUrl][0] = sFav
  366. # watched[sUrl][1] = sId
  367. # watched[sUrl][2] = sTitle
  368. # watched[sUrl][3] = sCat
  369.  
  370. # file(fav_db, "w").write("%r" % watched)
  371. # cConfig().showInfo('Marque-Page', sTitle)
  372. #fav_db.close()
  373.  
  374. #***********************************
  375. # Download fonctions
  376. #***********************************
  377.  
  378. def insert_download(self, meta):
  379.  
  380. title = self.str_conv(meta['title'])
  381. url = urllib.quote_plus(meta['url'])
  382. sIcon = urllib.quote_plus(meta['icon'])
  383. sPath = meta['path']
  384.  
  385. ex = "INSERT INTO download (title, url, path, cat, icon, size, totalsize, status) VALUES (?, ?, ?, ?, ?, ?, ?, ?)"
  386. self.dbcur.execute(ex, (title,url, sPath,meta['cat'],sIcon, '', '', 0))
  387.  
  388. try:
  389. self.db.commit()
  390. VSlog('SQL INSERT download Successfully')
  391. self.DIALOG.VSinfo('Enregistré avec succés', meta['title'])
  392. except Exception, e:
  393. #print ('************* Error attempting to insert into %s cache table: %s ' % (table, e))
  394. VSlog('SQL ERROR INSERT')
  395. pass
  396. self.db.close()
  397.  
  398. def get_Download(self, meta = ''):
  399.  
  400. if meta == '':
  401. sql_select = "SELECT * FROM download"
  402. else:
  403. url = urllib.quote_plus(meta['url'])
  404. sql_select = "SELECT * FROM download WHERE url = '%s' AND status = '0'" % (url)
  405.  
  406. try:
  407. self.dbcur.execute(sql_select)
  408. matchedrow = self.dbcur.fetchall()
  409. return matchedrow
  410. except Exception, e:
  411. VSlog('SQL ERROR EXECUTE')
  412. return None
  413. self.dbcur.close()
  414.  
  415. def clean_download(self):
  416.  
  417. sql_select = "DELETE FROM download WHERE status = '2'"
  418.  
  419. try:
  420. self.dbcur.execute(sql_select)
  421. self.db.commit()
  422. return False, False
  423. except Exception, e:
  424. VSlog('SQL ERROR EXECUTE')
  425. return False, False
  426. self.dbcur.close()
  427.  
  428. def reset_download(self, meta):
  429.  
  430. url = urllib.quote_plus(meta['url'])
  431. sql_select = "UPDATE download SET status = '0' WHERE status = '2' AND url = '%s'" % (url)
  432.  
  433. try:
  434. self.dbcur.execute(sql_select)
  435. self.db.commit()
  436. return False, False
  437. except Exception, e:
  438. VSlog('SQL ERROR EXECUTE')
  439. return False, False
  440. self.dbcur.close()
  441.  
  442. def del_download(self, meta):
  443.  
  444. if len(meta['url']) > 1:
  445. url = urllib.quote_plus(meta['url'])
  446. sql_select = "DELETE FROM download WHERE url = '%s'" % (url)
  447. elif len(meta['path']) > 1:
  448. path = meta['path']
  449. sql_select = "DELETE FROM download WHERE path = '%s'" % (path)
  450. else:
  451. return
  452.  
  453. try:
  454. self.dbcur.execute(sql_select)
  455. self.db.commit()
  456. return False, False
  457. except Exception, e:
  458. VSlog('SQL ERROR EXECUTE')
  459. return False, False
  460. self.dbcur.close()
  461.  
  462. def Cancel_download(self):
  463. sql_select = "UPDATE download SET status = '0' WHERE status = '1'"
  464. try:
  465. self.dbcur.execute(sql_select)
  466. self.db.commit()
  467. return False, False
  468. except Exception, e:
  469. VSlog('SQL ERROR EXECUTE')
  470. return False, False
  471. self.dbcur.close()
  472.  
  473. def update_download(self, meta):
  474.  
  475. path = meta['path']
  476. size = meta['size']
  477. totalsize = meta['totalsize']
  478. status = meta['status']
  479.  
  480. sql_select = "UPDATE download set size = '%s', totalsize = '%s', status= '%s' WHERE path = '%s'" % (size, totalsize, status, path)
  481.  
  482. try:
  483. self.dbcur.execute(sql_select)
  484. self.db.commit()
  485. return False, False
  486. except Exception, e:
  487. VSlog('SQL ERROR EXECUTE')
  488. return False, False
  489. self.dbcur.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement