PageRenderTime 27ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/script.xbmc.subtitles/resources/lib/services/Subs4Free/service.py

https://github.com/yllar/script.xbmc.subtitles
Python | 292 lines | 288 code | 3 blank | 1 comment | 7 complexity | c34355079467d5da246c651a45270d56 MD5 | raw file
Possible License(s): AGPL-1.0
  1. # -*- coding: UTF-8 -*-
  2. import os, sys, re, xbmc, xbmcgui, string, time, urllib, urllib2
  3. import mechanize
  4. from utilities import log
  5. _ = sys.modules[ "__main__" ].__language__
  6. movie_url = "http://www.small-industry.com"
  7. tvshow_url = "http://www.subs4series.com"
  8. debug_pretext = "subs4free"
  9. def get_url(url,referer=None):
  10. if referer is None:
  11. headers = {'User-agent' : 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:6.0) Gecko/20100101 Firefox/6.0'}
  12. else:
  13. headers = {'User-agent' : 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:6.0) Gecko/20100101 Firefox/6.0', 'Referer' : referer}
  14. req = urllib2.Request(url,None,headers)
  15. response = urllib2.urlopen(req)
  16. content = response.read()
  17. response.close()
  18. content = content.replace('\n','')
  19. return content
  20. def get_rating(downloads):
  21. rating = int(downloads)
  22. if (rating < 50):
  23. rating = 1
  24. elif (rating >= 50 and rating < 100):
  25. rating = 2
  26. elif (rating >= 100 and rating < 150):
  27. rating = 3
  28. elif (rating >= 150 and rating < 200):
  29. rating = 4
  30. elif (rating >= 200 and rating < 250):
  31. rating = 5
  32. elif (rating >= 250 and rating < 300):
  33. rating = 6
  34. elif (rating >= 300 and rating < 350):
  35. rating = 7
  36. elif (rating >= 350 and rating < 400):
  37. rating = 8
  38. elif (rating >= 400 and rating < 450):
  39. rating = 9
  40. elif (rating >= 450):
  41. rating = 10
  42. return rating
  43. def unpack_subtitles(local_tmp_file, zip_subs, tmp_sub_dir, sub_folder):
  44. files = os.listdir(tmp_sub_dir)
  45. init_filecount = len(files)
  46. max_mtime = 0
  47. filecount = init_filecount
  48. # determine the newest file from tmp_sub_dir
  49. for file in files:
  50. if (string.split(file,'.')[-1] in ['srt','sub','txt']):
  51. mtime = os.stat(os.path.join(tmp_sub_dir, file)).st_mtime
  52. if mtime > max_mtime:
  53. max_mtime = mtime
  54. init_max_mtime = max_mtime
  55. time.sleep(2) # wait 2 seconds so that the unpacked files are at least 1 second newer
  56. xbmc.executebuiltin("XBMC.Extract(" + local_tmp_file + "," + tmp_sub_dir +")")
  57. waittime = 0
  58. while (filecount == init_filecount) and (waittime < 20) and (init_max_mtime == max_mtime): # nothing yet extracted
  59. time.sleep(1) # wait 1 second to let the builtin function 'XBMC.extract' unpack
  60. files = os.listdir(tmp_sub_dir)
  61. filecount = len(files)
  62. # determine if there is a newer file created in tmp_sub_dir (marks that the extraction had completed)
  63. for file in files:
  64. if (string.split(file,'.')[-1] in ['srt','sub','txt']):
  65. mtime = os.stat(os.path.join(tmp_sub_dir, file)).st_mtime
  66. if (mtime > max_mtime):
  67. max_mtime = mtime
  68. waittime = waittime + 1
  69. if waittime == 20:
  70. log( __name__ ," Failed to unpack subtitles in '%s'" % (tmp_sub_dir))
  71. pass
  72. else:
  73. log( __name__ ," Unpacked files in '%s'" % (tmp_sub_dir))
  74. pass
  75. for file in files:
  76. # there could be more subtitle files in tmp_sub_dir, so make sure we get the newly created subtitle file
  77. if (string.split(file, '.')[-1] in ['srt', 'sub', 'txt']) and (os.stat(os.path.join(tmp_sub_dir, file)).st_mtime > init_max_mtime): # unpacked file is a newly created subtitle file
  78. log( __name__ ," Unpacked subtitles file '%s'" % (file))
  79. subs_file = os.path.join(tmp_sub_dir, file)
  80. def search_subtitles(file_original_path, title, tvshow, year, season, episode, set_temp, rar, lang1, lang2, lang3, stack): #standard input
  81. subtitles_list = []
  82. msg = ""
  83. if not (string.lower(lang1) or string.lower(lang2) or string.lower(lang3)) == "greek":
  84. msg = "Won't work, subs4free is only for Greek subtitles."
  85. return subtitles_list, "", msg #standard output
  86. try:
  87. log( __name__ ,"%s Clean title = %s" % (debug_pretext, title))
  88. premiered = year
  89. title, year = xbmc.getCleanMovieTitle( title )
  90. except:
  91. pass
  92. content = 1
  93. if len(tvshow) == 0: # Movie
  94. searchstring = "%s (%s)" % (title, premiered)
  95. elif len(tvshow) > 0 and title == tvshow: # Movie not in Library
  96. searchstring = "%s (%#02d%#02d)" % (tvshow, int(season), int(episode))
  97. elif len(tvshow) > 0: # TVShow
  98. searchstring = "%s S%#02dE%#02d" % (tvshow, int(season), int(episode))
  99. content = 2
  100. else:
  101. searchstring = title
  102. log( __name__ ,"%s Search string = %s" % (debug_pretext, searchstring))
  103. if content == 1:
  104. get_movie_subtitles_list(searchstring, "el", "Greek", subtitles_list)
  105. else:
  106. get_tvshow_subtitles_list(searchstring, "el", "Greek", subtitles_list)
  107. return subtitles_list, "", msg #standard output
  108. def download_subtitles(subtitles_list, pos, zip_subs, tmp_sub_dir, sub_folder, session_id): #standard input
  109. id = subtitles_list[pos][ "id" ]
  110. language = subtitles_list[pos][ "language_name" ]
  111. name = subtitles_list[pos][ "filename" ]
  112. name = name.replace('.',' ')
  113. # Browser
  114. browser = mechanize.Browser()
  115. browser.set_handle_refresh(mechanize._http.HTTPRefreshProcessor(), max_time=1)
  116. browser.addheaders = [('User-agent', 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:6.0) Gecko/20100101 Firefox/6.0')]
  117. browser.addheaders = [('Referer', id)]
  118. try:
  119. log( __name__ ,"%s Getting url: %s" % (debug_pretext, id))
  120. response = browser.open(id)
  121. content = response.read()
  122. log( __name__ ,"%s Getting subtitle link" % (debug_pretext))
  123. try:
  124. subtitle_id = re.compile('href="(getSub-.+?)"').findall(content.replace('\n',''))[1] # movie_url
  125. except:
  126. pass
  127. try:
  128. subtitle_id = re.compile('href="(/getSub-.+?)"').findall(content.replace('\n',''))[1] # tvshow_url
  129. except:
  130. pass
  131. log( __name__ ,"%s Getting url: %s" % (debug_pretext, subtitle_id))
  132. response = browser.open(subtitle_id)
  133. content = response.read()
  134. type = response.info()["Content-type"]
  135. except:
  136. log( __name__ ,"%s Failed to parse url:%s" % (debug_pretext, id))
  137. return True,language, "" #standard output
  138. if type == 'application/x-rar-compressed':
  139. local_tmp_file = os.path.join(tmp_sub_dir, "subs4series.rar")
  140. packed = True
  141. elif type == 'application/zip':
  142. local_tmp_file = os.path.join(tmp_sub_dir, "subs4series.zip")
  143. packed = True
  144. elif not type.startswith('text/html'): # never found/downloaded an unpacked subtitles file, but just to be sure ...
  145. local_tmp_file = os.path.join(tmp_sub_dir, "subs4series.srt") # assume unpacked subtitels file is an '.srt'
  146. subs_file = local_tmp_file
  147. packed = False
  148. else:
  149. try:
  150. log( __name__ ,"%s Getting subtitles by subz.tv" % (debug_pretext))
  151. subtitles = re.compile("(<li style='margin-bottom.+?</li>)").findall(content.replace('\n',''))
  152. for subtitle in subtitles:
  153. try:
  154. try:
  155. subz = re.compile("<span.+?>(.+?)</span>.+?</b>").findall(subtitle)[0]
  156. subz = subz.replace('.',' ')
  157. except:
  158. subz = ''
  159. pass
  160. id = re.compile("<a href='(.+?)'").findall(subtitle)[0]
  161. id = id.replace('amp;','')
  162. id = 'http://www.subz.tv/infusions/pro_download_panel/%s&dlok=on' % (id)
  163. if re.search(subz,name) is not None:
  164. response = browser.open(id)
  165. content = response.read()
  166. type = response.info()["Content-type"]
  167. if type == 'application/x-rar-compressed':
  168. local_tmp_file = os.path.join(tmp_sub_dir, "subztv.rar")
  169. packed = True
  170. elif type == 'application/zip':
  171. local_tmp_file = os.path.join(tmp_sub_dir, "subztv.zip")
  172. packed = True
  173. elif not type.startswith('text/html'):
  174. local_tmp_file = os.path.join(tmp_sub_dir, "subztv.srt") # assume unpacked subtitels file is an '.srt'
  175. subs_file = local_tmp_file
  176. packed = False
  177. else:
  178. local_tmp_file = None
  179. break
  180. except:
  181. pass
  182. except:
  183. log( __name__ ,"%s Failed to get subtitles by subz.tv" % (debug_pretext))
  184. pass
  185. if local_tmp_file is not None:
  186. try:
  187. log( __name__ ,"%s Saving subtitles to '%s'" % (debug_pretext, local_tmp_file))
  188. local_file_handle = open(local_tmp_file, "wb")
  189. local_file_handle.write(content)
  190. local_file_handle.close()
  191. if packed:
  192. unpack_subtitles(local_tmp_file, zip_subs, tmp_sub_dir, sub_folder)
  193. return True,language, "" #standard output
  194. except:
  195. log( __name__ ,"%s Failed to save subtitles to '%s'" % (debug_pretext, local_tmp_file))
  196. pass
  197. return True,language, "" #standard output
  198. def get_movie_subtitles_list(searchstring, languageshort, languagelong, subtitles_list):
  199. url = '%s/search_report.php?search=%s&x=14&y=11&searchType=1' % (movie_url, urllib.quote_plus(searchstring))
  200. try:
  201. log( __name__ ,"%s Getting url: %s" % (debug_pretext, url))
  202. content = get_url(url,referer=movie_url)
  203. except:
  204. log( __name__ ,"%s Failed to get url:%s" % (debug_pretext, url))
  205. return
  206. try:
  207. log( __name__ ,"%s Getting '%s' subs ..." % (debug_pretext, languageshort))
  208. subtitles = re.compile('(/el.gif" alt="Greek".+?</B>DLs)').findall(content)
  209. except:
  210. log( __name__ ,"%s Failed to get subtitles" % (debug_pretext))
  211. return
  212. for subtitle in subtitles:
  213. try:
  214. filename = re.compile('<B>(.+?)</B>').findall(subtitle)[0]
  215. id = re.compile('href="link.php[?]p=(.+?)"').findall(subtitle)[0]
  216. id = '%s/%s' % (movie_url, id)
  217. try:
  218. uploader = re.compile('<B>(.+?)</B>').findall(subtitle)[1]
  219. filename = '[%s] %s' % (uploader, filename)
  220. except:
  221. pass
  222. try:
  223. downloads = re.compile('<B>(.+?)</B>').findall(subtitle)[-1]
  224. filename += ' [%s DLs]' % (downloads)
  225. except:
  226. pass
  227. try:
  228. rating = get_rating(downloads)
  229. except:
  230. rating = 0
  231. pass
  232. log( __name__ ,"%s Subtitles found: %s (id = %s)" % (debug_pretext, filename, id))
  233. subtitles_list.append({'rating': str(rating), 'no_files': 1, 'filename': filename, 'sync': False, 'id' : id, 'language_flag': 'flags/' + languageshort + '.gif', 'language_name': languagelong})
  234. except:
  235. pass
  236. return
  237. def get_tvshow_subtitles_list(searchstring, languageshort, languagelong, subtitles_list):
  238. url = '%s/search_report.php?search=%s&x=14&y=11&searchType=1' % (tvshow_url, urllib.quote_plus(searchstring))
  239. try:
  240. log( __name__ ,"%s Getting url: %s" % (debug_pretext, url))
  241. content = get_url(url,referer=tvshow_url)
  242. except:
  243. log( __name__ ,"%s Failed to get url:%s" % (debug_pretext, url))
  244. return
  245. try:
  246. log( __name__ ,"%s Getting '%s' subs ..." % (debug_pretext, languageshort))
  247. subtitles = re.compile('(/el.gif" alt="Greek".+?</B>DLs)').findall(content)
  248. except:
  249. log( __name__ ,"%s Failed to get subtitles" % (debug_pretext))
  250. return
  251. for subtitle in subtitles:
  252. try:
  253. filename = re.compile('<B>(.+?)</B>').findall(subtitle)[0]
  254. id = re.compile('<a href="(.+?)"').findall(subtitle)[0]
  255. id = '%s%s' % (tvshow_url, id)
  256. try:
  257. uploader = re.compile('<B>(.+?)</B>').findall(subtitle)[1]
  258. filename = '[%s] %s' % (uploader, filename)
  259. except:
  260. pass
  261. try:
  262. downloads = re.compile('<B>(.+?)</B>').findall(subtitle)[-1]
  263. filename += ' [%s DLs]' % (downloads)
  264. except:
  265. pass
  266. try:
  267. rating = get_rating(downloads)
  268. except:
  269. rating = 0
  270. pass
  271. log( __name__ ,"%s Subtitles found: %s (id = %s)" % (debug_pretext, filename, id))
  272. subtitles_list.append({'rating': str(rating), 'no_files': 1, 'filename': filename, 'sync': False, 'id' : id, 'language_flag': 'flags/' + languageshort + '.gif', 'language_name': languagelong})
  273. except:
  274. pass
  275. return