move sys.argv hack to tubelib/youtube
[enigma2-plugins.git] / tubelib / src / youtube / Search.py
1 #enigma2
2 from Components.config import config
3 #youtube
4 from apiclient.discovery import build
5 #local
6 from ThreadedRequest import ThreadedRequest
7 from YoutubeQueryBase import YoutubeQueryBase
8 from Videos import Videos
9
10 from Tools.Log import Log
11
12 class Search(Videos):
13         ORDER_DATE = "date"
14         ORDER_RATING = "rating"
15         ORDER_RELEVANCE = "relevance"
16         ORDER_TITLE = "title"
17         ORDER_VIDEOCOUNT = "videoCount"
18         ORDER_VIEWCOUNT = "viewCount"
19
20         SAFE_SEARCH_NONE = "none"
21         SAFE_SEARCH_MODERATE = "moderate"
22         SAFE_SEARCH_STRICT = "strict"
23
24         def list(self, callback, searchTerm=None, order=ORDER_RELEVANCE, maxResults=25, relatedToVideoId=None, channelId=None, safeSearch=SAFE_SEARCH_NONE):
25                 self._listrequest = None
26                 self._args = {
27                         'part' : 'id',
28                         'type' : 'video',
29                         'maxResults' : maxResults,
30                         'safeSearch' : safeSearch
31                 }
32                 if searchTerm:
33                         self._args['q'] = searchTerm
34                 if relatedToVideoId:
35                         self._args['relatedToVideoId'] = relatedToVideoId
36                 if channelId:
37                         self._args['channelId'] = channelId
38                 self._doQuery(callback)
39
40         def _doQuery(self, callback):
41                 request = self._youtube.search().list(**self._args)
42                 def subquery(data):
43                         if not data:
44                                 self._onResult(False, [])
45                         items = []
46                         for item in data['items']:
47                                 items.append(item["id"]["videoId"])
48                         kwargs = {
49                                 "part" : "id,snippet,statistics,contentDetails",
50                                 "maxResults" : 50,
51                                 "hl" : config.plugins.mytube.search.lr.value,
52                                 "id" : ",".join(items)
53                         }
54                         request = self._youtube.videos().list(**kwargs)
55                         return request
56                 return self._query(callback, request, subquery=subquery)