Skip to content

Commit

Permalink
added as_dict() method to all objects
Browse files Browse the repository at this point in the history
  • Loading branch information
jmolinski committed Jun 27, 2016
1 parent 79f6610 commit 702cde7
Show file tree
Hide file tree
Showing 12 changed files with 129 additions and 2 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ title: str
author: str
views: int
thumbnail: str
as_dict() -> dict
```

---
Expand All @@ -81,6 +82,7 @@ name: str
videos_amount: int
subscriptions: int
thumbnail: str
as_dict() -> dict
```

---
Expand All @@ -98,6 +100,7 @@ thumbnail: str
author: str
first_video_id: str
first_video_url: str
as_dict() -> dict
```

---
Expand All @@ -117,6 +120,7 @@ views: int
thumbnail: str
next_video: VideoSignature
related_videos: Tuple of VideoSignature
as_dict() -> dict
```

---
Expand All @@ -133,6 +137,7 @@ author: str
length: str
thumbnail: str
videos: Tuple of VideoSignature
as_dict() -> dict
```

---
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.0.0',
version='2.1.0',
description='Open YouTube API library',
url='https://github.com/Glenpl/openytapi',
download_url='https://github.com/Glenpl/openytapi/tarball/2.0.0',
download_url='https://github.com/Glenpl/openytapi/tarball/2.1.0',
author='Glenpl/Jakub Molinski',
author_email='kubamolinski@gmail.com',
license='MIT',
Expand Down
11 changes: 11 additions & 0 deletions tests/testchannelsignature.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,14 @@ def test_getters(self):
self.assertEqual(signature.subscriptions, 3555)
self.assertEqual(signature.thumbnail, 'thumb')
self.assertEqual(signature.name, 'name')

def test_as_dict(self):
signature = ChannelSignature('UCAw23LRxypuNn0ilUu1o1Cw', 'name', 11, 3555, 'thumb')
self.assertEqual(signature.as_dict(), {
'id': 'UCAw23LRxypuNn0ilUu1o1Cw',
'name': 'name',
'videos_amount': 11,
'subscriptions': 3555,
'url': 'https://www.youtube.com/channel/UCAw23LRxypuNn0ilUu1o1Cw',
'thumbnail': 'thumb'
})
12 changes: 12 additions & 0 deletions tests/testplaylist.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,15 @@ def test_youtubeplaylist_constructor(self):
def test_real_get_video_invalid_id(self):
self.assertRaises(YoutubeInvalidIdError,
lambda: YoutubeApi(nocache=True).get_playlist('hdsvhbdhdsvhhdsv'))

def test_as_dict(self):
def test_api_get_playlist(self):
html_code = read_in_file('tests/htmls/playlist_sample_source.txt')
playlist = YoutubeApi(http_fetcher=FakeFetcher(html_code), nocache=True).get_playlist(
'PLLUYFDT7vPkqBZQsTGBpGCjIoePETnOxi')

keys_playlist = ['videos', 'id', 'name', 'author', 'length', 'url', 'thumbnail']
self.assertTrue(all(x in playlist.as_dict() for x in keys_playlist))
keys_video = ['id', 'title', 'author', 'length', 'url', 'thumbnail', 'views']
for video in playlist.as_dict()['videos']:
self.assertTrue(all(x in video for x in keys_video))
14 changes: 14 additions & 0 deletions tests/testplaylistsignature.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,17 @@ def test_getters(self):
self.assertEqual(signature.thumbnail, 'thumb')
self.assertEqual(signature.name, 'name')
self.assertEqual(signature.first_video_id, 'nVjsGKrE6E8')

def test_as_dict(self):
signature = PlaylistSignature('PLLUYFDT7vPkqBZQsTGBpGCjIoePETnOxi',
'name', 11, 'author', 'thumb', 'nVjsGKrE6E8')
self.assertEqual(signature.as_dict(), {
'id': 'PLLUYFDT7vPkqBZQsTGBpGCjIoePETnOxi',
'name': 'name',
'author': 'author',
'first_video_url': signature.first_video_url,
'first_video_id': 'nVjsGKrE6E8',
'length': 11,
'url': signature.url,
'thumbnail': signature.thumbnail
})
15 changes: 15 additions & 0 deletions tests/testvideo.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,18 @@ def test_real_get_video_invalid_fetcher(self):
lambda: YoutubeApi(
http_fetcher=ExceptionRaisingFetcher(),
nocache=True).get_video('nVjsGKrE6E8'))

def test_as_dict(self):
html_code = read_in_file('tests/htmls/video_sample_source.txt')
video = YoutubeApi(http_fetcher=FakeFetcher(html_code), nocache=True).get_video('nVjsGKrE6E8')
keys_video = []

keys_video = ['id', 'title', 'author', 'length', 'url', 'thumbnail', 'views',
'length_in_seconds', 'next_video', 'related_videos']
self.assertTrue(all(x in video.as_dict() for x in keys_video))

keys_videosignature = ['id', 'title', 'author', 'length', 'url', 'thumbnail', 'views']
self.assertTrue(all(x in video.as_dict()['next_video'] for x in keys_videosignature))

for video in video.as_dict()['related_videos']:
self.assertTrue(all(x in video for x in keys_videosignature))
12 changes: 12 additions & 0 deletions tests/testvideosignature.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,15 @@ def test_videosignature_constructor(self):
def test_get_video_url(self):
signature = VideoSignature('TdrL3QxjyVw', ' ', ' ', 0, ' ')
self.assertEqual(signature.url, 'https://www.youtube.com/watch?v=TdrL3QxjyVw')

def test_as_dict(self):
signature = VideoSignature('TdrL3QxjyVw', 'a', 'b', 0, 'c')
self.assertEqual(signature.as_dict(), {
'id': 'TdrL3QxjyVw',
'title': 'a',
'author': 'b',
'views': 0,
'length': 'c',
'url': 'https://www.youtube.com/watch?v=TdrL3QxjyVw',
'thumbnail': 'https://i.ytimg.com/vi/TdrL3QxjyVw/mqdefault.jpg'
})
10 changes: 10 additions & 0 deletions youtube/channelsignature.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,13 @@ def url(self):
return 'https://www.youtube.com/channel/' + self.channel_id
else:
return 'https://www.youtube.com/user/' + self.channel_id

def as_dict(self):
return {
'id': self.channel_id,
'name': self.name,
'videos_amount': self.videos_amount,
'subscriptions': self.subscriptions,
'url': self.url,
'thumbnail': self.thumbnail
}
11 changes: 11 additions & 0 deletions youtube/playlist.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,14 @@ def url(self):
@property
def videos(self):
return self._videos[:]

def as_dict(self):
return {
'id': self.playlist_id,
'name': self.name,
'author': self.author,
'length': self.length,
'url': self.url,
'thumbnail': self.thumbnail,
'videos': [video.as_dict() for video in self.videos]
}
12 changes: 12 additions & 0 deletions youtube/playlistsignature.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,15 @@ def url(self):
@property
def first_video_url(self):
return 'https://www.youtube.com/watch?v={0}&list={1}'.format(self.first_video_id, self.playlist_id)

def as_dict(self):
return {
'id': self.playlist_id,
'name': self.name,
'author': self.author,
'first_video_url': self.first_video_url,
'first_video_id': self.first_video_id,
'length': self.length,
'url': self.url,
'thumbnail': self.thumbnail
}
14 changes: 14 additions & 0 deletions youtube/video.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,3 +60,17 @@ def related_videos(self):
@property
def thumbnail(self):
return self._signature.thumbnail

def as_dict(self):
return {
'id': self.video_id,
'title': self.title,
'author': self.author,
'views': self.views,
'length': self.length,
'length_in_seconds': self.length_in_seconds,
'url': self.url,
'thumbnail': self.thumbnail,
'next_video': self.next_video.as_dict(),
'related_videos': [video.as_dict() for video in self.related_videos]
}
11 changes: 11 additions & 0 deletions youtube/videosignature.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,14 @@ def url(self):
@property
def thumbnail(self):
return "https://i.ytimg.com/vi/{}/mqdefault.jpg".format(self.video_id)

def as_dict(self):
return {
'id': self.video_id,
'title': self.title,
'author': self.author,
'views': self.views,
'length': self.length,
'url': self.url,
'thumbnail': self.thumbnail
}

0 comments on commit 702cde7

Please sign in to comment.