Skip to content

Commit

Permalink
changed mixed_search method output format
Browse files Browse the repository at this point in the history
  • Loading branch information
jmolinski committed Jun 30, 2016
1 parent 702cde7 commit 055c40d
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 23 deletions.
11 changes: 10 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ The main API class is YoutubeApi, which consists of 7 methods:
```
constructor(http_fetcher=None, nocache=False, global_cache=False)
search(search_string: str) -> Tuple of VideoSignature, ChannelSignature and PlaylistSignature
search(search_string: str) -> Dict of VideoSignature, ChannelSignature and PlaylistSignature
search_videos(search_string: str) -> Tuple of VideoSignature
search_channels(search_string: str) -> Tuple of ChannelSignature
search_playlists(search_string: str) -> Tuple of PlaylistSignature
Expand All @@ -46,6 +46,15 @@ Object passed as http_fetcher is used to fetch page source - it has to implement
class Fetcher():
def fetch_page(self, url): pass # returns utf-8 decoded page source
```

YoutubeApi.search returns result in such format:
```python
{
'videos': Tuple of found VideoSignature,
'playlists': Tuple of found PlaylistSignature,
'channels': Tuple of found ChannelSignature
}
```
It's probably best to just let the YoutubeApi use it's default http_fetcher - the one that works just fine.
Important note: if you want to supply your own http_fetcher which doesn't make real http calls, it's best to set nocache to True.
If nocache is set to True no cache is used at all. If nocache is set to False or omitted cache is used.
Expand Down
4 changes: 2 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@

setup(
name='openytapi',
version='2.1.0',
version='2.2.0',
description='Open YouTube API library',
url='https://github.com/Glenpl/openytapi',
download_url='https://github.com/Glenpl/openytapi/tarball/2.1.0',
download_url='https://github.com/Glenpl/openytapi/tarball/2.2.0',
author='Glenpl/Jakub Molinski',
author_email='kubamolinski@gmail.com',
license='MIT',
Expand Down
33 changes: 16 additions & 17 deletions tests/testmixedsearch.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,36 +10,35 @@ class YoutubeApiVideoSearchTest(unittest.TestCase):
def test_api_search_no_results(self):
html_code = read_in_file('tests/htmls/search_no_results.txt')
found_items = YoutubeApi(http_fetcher=FakeFetcher(html_code), nocache=True).search('')
self.assertEqual(len(found_items), 0)
length = len(found_items['videos'] + found_items['playlists'] + found_items['channels'])
self.assertEqual(length, 0)

def test_api_search(self):
html_code = read_in_file('tests/htmls/search_mixed_17_results.txt')
found_items = YoutubeApi(http_fetcher=FakeFetcher(html_code), nocache=True).search('')
self.assertEqual(len(found_items), 20)
videos = [item for item in found_items if isinstance(item, VideoSignature)]
playlists = [item for item in found_items if isinstance(item, PlaylistSignature)]
channels = [item for item in found_items if isinstance(item, ChannelSignature)]
self.assertEqual(len(videos), 17)
self.assertEqual(len(playlists), 1)
self.assertEqual(len(channels), 2)
length = len(found_items['videos'] + found_items['playlists'] + found_items['channels'])
self.assertEqual(length, 20)
self.assertEqual(len(found_items['videos']), 17)
self.assertEqual(len(found_items['playlists']), 1)
self.assertEqual(len(found_items['channels']), 2)

def test_real_search(self):
found_items = YoutubeApi(nocache=True).search('lana del rey')
self.assertEqual(len(found_items), 20)
videos = [item for item in found_items if isinstance(item, VideoSignature)]
playlists = [item for item in found_items if isinstance(item, PlaylistSignature)]
channels = [item for item in found_items if isinstance(item, ChannelSignature)]
self.assertTrue(len(videos) >= 15)
self.assertTrue(len(playlists) >= 1)
self.assertTrue(len(channels) >= 1)
length = len(found_items['videos'] + found_items['playlists'] + found_items['channels'])
self.assertEqual(length, 20)
self.assertTrue(len(found_items['videos']) >= 15)
self.assertTrue(len(found_items['playlists']) >= 1)
self.assertTrue(len(found_items['channels']) >= 1)

def test_real_search_no_results(self):
found_results = YoutubeApi(nocache=True).search('dbg76i6bncw6ogefnxbwegfbnl')
self.assertEqual(len(found_results), 0)
length = len(found_results['videos'] + found_results['playlists'] + found_results['channels'])
self.assertEqual(length, 0)

def test_real_search_multiple_results(self):
found_results = YoutubeApi(nocache=True).search('pink floyd')
self.assertEqual(len(found_results), 20)
length = len(found_results['videos'] + found_results['playlists'] + found_results['channels'])
self.assertTrue(length >= 20)

def test_real_search_invalid_url(self):
with self.assertRaises(YoutubeApiConnectionError):
Expand Down
8 changes: 5 additions & 3 deletions youtube/mixedsearch.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ class MixedSearch(BaseSearch):

def search(self, search_string):
page_source = self._get_page_content(self._make_search_url(search_string))
return (ChannelSearchParser().parse(page_source) +
PlaylistSearchParser().parse(page_source) +
VideoSearchParser().parse(page_source))
return {
'videos': VideoSearchParser().parse(page_source),
'playlists': PlaylistSearchParser().parse(page_source),
'channels': ChannelSearchParser().parse(page_source),
}

0 comments on commit 055c40d

Please sign in to comment.