update de translation
[enigma2-plugins.git] / mytube / src / MyTubeService.py
1 # -*- coding: iso-8859-1 -*-
2 from __init__ import _
3 import gdata.youtube
4 import gdata.youtube.service
5 from gdata.service import BadAuthentication
6 from Tools.LoadPixmap import LoadPixmap
7 from Components.config import config, Config, ConfigSelection, ConfigText, getConfigListEntry, ConfigSubsection, ConfigYesNo, ConfigIP, ConfigNumber
8 from Components.ConfigList import ConfigListScreen
9 from Components.config import KEY_DELETE, KEY_BACKSPACE, KEY_LEFT, KEY_RIGHT, KEY_HOME, KEY_END, KEY_TOGGLEOW, KEY_ASCII, KEY_TIMEOUT
10
11 from twisted.web import client
12 from twisted.internet import reactor
13 from urllib2 import Request, URLError, HTTPError
14 from socket import gaierror,error
15 import re, os, sys, socket
16 import urllib
17 from urllib import FancyURLopener, quote
18 import cookielib
19 from httplib import HTTPConnection,CannotSendRequest,BadStatusLine
20 HTTPConnection.debuglevel = 1
21
22 #config.plugins.mytube = ConfigSubsection()
23 #config.plugins.mytube.general = ConfigSubsection()
24 #config.plugins.mytube.general.useHTTPProxy = ConfigYesNo(default = False)
25 #config.plugins.mytube.general.ProxyIP = ConfigIP(default=[0,0,0,0])
26 #config.plugins.mytube.general.ProxyPort = ConfigNumber(default=8080)
27
28 class MyOpener(FancyURLopener):
29         version = 'Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.8.0.12) Gecko/20070731 Ubuntu/dapper-security Firefox/1.5.0.12'
30
31
32 class GoogleSuggestions():
33         def __init__(self, callback, ds = None, json = None, hl = None):
34                 self.callback = callback
35                 self.conn = HTTPConnection("google.com")
36                 self.prepQuerry = "/complete/search?"
37                 if ds is not None:
38                         self.prepQuerry = self.prepQuerry + "ds=" + ds + "&"
39                 if json is not None:
40                         self.prepQuerry = self.prepQuerry + "json=" + json + "&"
41                 if hl is not None:
42                         self.prepQuerry = self.prepQuerry + "hl=" + hl + "&"
43                 self.prepQuerry = self.prepQuerry + "jsonp=self.gotSuggestions&q="
44
45         def gotSuggestions(self, suggestslist):
46                 self.callback(suggestslist)
47
48         def getSuggestions(self, querryString):
49                 if querryString is not "":
50                         querry = self.prepQuerry + quote(querryString)
51                         try:
52                                 self.conn.request("GET", querry)
53                         except (CannotSendRequest, gaierror, error):
54                                 print "[YTB] Can not send request for suggestions"
55                                 self.callback(None)
56                         else:
57                                 try:
58                                         response = self.conn.getresponse()
59                                 except BadStatusLine:
60                                         print "[YTB] Can not get a response from google"
61                                         self.callback(None)
62                                 else:
63                                         if response.status == 200:
64                                                 data = response.read()
65                                                 exec data
66                                         else:
67                                                 self.callback(None)
68                         self.conn.close()
69                 else:
70                         self.callback(None)
71
72
73 class MyTubeFeedEntry():
74         def __init__(self, feed, entry, favoritesFeed = False):
75                 self.feed = feed
76                 self.entry = entry
77                 self.favoritesFeed = favoritesFeed
78                 self.thumbnail = {}
79                 self.myopener = MyOpener()
80                 urllib.urlopen = MyOpener().open
81                 """if config.plugins.mytube.general.useHTTPProxy.value is True:
82                         proxy = {'http': 'http://'+str(config.plugins.mytube.general.ProxyIP.getText())+':'+str(config.plugins.mytube.general.ProxyPort.value)}
83                         self.myopener = MyOpener(proxies=proxy)
84                         urllib.urlopen = MyOpener(proxies=proxy).open
85                 else:
86                         self.myopener = MyOpener()
87                         urllib.urlopen = MyOpener().open"""
88                 
89         def isPlaylistEntry(self):
90                 return False
91
92         def getTubeId(self):
93                 #print "[MyTubeFeedEntry] getTubeId"
94                 ret = None
95                 if self.entry.media.player:
96                         ret = self.entry.media.player.url.split("=").pop()
97                 return ret
98
99         def getTitle(self):
100                 #print "[MyTubeFeedEntry] getTitle",self.entry.media.title.text
101                 return self.entry.media.title.text
102
103         def getDescription(self):
104                 #print "[MyTubeFeedEntry] getDescription"
105                 if self.entry.media is not None and self.entry.media.description is not None:
106                         return self.entry.media.description.text
107                 return "not vailable"
108
109         def getThumbnailUrl(self, index = 0):
110                 #print "[MyTubeFeedEntry] getThumbnailUrl"
111                 if index < len(self.entry.media.thumbnail):
112                         return self.entry.media.thumbnail[index].url
113                 return None
114
115         def getPublishedDate(self):
116                 if self.entry.published is not None:
117                         return self.entry.published.text
118                 return "unknown"
119
120         def getViews(self):
121                 if self.entry.statistics is not None:
122                         return self.entry.statistics.view_count
123                 return "not available"
124         
125         def getDuration(self):
126                 if self.entry.media is not None and self.entry.media.duration is not None:
127                         return self.entry.media.duration.seconds
128                 else:
129                         return 0
130
131         def getRatingAverage(self):
132                 if self.entry.rating is not None:
133                         return self.entry.rating.average
134                 return 0
135
136
137         def getNumRaters(self):
138                 if self.entry.rating is not None:
139                         return self.entry.rating.num_raters
140                 return ""       
141
142         def getAuthor(self):
143                 authors = []
144                 for author in self.entry.author:
145                         authors.append(author.name.text)
146                 author = ", ".join(authors)
147                 return author
148
149         def PrintEntryDetails(self):
150                 EntryDetails = { 'Title': None, 'TubeID': None, 'Published': None, 'Published': None, 'Description': None, 'Category': None, 'Tags': None, 'Duration': None, 'Views': None, 'Rating': None, 'Thumbnails': None}
151                 EntryDetails['Title'] = self.entry.media.title.text
152                 EntryDetails['TubeID'] = self.getTubeId()
153                 EntryDetails['Description'] = self.getDescription()
154                 EntryDetails['Category'] = self.entry.media.category[0].text
155                 EntryDetails['Tags'] = self.entry.media.keywords.text
156                 EntryDetails['Published'] = self.getPublishedDate()
157                 EntryDetails['Views'] = self.getViews()
158                 EntryDetails['Duration'] = self.getDuration()
159                 EntryDetails['Rating'] = self.getNumRaters()
160                 EntryDetails['RatingAverage'] = self.getRatingAverage()
161                 EntryDetails['Author'] = self.getAuthor()
162                 # show thumbnails
163                 list = []
164                 for thumbnail in self.entry.media.thumbnail:
165                         print 'Thumbnail url: %s' % thumbnail.url
166                         list.append(str(thumbnail.url))
167                 EntryDetails['Thumbnails'] = list
168                 #print EntryDetails
169                 return EntryDetails
170
171
172         def getVideoUrl(self):
173                 mrl = None
174                 isHDAvailable = False
175                 req = "http://www.youtube.com/watch?v=" + str(self.getTubeId())
176                 try:
177                         response = urllib.urlopen(req)
178                         """if config.plugins.mytube.general.useHTTPProxy.value is True:
179                                 proxy = {'http': str(config.plugins.mytube.general.ProxyIP.getText())+':'+str(config.plugins.mytube.general.ProxyPort.value)}
180                                 print "USING PRXY---->",proxy
181                                 response = urllib.urlopen(req,proxies=proxy)
182                         else:
183                                 response = urllib.urlopen(req)"""
184                 except HTTPError, e:
185                         print "[MyTube] The server coundn't fulfill the request."
186                         print "[MyTube] Error code: ", e.code
187                 except URLError, e:
188                         print "[MyTube] We failed to reach a server."
189                         print "[MyTube] Reason: ", e.reason
190                 except IOError, e:
191                         print "[MyTube] We failed to reach a server."
192                         print "[MyTube] Reason: ", e
193                 else:
194                         while not None:
195                                 data = response.readline()
196                                 if data == "":
197                                         break
198
199                                 if "isHDAvailable = true" in data:
200                                         isHDAvailable = True
201                                         print "HD AVAILABLE"
202                                 else:
203                                         pass
204                                 m = re.search("watch_fullscreen\\?(?P<vid_query>.*?)&title=(?P<name>.*)';\n", data)
205                                 if m:
206                                         break
207                         response.close
208                         if m:
209                                 t= re.match (".*[?&]t=([^&]+)", m.group('vid_query')).groups()[0]
210                                 if isHDAvailable is True:
211                                         mrl = "http://www.youtube.com/get_video?video_id=" + quote(self.getTubeId()) + "&t=" + t + "&fmt=22"
212                                         print "[MyTube] GOT HD URL: ", mrl
213                                 else:
214                                         mrl = "http://www.youtube.com/get_video?video_id=" + quote(self.getTubeId()) + "&t=" + t + "&fmt=18"
215                                         print "[MyTube] GOT SD URL: ", mrl
216
217                         else:
218                                 print "[MyTube] No valid mp4-url found"
219                 #self.myopener = MyOpener()
220                 #urllib.urlopen = MyOpener().open
221                 return mrl
222
223         def getRelatedVideos(self):
224                 print "[MyTubeFeedEntry] getResponseVideos()"
225                 for link in self.entry.link:
226                         #print "Related link: ", link.rel.endswith
227                         if link.rel.endswith("video.related"):
228                                 print "Found Related: ", link.href
229                                 return link.href
230
231         def getResponseVideos(self):
232                 print "[MyTubeFeedEntry] getResponseVideos()"
233                 for link in self.entry.link:
234                         #print "Responses link: ", link.rel.endswith
235                         if link.rel.endswith("video.responses"):
236                                 print "Found Responses: ", link.href
237                                 return link.href
238
239 class MyTubePlayerService():
240 #       Do not change the client_id and developer_key in the login-section!
241 #       ClientId: ytapi-dream-MyTubePlayer-i0kqrebg-0
242 #       DeveloperKey: AI39si4AjyvU8GoJGncYzmqMCwelUnqjEMWTFCcUtK-VUzvWygvwPO-sadNwW5tNj9DDCHju3nnJEPvFy4WZZ6hzFYCx8rJ6Mw
243         def __init__(self):
244                 print "[MyTube] MyTubePlayerService - init"
245                 self.feedentries = []
246                 self.feed = None
247                 
248         def startService(self):
249                 print "[MyTube] MyTubePlayerService - startService"
250                 self.yt_service = gdata.youtube.service.YouTubeService()
251                 self.yt_service.developer_key = 'AI39si4AjyvU8GoJGncYzmqMCwelUnqjEMWTFCcUtK-VUzvWygvwPO-sadNwW5tNj9DDCHju3nnJEPvFy4WZZ6hzFYCx8rJ6Mw'
252                 self.yt_service.client_id = 'ytapi-dream-MyTubePlayer-i0kqrebg-0'
253                 self.loggedIn = False
254                 #os.environ['http_proxy'] = 'http://169.229.50.12:3128'
255                 #proxy = os.environ.get('http_proxy')
256                 #print "FOUND ENV PROXY-->",proxy
257                 #for a in os.environ.keys():
258                 #       print a
259
260         def stopService(self):
261                 print "[MyTube] MyTubePlayerService - stopService"
262                 del self.ytService
263                 self.loggedIn = False
264
265         def isLoggedIn(self):
266                 return self.loggedIn
267
268         def getFeed(self, url):
269                 print "[MyTube] MyTubePlayerService - getFeed:",url
270                 self.feedentries = []
271                 self.feed = self.yt_service.GetYouTubeVideoFeed(url)
272                 for entry in self.feed.entry:
273                         MyFeedEntry = MyTubeFeedEntry(self, entry)
274                         self.feedentries.append(MyFeedEntry)
275                 return self.feed                        
276
277         def search(self, searchTerms, startIndex = 1, maxResults = 25,
278                                         orderby = "relevance", racy = "include", 
279                                         author = "", lr = "", categories = "", sortOrder = "ascending"):
280                 print "[MyTube] MyTubePlayerService - search()"
281                 self.feedentries = []
282                 query = gdata.youtube.service.YouTubeVideoQuery()
283                 query.vq = searchTerms
284                 query.orderby = orderby
285                 query.racy = racy
286                 query.sortorder = sortOrder
287                 if lr is not None:
288                         query.lr = lr
289                 if categories[0] is not None:
290                         query.categories = categories
291                 query.start_index = startIndex
292                 query.max_results = maxResults
293                 try:
294                         feed = self.yt_service.YouTubeQuery(query)
295                 except gaierror:
296                         feed = None
297                 if feed is not None:
298                         self.feed = feed
299                         for entry in self.feed.entry:
300                                 MyFeedEntry = MyTubeFeedEntry(self, entry)
301                                 self.feedentries.append(MyFeedEntry)
302                 return self.feed                
303
304         def getTitle(self):
305                 return self.feed.title.text
306
307         def getEntries(self):
308                 return self.feedentries
309
310         def itemCount(self):
311                 return self.feed.items_per_page.text
312
313         def getTotalResults(self):
314                 return self.feed.total_results.text
315         
316         def getNextFeedEntriesURL(self):
317                 for link in self.feed.link:
318                         if link.rel == "next":
319                                 return link.href
320                 return None
321
322
323 myTubeService = MyTubePlayerService()