1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
|
from collections import defaultdict
import mock
from searx.engines import youtube
from searx.testing import SearxTestCase
class TestYoutubeEngine(SearxTestCase):
def test_request(self):
query = 'test_query'
dicto = defaultdict(dict)
dicto['pageno'] = 0
dicto['language'] = 'fr_FR'
params = youtube.request(query, dicto)
self.assertTrue('url' in params)
self.assertTrue(query in params['url'])
self.assertTrue('youtube.com' in params['url'])
self.assertTrue('fr' in params['url'])
dicto['language'] = 'all'
params = youtube.request(query, dicto)
self.assertFalse('fr' in params['url'])
def test_response(self):
self.assertRaises(AttributeError, youtube.response, None)
self.assertRaises(AttributeError, youtube.response, [])
self.assertRaises(AttributeError, youtube.response, '')
self.assertRaises(AttributeError, youtube.response, '[]')
response = mock.Mock(text='{}')
self.assertEqual(youtube.response(response), [])
response = mock.Mock(text='{"data": []}')
self.assertEqual(youtube.response(response), [])
json = """
{"feed":{"entry":[{
"id":{"$t":"http://gdata.youtube.com/feeds/api/videos/DIVZCPfAOeM"},
"published":{"$t":"2015-01-23T21:25:00.000Z"},
"updated":{"$t":"2015-01-26T14:38:15.000Z"},
"title":{"$t":"Title",
"type":"text"},"content":{"$t":"Description","type":"text"},
"link":[{"rel":"alternate","type":"text/html",
"href":"https://www.youtube.com/watch?v=DIVZCPfAOeM&feature=youtube_gdata"},
{"rel":"http://gdata.youtube.com/schemas/2007#video.related",
"type":"application/atom+xml",
"href":"https://gdata.youtube.com/feeds/api/videos/DIVZCPfAOeM/related"},
{"rel":"http://gdata.youtube.com/schemas/2007#mobile","type":"text/html",
"href":"https://m.youtube.com/details?v=DIVZCPfAOeM"},
{"rel":"self","type":"application/atom+xml",
"href":"https://gdata.youtube.com/feeds/api/videos/DIVZCPfAOeM"}],
"author":[{"name":{"$t":"Cauet"},
"uri":{"$t":"https://gdata.youtube.com/feeds/api/users/cauetofficiel"} }],
"gd$comments":{"gd$feedLink":{"rel":"http://gdata.youtube.com/schemas/2007#comments",
"href":"https://gdata.youtube.com/feeds/api/videos/DIVZCPfAOeM/comments",
"countHint":8} },
"media$group":{"media$category":[{"$t":"Comedy","label":"Comedy",
"scheme":"http://gdata.youtube.com/schemas/2007/categories.cat"}],
"media$content":[{"url":"https://www.youtube.com/v/DIVZCPfAOeM?version=3&f=videos&app=youtube_gdata",
"type":"application/x-shockwave-flash","medium":"video",
"isDefault":"true","expression":"full","duration":354,"yt$format":5},
{"url":"rtsp://r1---sn-cg07luel.c.youtube.com/CiILENy73wIaGQnjOcD3CFmFDBMYDSANFEgGUgZ2aWRlb3MM/0/0/0/video.3gp",
"type":"video/3gpp","medium":"video","expression":"full","duration":354,
"yt$format":1},
{"url":"rtsp://r1---sn-cg07luel.c.youtube.com/CiILENy73wIaGQnjOcD3CFmFDBMYESARFEgGUgZ2aWRlb3MM/0/0/0/video.3gp",
"type":"video/3gpp","medium":"video","expression":"full","duration":354,"yt$format":6}],
"media$description":{"$t":"Desc","type":"plain"},
"media$keywords":{},
"media$player":[{"url":"https://www.youtube.com/watch?v=DIVZCPfAOeM&feature=youtube_gdata_player"}],
"media$thumbnail":[{"url":"https://i.ytimg.com/vi/DIVZCPfAOeM/0.jpg",
"height":360,"width":480,"time":"00:02:57"},
{"url":"https://i.ytimg.com/vi/DIVZCPfAOeM/1.jpg","height":90,"width":120,"time":"00:01:28.500"},
{"url":"https://i.ytimg.com/vi/DIVZCPfAOeM/2.jpg","height":90,"width":120,"time":"00:02:57"},
{"url":"https://i.ytimg.com/vi/DIVZCPfAOeM/3.jpg","height":90,"width":120,"time":"00:04:25.500"}],
"media$title":{"$t":"Title","type":"plain"},
"yt$duration":{"seconds":"354"} },
"gd$rating":{"average":4.932159,"max":5,"min":1,"numRaters":1533,
"rel":"http://schemas.google.com/g/2005#overall"},
"yt$statistics":{"favoriteCount":"0","viewCount":"92464"} }
]
}
}
"""
response = mock.Mock(text=json)
results = youtube.response(response)
self.assertEqual(type(results), list)
self.assertEqual(len(results), 1)
self.assertEqual(results[0]['title'], 'Title')
self.assertEqual(results[0]['url'], 'https://www.youtube.com/watch?v=DIVZCPfAOeM')
self.assertEqual(results[0]['content'], 'Description')
self.assertEqual(results[0]['thumbnail'], 'https://i.ytimg.com/vi/DIVZCPfAOeM/0.jpg')
self.assertTrue('DIVZCPfAOeM' in results[0]['embedded'])
json = """
{"feed":{"entry":[{
"id":{"$t":"http://gdata.youtube.com/feeds/api/videos/DIVZCPfAOeM"},
"published":{"$t":"2015-01-23T21:25:00.000Z"},
"updated":{"$t":"2015-01-26T14:38:15.000Z"},
"title":{"$t":"Title",
"type":"text"},"content":{"$t":"Description","type":"text"},
"link":[{"rel":"http://gdata.youtube.com/schemas/2007#video.related",
"type":"application/atom+xml",
"href":"https://gdata.youtube.com/feeds/api/videos/DIVZCPfAOeM/related"},
{"rel":"self","type":"application/atom+xml",
"href":"https://gdata.youtube.com/feeds/api/videos/DIVZCPfAOeM"}],
"author":[{"name":{"$t":"Cauet"},
"uri":{"$t":"https://gdata.youtube.com/feeds/api/users/cauetofficiel"} }],
"gd$comments":{"gd$feedLink":{"rel":"http://gdata.youtube.com/schemas/2007#comments",
"href":"https://gdata.youtube.com/feeds/api/videos/DIVZCPfAOeM/comments",
"countHint":8} },
"media$group":{"media$category":[{"$t":"Comedy","label":"Comedy",
"scheme":"http://gdata.youtube.com/schemas/2007/categories.cat"}],
"media$content":[{"url":"https://www.youtube.com/v/DIVZCPfAOeM?version=3&f=videos&app=youtube_gdata",
"type":"application/x-shockwave-flash","medium":"video",
"isDefault":"true","expression":"full","duration":354,"yt$format":5},
{"url":"rtsp://r1---sn-cg07luel.c.youtube.com/CiILENy73wIaGQnjOcD3CFmFDBMYDSANFEgGUgZ2aWRlb3MM/0/0/0/video.3gp",
"type":"video/3gpp","medium":"video","expression":"full","duration":354,
"yt$format":1},
{"url":"rtsp://r1---sn-cg07luel.c.youtube.com/CiILENy73wIaGQnjOcD3CFmFDBMYESARFEgGUgZ2aWRlb3MM/0/0/0/video.3gp",
"type":"video/3gpp","medium":"video","expression":"full","duration":354,"yt$format":6}],
"media$description":{"$t":"Desc","type":"plain"},
"media$keywords":{},
"media$player":[{"url":"https://www.youtube.com/watch?v=DIVZCPfAOeM&feature=youtube_gdata_player"}],
"media$thumbnail":[{"url":"https://i.ytimg.com/vi/DIVZCPfAOeM/0.jpg",
"height":360,"width":480,"time":"00:02:57"},
{"url":"https://i.ytimg.com/vi/DIVZCPfAOeM/1.jpg","height":90,"width":120,"time":"00:01:28.500"},
{"url":"https://i.ytimg.com/vi/DIVZCPfAOeM/2.jpg","height":90,"width":120,"time":"00:02:57"},
{"url":"https://i.ytimg.com/vi/DIVZCPfAOeM/3.jpg","height":90,"width":120,"time":"00:04:25.500"}],
"media$title":{"$t":"Title","type":"plain"},
"yt$duration":{"seconds":"354"} },
"gd$rating":{"average":4.932159,"max":5,"min":1,"numRaters":1533,
"rel":"http://schemas.google.com/g/2005#overall"},
"yt$statistics":{"favoriteCount":"0","viewCount":"92464"} }
]
}
}
"""
response = mock.Mock(text=json)
results = youtube.response(response)
self.assertEqual(type(results), list)
self.assertEqual(len(results), 0)
json = """
{"feed":{"entry":[{
"id":{"$t":"http://gdata.youtube.com/feeds/api/videos/DIVZCPfAOeM"},
"published":{"$t":"2015-01-23T21:25:00.000Z"},
"updated":{"$t":"2015-01-26T14:38:15.000Z"},
"title":{"$t":"Title",
"type":"text"},"content":{"$t":"Description","type":"text"},
"link":[{"rel":"alternate","type":"text/html",
"href":"https://www.youtube.com/watch?v=DIVZCPfAOeM"},
{"rel":"http://gdata.youtube.com/schemas/2007#video.related",
"type":"application/atom+xml",
"href":"https://gdata.youtube.com/feeds/api/videos/DIVZCPfAOeM/related"},
{"rel":"http://gdata.youtube.com/schemas/2007#mobile","type":"text/html",
"href":"https://m.youtube.com/details?v=DIVZCPfAOeM"},
{"rel":"self","type":"application/atom+xml",
"href":"https://gdata.youtube.com/feeds/api/videos/DIVZCPfAOeM"}],
"author":[{"name":{"$t":"Cauet"},
"uri":{"$t":"https://gdata.youtube.com/feeds/api/users/cauetofficiel"} }],
"gd$comments":{"gd$feedLink":{"rel":"http://gdata.youtube.com/schemas/2007#comments",
"href":"https://gdata.youtube.com/feeds/api/videos/DIVZCPfAOeM/comments",
"countHint":8} },
"media$group":{"media$category":[{"$t":"Comedy","label":"Comedy",
"scheme":"http://gdata.youtube.com/schemas/2007/categories.cat"}],
"media$content":[{"url":"https://www.youtube.com/v/DIVZCPfAOeM?version=3&f=videos&app=youtube_gdata",
"type":"application/x-shockwave-flash","medium":"video",
"isDefault":"true","expression":"full","duration":354,"yt$format":5},
{"url":"rtsp://r1---sn-cg07luel.c.youtube.com/CiILENy73wIaGQnjOcD3CFmFDBMYDSANFEgGUgZ2aWRlb3MM/0/0/0/video.3gp",
"type":"video/3gpp","medium":"video","expression":"full","duration":354,
"yt$format":1},
{"url":"rtsp://r1---sn-cg07luel.c.youtube.com/CiILENy73wIaGQnjOcD3CFmFDBMYESARFEgGUgZ2aWRlb3MM/0/0/0/video.3gp",
"type":"video/3gpp","medium":"video","expression":"full","duration":354,"yt$format":6}],
"media$description":{"$t":"Desc","type":"plain"},
"media$keywords":{},
"media$player":[{"url":"https://www.youtube.com/watch?v=DIVZCPfAOeM&feature=youtube_gdata_player"}],
"media$title":{"$t":"Title","type":"plain"},
"yt$duration":{"seconds":"354"} },
"gd$rating":{"average":4.932159,"max":5,"min":1,"numRaters":1533,
"rel":"http://schemas.google.com/g/2005#overall"},
"yt$statistics":{"favoriteCount":"0","viewCount":"92464"} }
]
}
}
"""
response = mock.Mock(text=json)
results = youtube.response(response)
self.assertEqual(type(results), list)
self.assertEqual(len(results), 1)
self.assertEqual(results[0]['title'], 'Title')
self.assertEqual(results[0]['url'], 'https://www.youtube.com/watch?v=DIVZCPfAOeM')
self.assertEqual(results[0]['content'], 'Description')
self.assertEqual(results[0]['thumbnail'], '')
self.assertTrue('DIVZCPfAOeM' in results[0]['embedded'])
json = """
{"toto":{"entry":[]
}
}
"""
response = mock.Mock(text=json)
results = youtube.response(response)
self.assertEqual(type(results), list)
self.assertEqual(len(results), 0)
|