modified: configure.ac
[enigma2-plugins.git] / subsdownloader2 / src / SourceCode / xbmc_subtitles / services / Itasa / service.py
1 # -*- coding: UTF-8 -*-
2
3 import os, sys, re, string, time, urllib, urllib2, cookielib
4 from Screens.MessageBox import MessageBox
5 from Components.config import config
6 #import xbmc, xbmcgui
7 from Plugins.Extensions.SubsDownloader2.SourceCode.xbmc_subtitles.XBMC_search import list_directory_files, new_file_in_directory
8 from Plugins.Extensions.SubsDownloader2.SourceCode.xbmc_subtitles.utilities import log
9 from Plugins.Extensions.SubsDownloader2.SourceCode.archives_extractor import zip_extractor
10 #_ = sys.modules[ "__main__" ].__language__
11 #__scriptname__ = sys.modules[ "__main__" ].__scriptname__
12 #__settings__ = sys.modules[ "__main__" ].__settings__
13
14 main_url = "http://www.italiansubs.net/"
15
16 #====================================================================================================================
17 # Regular expression patterns
18 #====================================================================================================================
19
20 #<input type="hidden" name="return" value="aHR0cDovL3d3dy5pdGFsaWFuc3Vicy5uZXQv" /><input type="hidden" name="c10b48443ee5730c9b5a0927736bd09f" value="1" />
21 unique_pattern = '<input type="hidden" name="return" value="([^\n\r\t ]+?)" /><input type="hidden" name="([^\n\r\t ]+?)" value="([^\n\r\t ]+?)" />'
22 #<a href="http://www.italiansubs.net/index.php?option=com_remository&amp;Itemid=6&amp;func=select&amp;id=1170"> Castle</a>
23 show_pattern = '<a href="http://www\.italiansubs\.net/(index.php\?option=com_remository&amp;Itemid=\d+&amp;func=select&amp;id=[^\n\r\t ]+?)"> %s</a>'
24 #href="http://www.italiansubs.net/index.php?option=com_remository&amp;Itemid=6&amp;func=select&amp;id=1171"> Stagione 1</a>
25 season_pattern = '<a href="http://www\.italiansubs\.net/(index.php\?option=com_remository&amp;Itemid=\d+?&amp;func=select&amp;id=[^\n\r\t ]+?)"> Stagione %s</a>'
26 #<img src='http://www.italiansubs.net/components/com_remository/images/folder_icons/category.gif' width=20 height=20><a name="1172"><a href="http://www.italiansubs.net/index.php?option=com_remository&amp;Itemid=6&amp;func=select&amp;id=1172"> 720p</a>
27 category_pattern = '<img src=\'http://www\.italiansubs\.net/components/com_remository/images/folder_icons/category\.gif\' width=20 height=20><a name="[^\n\r\t ]+?"><a href="http://www\.italiansubs\.net/(index.php\?option=com_remository&amp;Itemid=\d+?&amp;func=select&amp;id=[^\n\r\t ]+?)"> ([^\n\r\t]+?)</a>'
28 #<a href="http://www.italiansubs.net/index.php?option=com_remository&amp;Itemid=6&amp;func=fileinfo&amp;id=7348">Dexter 3x02</a>
29 subtitle_pattern = '<a href="http://www\.italiansubs\.net/(index.php\?option=com_remository&amp;Itemid=\d+?&amp;func=fileinfo&amp;id=([^\n\r\t ]+?))">(%s %sx%02d.*?)</a>'
30 #<a href='http://www.italiansubs.net/index.php?option=com_remository&amp;Itemid=6&amp;func=download&amp;id=7228&amp;chk=5635630f675375afbdd6eec317d8d688&amp;no_html=1'>
31 subtitle_download_pattern = '<a href=\'http://www\.italiansubs\.net/(index\.php\?option=com_remository&amp;Itemid=\d+?&amp;func=download&amp;id=%s&amp;chk=[^\n\r\t ]+?&amp;no_html=1\')>'
32
33
34 #====================================================================================================================
35 # Functions
36 #====================================================================================================================
37
38 def geturl(url):
39     log( __name__ , " Getting url: %s" % (url))
40     try:
41         response = urllib2.urlopen(url)
42         content = response.read()
43     except:
44         log( __name__ , " Failed to get url:%s" % (url))
45         content = None
46     return(content)
47
48
49 def login(username, password):
50     log( __name__ , " Logging in with username '%s' ..." % (username))
51     content= geturl(main_url + 'index.php')
52     if content is not None:
53         match = re.search('logouticon.png', content, re.IGNORECASE | re.DOTALL)
54         if match:
55             return 1
56         else:
57             match = re.search(unique_pattern, content, re.IGNORECASE | re.DOTALL)
58             if match:
59                 return_value = match.group(1)
60                 unique_name = match.group(2)
61                 unique_value = match.group(3)
62                 login_postdata = urllib.urlencode({'username': username, 'passwd': password, 'remember': 'yes', 'Submit': 'Login', 'remember': 'yes', 'option': 'com_user', 'task': 'login', 'silent': 'true', 'return': return_value, unique_name: unique_value} )
63                 cj = cookielib.CookieJar()
64                 my_opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))
65                 my_opener.addheaders = [('Referer', main_url)]
66                 urllib2.install_opener(my_opener)
67                 request = urllib2.Request(main_url + 'index.php',login_postdata)
68                 response = urllib2.urlopen(request).read()
69                 match = re.search('logouticon.png', response, re.IGNORECASE | re.DOTALL)
70                 if match:
71                     return 1
72                 else:
73                     return 0
74     else:
75         return 0
76
77
78 #def search_subtitles( file_original_path, title, tvshow, year, season, episode, set_temp, rar, lang1, lang2, lang3, stack ): #standard input
79 def search_subtitles( file_original_path, title, tvshow, year, season, episode, set_temp, rar, lang1, lang2, lang3, stack, screen_session): #standard input
80     subtitles_list = []
81     subtitles_list = []
82     msg = ""
83     if len(tvshow) > 0:
84         italian = 0
85         if (string.lower(lang1) == "italian") or (string.lower(lang2) == "italian") or (string.lower(lang3) == "italian") or (string.lower(lang1) == "all") or (string.lower(lang2) == "all") or (string.lower(lang3) == "all"):
86             #username = __settings__.getSetting( "ITuser" )
87             #password = __settings__.getSetting( "ITpass" )
88             username = config.plugins.subsdownloader.ItasaUser.value
89             password = config.plugins.subsdownloader.ItasaPassword.value
90             if login(username, password):
91                 log( __name__ , " Login successful")
92                 content= geturl(main_url + 'index.php?option=com_remository&Itemid=6')
93                 if content is not None:
94                     match = re.search(show_pattern % tvshow, content, re.IGNORECASE | re.DOTALL)
95                     if match:
96                         log( __name__ ," Tv show '%s' found" % tvshow)
97                         content= geturl(main_url + match.group(1))
98                         if content is not None:
99                             match = re.search(season_pattern % season, content, re.IGNORECASE | re.DOTALL)
100                             if match:
101                                 log( __name__ ," Season %s of tv show '%s' found" % (season, tvshow))
102                                 category = 'normal'
103                                 categorypage = match.group(1)
104                                 content= geturl(main_url + categorypage)
105                                 if content is not None:
106                                     for matches in re.finditer(subtitle_pattern % (tvshow, int(season), int(episode)), content, re.IGNORECASE | re.DOTALL):
107                                         filename = matches.group(3)
108                                         id = matches.group(2)
109                                         log( __name__ ," Adding '%s' to list of subtitles" % filename)
110                                         subtitles_list.append({'rating': '0', 'no_files': 1, 'filename': filename, 'sync': False, 'id' : id, 'link' : categorypage, 'language_flag': 'flags/it.gif', 'language_name': 'Italian'})
111                                     for matches in re.finditer(category_pattern, content, re.IGNORECASE | re.DOTALL):
112                                         categorypage = matches.group(1)
113                                         category = matches.group(2)
114                                         log( __name__ ," Page for category '%s' found" % category)
115                                         content= geturl(main_url + categorypage)
116                                         if content is not None:
117                                             for matches in re.finditer(subtitle_pattern % (tvshow, int(season), int(episode)), content, re.IGNORECASE | re.DOTALL):
118                                                 id = matches.group(2)
119                                                 filename = matches.group(3)
120                                                 log( __name__ ," Adding '%s (%s)' to list of subtitles" % (filename, category))
121                                                 subtitles_list.append({'rating': '0', 'no_files': 1, 'filename': "%s (%s)" % (filename, category), 'sync': False, 'id' : id, 'link' : categorypage, 'language_flag': 'flags/it.gif', 'language_name': 'Italian'})
122                             else:
123                                 log( __name__ ," Season %s of tv show '%s' not found" % (season, tvshow))
124                                 msg = "Season %s of tv show '%s' not found" % (season, tvshow)
125                     else:
126                         log( __name__ ," Tv show '%s' not found." % tvshow)
127                         msg = "Tv show '%s' not found" % tvshow
128             else:
129                 #log( __name__ ," Login to Itasa failed. Check your username/password at the addon configuration.")
130                 screen_session.open(MessageBox,_(" Login to Itasa failed. Check your username/password at the configuration menu."), MessageBox.TYPE_INFO, timeout = 5)
131                                
132                 msg = "Login to Itasa failed. Check your username/password at the addon configuration."
133         else:
134             msg = "Won't work, Itasa is only for Italian subtitles."
135     else:
136         msg = "Won't work, Itasa is only for tv shows."
137     return subtitles_list, "", msg #standard output
138
139
140 #def download_subtitles (subtitles_list, pos, zip_subs, tmp_sub_dir, sub_folder, session_id): #standard input
141 def download_subtitles (subtitles_list, pos, zip_subs, tmp_sub_dir, sub_folder, session_id, screen_session): #standard input
142     #username = __settings__.getSetting( "ITuser" )
143     #password = __settings__.getSetting( "ITpass" )
144     username = config.plugins.subsdownloader.ItasaUser.value
145     password = config.plugins.subsdownloader.ItasaPassword.value
146     
147     if login(username, password):
148         log( __name__ , " Login successful")
149         id = subtitles_list[pos][ "id" ]
150         link = subtitles_list[pos][ "link" ] 
151         
152         content= geturl(main_url + link)        
153         match = re.search(subtitle_download_pattern % id, content, re.IGNORECASE | re.DOTALL)
154
155         if match:
156             language = subtitles_list[pos][ "language_name" ]
157             log( __name__ ," Fetching subtitles using url %s" % (main_url + match.group(1)))
158             content = geturl(main_url + match.group(1))
159             if content is not None:
160                 header = content[:4]
161                 if header == 'Rar!':
162                     local_tmp_file = os.path.join(tmp_sub_dir, "undertexter.rar")
163                     packed = True
164                 elif header == 'PK\ 3\ 4':
165                     local_tmp_file = os.path.join(tmp_sub_dir, "undertexter.zip")
166                     packed = True
167                 else: # never found/downloaded an unpacked subtitles file, but just to be sure ...
168                     local_tmp_file = os.path.join(tmp_sub_dir, "undertexter.srt") # assume unpacked subtitels file is an '.srt'
169                     subs_file = local_tmp_file
170                     packed = False
171                 log( __name__ ," Saving subtitles to '%s'" % (local_tmp_file))
172                                 
173                 try:
174                     local_file_handle = open(local_tmp_file, "wb")
175                     local_file_handle.write(content)
176                     local_file_handle.close()
177                 except:
178                     log( __name__ ," Failed to save subtitles to '%s'" % (local_tmp_file))
179                 if packed:
180                     if header == 'PK\ 3\ 4':
181                         zipped_file = zip_extractor(local_tmp_file, tmp_sub_dir)
182                         subs_file = zipped_file.extract_zipped_file()
183                         os.remove(local_tmp_file)
184                     if header == 'Rar!':
185                         if os.path.exists("/usr/bin/unrar"): 
186                             files_before_unrar = list_directory_files(tmp_sub_dir)
187                             os.system('unrar -p- -y x %s %s' % (local_tmp_file,tmp_sub_dir))
188                             files_after_unrar = list_directory_files(tmp_sub_dir)
189                             subs_file = new_file_in_directory(files_before_unrar,files_after_unrar)
190                             os.remove(local_tmp_file)
191                         else:
192                             from Components.Ipkg import IpkgComponent
193                             from Screens.Ipkg import Ipkg
194                             __cmdList = []
195                             __cmdList.append((IpkgComponent.CMD_INSTALL, { "package": 'unrar' }))
196                             screen_session.openWithCallback(__restartMessage__(screen_session, callback = None), Ipkg, cmdList = __cmdList)
197                     
198                 return False, language, subs_file #standard output
199     #log( __name__ ," Login to Itasa failed. Check your username/password at the addon configuration.")
200     screen_session.open(MessageBox,_(" Login to Itasa failed. Check your username/password at the configuration menu."), MessageBox.TYPE_INFO, timeout = 5)
201     return False, "None", []
202
203 def __restartMessage__(screen_session, callback = None):
204         screen_session.open(MessageBox,_("Please restart GUI to apply changes.\n\n If UNRAR package haven't been installed it means that Your image doesn't have it. \n You can download package from project Google Code page or unpack rar manually and make local convertion."), MessageBox.TYPE_INFO, timeout = 25)