MyTube: Fix sorting of search results
[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                         'order' : order,
31                         'safeSearch' : safeSearch
32                 }
33                 if searchTerm:
34                         self._args['q'] = searchTerm
35                 if relatedToVideoId:
36                         self._args['relatedToVideoId'] = relatedToVideoId
37                 if channelId:
38                         self._args['channelId'] = channelId
39                 self._doQuery(callback)
40
41         def _doQuery(self, callback):
42                 request = self._youtube.search().list(**self._args)
43                 def subquery(data):
44                         if not data:
45                                 self._onResult(False, [])
46                         items = []
47                         for item in data['items']:
48                                 items.append(item["id"]["videoId"])
49                         kwargs = {
50                                 "part" : "id,snippet,statistics,contentDetails",
51                                 "maxResults" : 50,
52                                 "hl" : config.plugins.mytube.search.lr.value,
53                                 "id" : ",".join(items)
54                         }
55                         request = self._youtube.videos().list(**kwargs)
56                         return request
57                 return self._query(callback, request, subquery=subquery)