modified: configure.ac
[enigma2-plugins.git] / subsdownloader2 / src / SourceCode / xbmc_subtitles / services / Napisyme / service.py
1 # -*- coding: UTF-8 -*-
2
3 # Attention this is a Frankenstein Monster. Patched from other pieces of code,
4 # ugly and may not work at all. Feel free to improve, change anything.
5 # I do not know how to code, especially in Python, at all. 
6 # Credits to amet, Guilherme Jardim, and many more.
7 # mrto
8
9 import urllib2, re, string, sys, os
10 #, xbmc
11 from Plugins.Extensions.SubsDownloader2.SourceCode.xbmc_subtitles.utilities import log, twotofull
12 from Plugins.Extensions.SubsDownloader2.SourceCode.archives_extractor import zip_extractor
13
14 #_ = sys.modules[ "__main__" ].__language__
15
16 main_url = "http://napisy.me/search.php?str="
17 down_url = "http://napisy.me/download/sr/"
18
19 subtitle_pattern = 'alt="(.+?)" border="0" />[\r\n\t ]+?</div>[\r\n\t ]+?<div class="title">[\r\n\t ]+?<a href="javascript:void\(0\);" onclick="javascript:pobierzNapis\(\'(.+?)\'\);" title="Wydanie: (.+?)" class="vtip">[ \r\n]*?                                (.+?)[ \r\n]*?</a>'
20
21
22 def getallsubs(content, title, subtitles_list, file_original_path):
23     for matches in re.finditer(subtitle_pattern, content):
24       jezyk, numer_napisu, wydanie, tytul = matches.groups()
25       if 'other' in jezyk:
26           continue
27       else:
28           jezyk = jezyk
29       link = "%s%s/" % (down_url, numer_napisu)
30       log( __name__ ,"Subtitles found: %s %s (link=%s)" % (tytul, wydanie, link))
31       obraz_flagi = "flags/%s.gif" % (jezyk)
32       lang = twotofull(jezyk)
33       tytul_pelny = '%s %s' % (tytul, wydanie)
34       wydanie_sclean = wydanie.replace(" ","")
35       wydanie_clean = wydanie_sclean.replace(",",";")
36       wydanie_srednik = '%s;' % (wydanie_clean)
37       for wydania in re.finditer('(.+?);', wydanie_srednik):
38           wydania = wydania.group()
39       wydania_clean = wydania.replace(";","")
40       wydania_upper = wydania_clean.upper()
41       filepatch_upper = file_original_path.upper()
42       if wydania_upper in filepatch_upper:
43         sync_value = True
44       else:
45         sync_value = False
46       subtitles_list.append({'filename': tytul_pelny, 'sync': sync_value, 'link': link, 'language_flag': obraz_flagi, 'language_name': lang,'rating': ""})
47
48 #def search_subtitles( file_original_path, title, tvshow, year, season, episode, set_temp, rar, lang1, lang2, lang3, stack ): #standard input
49 def search_subtitles( file_original_path, title, tvshow, year, season, episode, set_temp, rar, lang1, lang2, lang3, stack, screen_sessiom): #standard input
50     subtitles_list = []
51     msg = ""
52     if len(tvshow) > 0:
53       for rok in re.finditer(' \(\d\d\d\d\)', tvshow):
54           rok = rok.group()
55           if len(rok) > 0:
56               tvshow = tvshow.replace(rok, "")
57           else:
58               continue
59       tvshow_plus = tvshow.replace(" ","+")
60       if len(str(season)) < 2:
61         season_full = '0%s' % (season)
62       else:
63         season_full = str(season)
64       if len(str(episode)) < 2:
65         episode_full = '0%s' % (episode)
66       else:
67         episode_full = str(episode)
68       url = '%s%s+%sx%s' % (main_url, tvshow_plus, season_full, episode_full)
69     else:
70       original_title = title #xbmc.getInfoLabel("VideoPlayer.OriginalTitle")
71       log( __name__ ,"Original title: [%s]" % (original_title))
72       movie_title_plus = original_title.replace(" ","+")
73       url = '%s%s' % (main_url, movie_title_plus)
74     log( __name__ , "Pobieram z [ %s ]" % (url))     
75     response = urllib2.urlopen(url)
76     content = response.read()
77     getallsubs(content, title, subtitles_list, file_original_path)
78     return subtitles_list, "", "" #standard output
79
80 #def download_subtitles (subtitles_list, pos, zip_subs, tmp_sub_dir, sub_folder, session_id): #standard input
81 def download_subtitles (subtitles_list, pos, zip_subs, tmp_sub_dir, sub_folder, session_id, screen_session):  #standard input
82     import urllib
83     f = urllib.urlopen(subtitles_list[pos][ "link" ])
84     language = subtitles_list[pos][ "language_name" ]
85    
86     local_tmp_file = os.path.join(tmp_sub_dir, "zipsubs.zip")
87     log( __name__ ,"Saving subtitles to '%s'" % (local_tmp_file))
88     
89     local_file = open(zip_subs, "w" + "b")
90     local_file.write(f.read())
91     local_file.close()
92     zipped_file = zip_extractor(zip_subs, destination_dir = tmp_sub_dir)
93     subs_file = zipped_file.extract_zipped_file() 
94     os.remove(zip_subs)   
95     return True,language, subs_file #standard output