PageRenderTime 43ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/addons/plugin.video.serialonline.netlite/default.py

http://seppius-xbmc-repo.googlecode.com/
Python | 406 lines | 375 code | 26 blank | 5 comment | 19 complexity | 73479beafcf5cdf677bb04997676102a MD5 | raw file
Possible License(s): GPL-3.0, AGPL-1.0
  1. #!/usr/bin/python
  2. # -*- coding: utf-8 -*-
  3. import httplib
  4. import urllib
  5. import urllib2
  6. import re
  7. import sys
  8. import os
  9. import Cookie
  10. import platform
  11. import xbmcplugin
  12. import xbmcgui
  13. import xbmcaddon
  14. import xbmc
  15. import xbmcaddon
  16. import httplib
  17. import re
  18. try:
  19. from hashlib import md5
  20. except:
  21. from md5 import md5
  22. import subprocess
  23. import time
  24. import random
  25. from urllib import unquote, quote
  26. import hashlib
  27. __addon__ = xbmcaddon.Addon( id = 'plugin.video.serialonline.netlite' )
  28. __language__ = __addon__.getLocalizedString
  29. sys.path.append( os.path.join( __addon__.getAddonInfo( 'path' ), 'resources', 'lib') )
  30. addon_icon = __addon__.getAddonInfo('icon')
  31. addon_fanart = __addon__.getAddonInfo('fanart')
  32. addon_path = __addon__.getAddonInfo('path')
  33. addon_type = __addon__.getAddonInfo('type')
  34. addon_id = __addon__.getAddonInfo('id')
  35. addon_author = __addon__.getAddonInfo('author')
  36. addon_name = __addon__.getAddonInfo('name')
  37. addon_version = __addon__.getAddonInfo('version')
  38. hos = int(sys.argv[1])
  39. show_len=50
  40. httpSiteUrl='http://serialonline.net/'
  41. import sqlite3 as db
  42. db_name = os.path.join( addon_path, "requests.db" )
  43. c = db.connect(database=db_name)
  44. cu = c.cursor()
  45. try:
  46. import json
  47. except ImportError:
  48. try:
  49. import simplejson as json
  50. xbmc.log( '[%s]: Error import json. Uses module simplejson' % addon_id, 2 )
  51. except ImportError:
  52. try:
  53. import demjson3 as json
  54. xbmc.log( '[%s]: Error import simplejson. Uses module demjson3' % addon_id, 3 )
  55. except ImportError:
  56. xbmc.log( '[%s]: Error import demjson3. Sorry.' % addon_id, 4 )
  57. from BeautifulSoup import BeautifulSoup, BeautifulStoneSoup
  58. def construct_request(params):
  59. return '%s?%s' % (sys.argv[0], urllib.urlencode(params))
  60. def showMessage(heading, message, times = 3000, pics = addon_icon):
  61. try: xbmc.executebuiltin('XBMC.Notification("%s", "%s", %s, "%s")' % (heading.encode('utf-8'), message.encode('utf-8'), times, pics.encode('utf-8')))
  62. except Exception, e:
  63. xbmc.log( '[%s]: showMessage: Transcoding UTF-8 failed [%s]' % (addon_id, e), 2 )
  64. try: xbmc.executebuiltin('XBMC.Notification("%s", "%s", %s, "%s")' % (heading, message, times, pics))
  65. except Exception, e:
  66. xbmc.log( '[%s]: showMessage: exec failed [%s]' % (addon_id, e), 3 )
  67. MAX_DELAY=99999999999
  68. def add_to_db(n, item,ddata):
  69. err=0
  70. m = hashlib.md5()
  71. m.update(str(n))
  72. req=m.hexdigest()
  73. #print req
  74. data=quote(item)
  75. img=ddata
  76. litm=str(len(data))
  77. try:
  78. old=[]
  79. for row in cu.execute('SELECT time FROM cache ORDER BY time'):
  80. tout = row[0]
  81. delay=int(time.time())-int(tout)
  82. if int(delay)>MAX_DELAY:
  83. old.append(tout)
  84. for dels in old:
  85. print '?????? ??????? %s'%str(dels)
  86. cu.execute("DELETE FROM cache WHERE time = '%s'" % dels)
  87. c.commit()
  88. except: pass
  89. try:
  90. cu.execute("INSERT INTO cache VALUES ('%s','%s','%s','%s')"%(req,img,data,int(time.time())))
  91. c.commit()
  92. except:
  93. try:
  94. cu.execute("CREATE TABLE cache (link, img, data, time int)")
  95. c.commit()
  96. cu.execute("INSERT INTO cache VALUES ('%s','%s','%s','%s')"%(req,img,data,int(time.time())))
  97. c.commit()
  98. except: pass
  99. def get_inf_db(n):
  100. m = hashlib.md5()
  101. m.update(str(n))
  102. req=m.hexdigest()
  103. cu.execute("SELECT time FROM cache WHERE link = '%s'" % req)
  104. c.commit()
  105. tout = cu.fetchone()[0]
  106. if int(tout)>0:
  107. delay=int(time.time())-int(tout)
  108. #print 'delay=%s'%str(delay)
  109. if int(delay)>MAX_DELAY:
  110. #print '?????? ??????? %s'%str(tout)
  111. cu.execute("DELETE FROM cache WHERE link = '%s'" % req)
  112. c.commit()
  113. info=''
  114. #print '?????? ??????? %s'%tout
  115. else:
  116. cu.execute("SELECT data,img FROM cache WHERE link = '%s'" % req)
  117. c.commit()
  118. ginfo = cu.fetchone()
  119. #print ginfo
  120. info= unquote(str(ginfo[0]))
  121. #info=info[3:(len(info)-3)].decode()
  122. img=ginfo[0]
  123. txt=unquote(ginfo[1]).encode('utf-8')
  124. #print ginfo[0],txt
  125. return [info,txt]
  126. else: return ''
  127. def GET(target, post=None):
  128. #print target
  129. try:
  130. req = urllib2.Request(url = target, data = post)
  131. req.add_header('User-Agent', 'Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0; Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1) ; .NET CLR 1.1.4322; .NET CLR 2.0.50727; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729; .NET4.0C)')
  132. #req.add_header('Host', 'online.stepashka.com')
  133. req.add_header('Accept', '*/*')
  134. req.add_header('Accept-Language', 'ru-RU')
  135. req.add_header('Accept-Charset', 'utf-8')
  136. req.add_header('Referer', 'http://serialonline.net/')
  137. resp = urllib2.urlopen(req)
  138. req.add_header('Content-Type','application/x-www-form-urlencoded')
  139. #CE = resp.headers.get('content-encoding')
  140. http = resp.read()
  141. #resp.close()
  142. return http
  143. except Exception, e:
  144. xbmc.log( '[%s]: GET EXCEPT [%s]' % (addon_id, e), 4 )
  145. showMessage('HTTP ERROR', e, 5000)
  146. def mainScreen(params):
  147. #print 'start main'
  148. li = xbmcgui.ListItem('[?????]', addon_fanart, addon_icon)
  149. li.setProperty('IsPlayable', 'false')
  150. uri = construct_request({
  151. 'func': 'doSearch'
  152. })
  153. xbmcplugin.addDirectoryItem(hos, uri, li, True)
  154. http = GET(httpSiteUrl)
  155. #print http
  156. if http == None: return False
  157. beautifulSoup = BeautifulSoup(http)
  158. content = beautifulSoup.findAll('div', attrs={'class': 'head-bl bl-title'})
  159. ind=0
  160. for line in content:
  161. li = xbmcgui.ListItem(line.string, addon_fanart, addon_icon)
  162. li.setProperty('IsPlayable', 'false')
  163. #href = line['href']
  164. uri = construct_request({
  165. 'href': line.string.encode('utf-8'),
  166. 'ind':ind,
  167. 'func': 'readCategory'
  168. })
  169. xbmcplugin.addDirectoryItem(hos, uri, li, True)
  170. ind=ind+1
  171. list= beautifulSoup.findAll('tr', attrs={'id': 'listraw'})
  172. cnt= len(list)
  173. news = beautifulSoup.findAll('div', attrs={'class': 'full_movie'})
  174. for new in news:
  175. title= '[COLOR FFFF0000]?????????? %s[/COLOR]'%new.find('h2').string.encode('utf-8')
  176. li = xbmcgui.ListItem(title, addon_icon, addon_icon)
  177. xbmcplugin.addDirectoryItem(hos, '', li)
  178. list= new.findAll('tr', attrs={'id': 'listraw'})
  179. for t in list:
  180. add= t.find('td', attrs={'id': 'rightlist'}).string.encode('utf-8')
  181. title= '[COLOR FF00FF00]%s:[/COLOR] %s'%(t.find('a').string.encode('utf-8'),add)
  182. link=t.find('a')['href']
  183. img=desk=""
  184. li = xbmcgui.ListItem(title, img, img)
  185. li.setInfo(type='video', infoLabels = {'plot':desk,'plotoutline':desk})
  186. uri = construct_request({
  187. 'href': link,
  188. 'func': 'readFolder'
  189. })
  190. xbmcplugin.addDirectoryItem(hos, uri, li, True,totalItems=int(cnt))
  191. xbmcplugin.endOfDirectory(hos)
  192. def readCategory(params):
  193. #params['href']=params['href'].encode('utf-8')
  194. http = GET(httpSiteUrl)
  195. #print http
  196. if http == None: return False
  197. beautifulSoup = BeautifulSoup(http)
  198. content = beautifulSoup.findAll('div', attrs={'class': 'top-bl'})
  199. #print content
  200. pist=content[int(params['ind'])]
  201. dist=pist.findAll('a')
  202. print len(dist)
  203. for n in dist:
  204. img=desk=""
  205. #except: pass
  206. li = xbmcgui.ListItem(str(n.string), img, img)
  207. li.setInfo(type='video', infoLabels = {'plot':desk,'plotoutline':desk})
  208. uri = construct_request({
  209. 'href': n['href'],
  210. 'img':img,
  211. 'func': 'readFolder'
  212. })
  213. xbmcplugin.addDirectoryItem(hos, uri, li, True,totalItems=int(len(dist)))
  214. xbmcplugin.endOfDirectory(hos)
  215. def readFolder(params):
  216. print params
  217. http = GET(params['href'])
  218. beautifulSoup = BeautifulSoup(http)
  219. content = beautifulSoup.findAll('div', attrs={'class': 'quote'})
  220. try:
  221. pist=content[0]
  222. except: pist=None
  223. z=None
  224. try: img='http://serialonline.net%s'%re.findall('left--><img src="(.+)" align="left" alt=',http)[0]
  225. except:
  226. img=""
  227. if pist:
  228. dist=pist.findAll('a')
  229. for n in dist:
  230. #print n
  231. #print str(n.find('b')).replace('<b>','').replace('</b>','')
  232. li = xbmcgui.ListItem(str(n.find('b')).replace('<b>','').replace('</b>',''), img, img)
  233. uri = construct_request({
  234. 'href': n['href'],
  235. 'func': 'readFolder'
  236. })
  237. #print n['href']
  238. xbmcplugin.addDirectoryItem(hos, uri, li, True)
  239. z=True
  240. header=re.findall('<h1>[^/]+</h1>',http)[0].replace('</h1>','').replace('<h1>','').decode('cp1251').encode('utf-8')
  241. header='[COLOR FF00FF00]%s:[/COLOR]'%header
  242. if z:
  243. li = xbmcgui.ListItem(' ', addon_fanart, addon_icon)
  244. uri = construct_request({
  245. 'filr': None
  246. })
  247. xbmcplugin.addDirectoryItem(hos, uri, li, False)
  248. li = xbmcgui.ListItem(header, addon_fanart, addon_icon)
  249. uri = construct_request({
  250. 'filr': None
  251. })
  252. xbmcplugin.addDirectoryItem(hos, uri, li, False)
  253. # need to find flashvars="pl=http://serialonline.net/pl/810f6cc0b9e3e3cdd9c8557d09ac0ac2/beskontrolnye_1.txt&config=http://s
  254. playlist=re.findall('http://serialonline.net/pl/[0-9a-z]+/[^/]+.txt',http)
  255. if playlist==[]: playlist=re.findall('http://cdn.serialonline.net/pl/[0-9a-z]+/[^/]+.txt',http)
  256. fplaylist= playlist[0]
  257. files= GET(fplaylist)
  258. #print files
  259. flist=json.loads(files)
  260. #style="display:inline;"><!--dle_image_begin:(.+?)|left-->
  261. for index in flist['playlist']:
  262. li = xbmcgui.ListItem(index['comment'], img, img)
  263. li.setProperty('IsPlayable', 'true')
  264. uri = construct_request({
  265. 'url': index['file'],
  266. 'func': 'play'
  267. })
  268. xbmcplugin.addDirectoryItem(hos, uri, li, False)
  269. #li = xbmcgui.ListItem(playlist[0].split['='][1], addon_fanart, addon_icon)
  270. #uri = construct_request({
  271. # 'func': 'readFolder'
  272. #})
  273. #print n['href']
  274. #xbmcplugin.addDirectoryItem(hos, uri, li, True)
  275. xbmcplugin.setContent(hos, 'movies')
  276. xbmcplugin.endOfDirectory(hos)
  277. def play(params):
  278. i = xbmcgui.ListItem(path = params['url'])
  279. xbmcplugin.setResolvedUrl(hos, True, i)
  280. def doSearch(params):
  281. kbd = xbmc.Keyboard()
  282. kbd.setDefault('')
  283. kbd.setHeading('?????')
  284. kbd.doModal()
  285. if kbd.isConfirmed():
  286. sts=kbd.getText();
  287. params['href'] = 'http://serialonline.net/index.php?do=search'
  288. post={}
  289. post['do']='search'
  290. post['full_search']='1'
  291. post['resorder']='0'
  292. post['result_from']='all'
  293. post['search_start']='1'
  294. post['sortby']='date'
  295. post['story']=sts.decode('utf-8','ignore').encode('cp1251','ignore')
  296. post['subaction']='search'
  297. post['titleonly']='3'
  298. post['replyless']=post['replylimit']=post['searchdate']=0
  299. post['showposts']='0'
  300. post['searchuser']=''
  301. http=GET('http://serialonline.net/index.php?do=search',urllib.urlencode(post))
  302. beautifulSoup = BeautifulSoup(http)
  303. movs= beautifulSoup.findAll('div', attrs={ 'class':"full_movie"})
  304. for t in movs:
  305. z=re.findall('<h2><a href="(.+?)">(.+?)</a> </h2>',str(t))
  306. title=z[0][1]
  307. href=z[0][0]
  308. img='http://serialonline.net%s'%re.findall('left--><img src="(.+)" align="left" alt=',str(t))[0]
  309. li = xbmcgui.ListItem(title, img, img)
  310. li.setProperty('IsPlayable', 'false')
  311. uri = construct_request({
  312. 'href': href,
  313. 'func': 'readFolder'
  314. })
  315. xbmcplugin.addDirectoryItem(hos, uri, li, True)
  316. xbmcplugin.setContent(hos, 'movies')
  317. xbmcplugin.endOfDirectory(hos)
  318. def get_params(paramstring):
  319. param=[]
  320. if len(paramstring)>=2:
  321. params=paramstring
  322. cleanedparams=params.replace('?','')
  323. if (params[len(params)-1]=='/'):
  324. params=params[0:len(params)-2]
  325. pairsofparams=cleanedparams.split('&')
  326. param={}
  327. for i in range(len(pairsofparams)):
  328. splitparams={}
  329. splitparams=pairsofparams[i].split('=')
  330. if (len(splitparams))==2:
  331. param[splitparams[0]]=splitparams[1]
  332. if len(param) > 0:
  333. for cur in param:
  334. param[cur] = urllib.unquote_plus(param[cur])
  335. return param
  336. print 'ok'
  337. params = get_params(sys.argv[2])
  338. try:
  339. func = params['func']
  340. del params['func']
  341. except:
  342. func = None
  343. xbmc.log( '[%s]: Primary input' % addon_id, 1 )
  344. mainScreen(params)
  345. if func != None:
  346. try: pfunc = globals()[func]
  347. except:
  348. pfunc = None
  349. xbmc.log( '[%s]: Function "%s" not found' % (addon_id, func), 4 )
  350. showMessage('Internal addon error', 'Function "%s" not found' % func, 2000)
  351. if pfunc: pfunc(params)