def insert_actors_info(): """ Check if actor has username. If so saves id and username on youtube.csv. Returns: YoutubeAPI object if successful, None otherwise. """ yt_api = YoutubeAPI() yt_api.generate_folder() check = yt_api.generate_csv(clean=True) if check: with open('config/actors.json') as json_file: data = json.load(json_file) for item in data['channels']: if 'username' in item: if item['username']: result = yt_api.get_channel_info_by_username( item['username']) item['id'] = result['items'][0]['id'] yt_api.insert_value(column='channel_id', value=item['id'], search_cell='actor', search_value=item['actor'].replace( '\n', '')) yt_api.insert_value(column='username', value=item['username'], search_cell='actor', search_value=item['actor'].replace( '\n', '')) return yt_api return None
def test_insert_multiple_values_of_an_actor(self): yt_api = YoutubeAPI() insert_actors_info() result = yt_api.insert_multiple_values(column=['channel_id'], search_cell='actor', search_value='Frente Brasil' + ' Popular') result = yt_api.get_row(column='actor', value='Frente Brasil Popular') self.assertEqual(result, {'username': '', 'channel_id': 'null'})
def test_insert_value_of_an_actor(self): yt_api = YoutubeAPI() insert_actors_info() result = yt_api.insert_value(column='channel_id', value='UCX2Aanu4fGewmhP4rf5GQ3Q', search_cell='actor', search_value='Frente Brasil Popular') result = yt_api.get_row(column='actor', value='Frente Brasil Popular') self.assertEqual(result, { 'username': '', 'channel_id': 'UCX2Aanu4fGewmhP4rf5GQ3Q' }) self.clean_csv(yt_api) self.clean_folder(yt_api)
def __init__(self): with open('config/parameters.json') as data_file: parameters = json.load(data_file)['parameters'] self.user = YoutubeAPI() self._activities = { 'part': 'snippet,contentDetails', 'channelId': '', 'maxResults': '', 'publishedAfter': parameters['initial_date'], 'key': self.user._youtube_key } self._activities_extent = { 'part': 'snippet,contentDetails', 'channelId': '', 'maxResults': '', 'publishedAfter': '2018-01-01T00:00:01.45-03:00', 'publishedBefore': '', 'key': self.user._youtube_key } self._videos = { 'part': 'snippet,statistics,status,contentDetails', 'id': '', 'maxResults': '', 'key': self.user._youtube_key } self._search = { 'part': 'id,snippet', 'maxResults': '', 'type': '', 'relatedToVideoId': '', 'key': self.user._youtube_key } self._category = { 'part': 'id,snippet', 'hl': 'pt_BR', 'id': '', 'key': self.user._youtube_key }
def test_clean_csv(self): yt_api = YoutubeAPI() self.assertTrue(yt_api.generate_csv(clean=True)) self.insert_value(yt_api) result = yt_api.get_row(column='actor', value='Frente Brasil Popular') self.assertEqual(result, { 'username': '', 'channel_id': 'UCX2Aanu4fGewmhP4rf5GQ3Q' }) self.assertTrue(yt_api.generate_csv(clean=True)) result = yt_api.get_row(column='actor', value='Frente Brasil Popular') self.assertEqual(result, {'username': '', 'channel_id': ''})
def setUp(self): self._video = Videos() self._user = YoutubeAPI()
class TestVideos(unittest.TestCase): def setUp(self): self._video = Videos() self._user = YoutubeAPI() def test_has_activities(self): channel_id = 'UCj34AOIMl_k1fF7hcBkD_dw' max_results = '5' result = self._video.get_activity_info(channel_id, max_results) self.assertTrue(result['items']) def test_category_info(self): id = '28' result = self._video.get_category_info(id) self.assertEqual(result['items'][0]['snippet']['title'], 'Ciência e tecnologia') def test_has_search(self): related_to_video_id = 'Xx7bjU7gabM' max_results = '5' type = 'video' result = self._video.get_search_info(max_results, related_to_video_id, type) self.assertTrue(result['items']) def test_has_video(self): channel_id = 'V6OvM-0SGUU' max_results = '5' result = self._video.get_videos_info(channel_id, max_results) self.assertTrue(result['items']) def test_all_video_ids(self): user_id = 'UC9uefWa6TXIPDRWGZYMcTuA' max_results = '7' result = self._user.get_channel_info(user_id) channel_id = self._user.get_channel_id(result) result_activities = self._video.get_activity_info( channel_id, max_results) video_ids = self._video.get_all_video_ids(result_activities) assert_list = ['FvNsjk62Xi8'] self.assertEqual(video_ids[-1:], assert_list) def test_all_videos_count_user_id(self): user_id = 'UCsCI7wlAwbzTPK55yVaX2Ig' result = self._user.get_channel_info(user_id) max_results = 50 video_views = self._video.get_all_video_views_user_id( result, max_results, 0) video_title = video_views[-1]['title'] self.assertEqual(video_title, 'Encontro Presencial - IAEaD 2018') video_view = int(video_views[-1]['views']) video_url = str(video_views[-1]['url']) video_likes = str(video_views[-1]['likes']) video_dislikes = str(video_views[-1]['dislikes']) video_comments = str(video_views[-1]['comments']) video_favorites = int(video_views[-1]['favorites']) self.assertGreater(video_view, 16) self.assertLess(video_view, 100) self.assertEqual(video_url, 'https://www.youtube.com/watch?v=B-5VHSKIuHw') self.assertEqual(video_likes, '1') self.assertEqual(video_dislikes, '0') self.assertEqual(video_comments, '0') self.assertGreaterEqual(video_favorites, 0)
class Videos: """Gets all video's informations. Following certain date gets video's id, views and title. """ def __init__(self): with open('config/parameters.json') as data_file: parameters = json.load(data_file)['parameters'] self.user = YoutubeAPI() self._activities = { 'part': 'snippet,contentDetails', 'channelId': '', 'maxResults': '', 'publishedAfter': parameters['initial_date'], 'key': self.user._youtube_key } self._activities_extent = { 'part': 'snippet,contentDetails', 'channelId': '', 'maxResults': '', 'publishedAfter': '2018-01-01T00:00:01.45-03:00', 'publishedBefore': '', 'key': self.user._youtube_key } self._videos = { 'part': 'snippet,statistics,status,contentDetails', 'id': '', 'maxResults': '', 'key': self.user._youtube_key } self._search = { 'part': 'id,snippet', 'maxResults': '', 'type': '', 'relatedToVideoId': '', 'key': self.user._youtube_key } self._category = { 'part': 'id,snippet', 'hl': 'pt_BR', 'id': '', 'key': self.user._youtube_key } def get_category_info(self, id): self._category['id'] = id return requests.get(CATEGORY_URL, params=self._category).json() def get_search_info(self, max_results, related_to_video_id, type): self._search['maxResults'] = max_results self._search['relatedToVideoId'] = related_to_video_id self._search['type'] = type return requests.get(SEARCH_URL, params=self._search).json() def get_activity_info(self, channel_id, max_results): self._activities['maxResults'] = max_results self._activities['channelId'] = channel_id self._activities_extent['maxResults'] = max_results self._activities_extent['channelId'] = channel_id request = requests.get(ACTIVITIES_URL, params=self._activities).json() if ('items' in request): length = len(request['items']) while (length >= 50): last_date = request['items'][-1]['snippet']['publishedAt'] self._activities_extent['publishedBefore'] = last_date request_extent = requests.get(ACTIVITIES_URL, params=self._activities_extent) request_extent = request_extent.json() if ('items' in request_extent): length = len(request_extent['items']) request['items'].extend(request_extent['items']) else: length = 0 self._activities_extent['publishedBefore'] = '' return request def get_videos_info(self, video_id, max_results): self._videos['maxResults'] = max_results self._videos['id'] = video_id return requests.get(VIDEOS_URL, params=self._videos).json() def get_all_video_ids(self, response): video_ids = [] for video in response['items']: try: video_ids.append(video['contentDetails']['upload']['videoId']) except KeyError: pass return video_ids def get_all_video_items(self, response, max_results, related_videos_max_results, get_related_videos): videos_dic = [] for item in response: views = self.get_videos_info(item, max_results) if get_related_videos: search = self.get_search_info(related_videos_max_results, item, 'video') id = views['items'][0]['id'] channel_id = views['items'][0]['snippet']['channelId'] if 'viewCount' in views['items'][0]['statistics']: video_views = views['items'][0]['statistics']['viewCount'] else: video_views = 'disabled' if 'likeCount' in views['items'][0]['statistics']: video_likes = views['items'][0]['statistics']['likeCount'] else: video_likes = 'disabled' if 'dislikeCount' in views['items'][0]['statistics']: video_dislikes = \ views['items'][0]['statistics']['dislikeCount'] else: video_dislikes = 'disabled' if 'commentCount' in views['items'][0]['statistics']: video_comments = \ views['items'][0]['statistics']['commentCount'] else: video_comments = 'disabled' if 'favoriteCount' in views['items'][0]['statistics']: video_favorites = \ views['items'][0]['statistics']['favoriteCount'] else: video_favorites = 'disabled' if 'publishedAt' in views['items'][0]['snippet']: video_published_date = \ views['items'][0]['snippet']['publishedAt'] else: video_published_date = 'disabled' if 'description' in views['items'][0]['snippet']: video_description = \ views['items'][0]['snippet']['description'] else: video_description = 'disabled' if 'tags' in views['items'][0]['snippet']: video_tags = \ views['items'][0]['snippet']['tags'] else: video_tags = 'disabled' if 'embeddable' in views['items'][0]['status']: video_embeddable = \ views['items'][0]['status']['embeddable'] else: video_embeddable = 'disabled' if 'duration' in views['items'][0]['contentDetails']: video_duration = \ views['items'][0]['contentDetails']['duration'] else: video_duration = 'disabled' if 'thumbnails' in views['items'][0]['snippet']: video_thumbnail = \ views['items'][0]['snippet']['thumbnails']['high']['url'] else: video_thumbnail = 'disabled' if 'categoryId' in views['items'][0]['snippet']: category_id = views['items'][0]['snippet']['categoryId'] category_info = self.get_category_info(category_id) video_category = category_info['items'][0]['snippet']['title'] else: video_category = 'disabled' video_titles = views['items'][0]['snippet']['title'] video_url = VIDEOS_BASE_URL + views['items'][0]['id'] if get_related_videos: if search['items']: related_to_video = [] for video in search['items']: related_to_video.append( self.get_all_video_items([video['id']['videoId']], 1, 0, False)[0]) related_to_video[-1]['original_id'] = item else: related_to_video = 'disabled' videos_dic.append({ 'video_id': id, 'channel_id': channel_id, 'title': video_titles, 'views': video_views, 'likes': video_likes, 'dislikes': video_dislikes, 'comments': video_comments, 'favorites': video_favorites, 'url': video_url, 'publishedAt': video_published_date, 'description': video_description, 'tags': video_tags, 'embeddable': video_embeddable, 'duration': video_duration, 'thumbnail': video_thumbnail, 'category': video_category }) if get_related_videos: videos_dic[-1]['related_to_video'] = related_to_video return videos_dic def get_all_video_views_user_id(self, response, max_results, related_videos_max_results): channel_id = self.user.get_channel_id(response) result_activities = self.get_activity_info(channel_id, max_results) videos_id = self.get_all_video_ids(result_activities) video_views = self.get_all_video_items(videos_id, max_results, related_videos_max_results, True) return video_views
def test_check_data_of_an_actor(self): yt_api = insert_actors_info() result = YoutubeAPI().get_row(column='actor', value='Bancada Ativista') self.assertEqual(result, {'username': '', 'channel_id': ''})
def test_generate_youtube_folder(self): YoutubeAPI().generate_folder() self.assertTrue(os.path.exists(self._foldername))
def test_generate_youtube_csv(self): csv = YoutubeAPI().generate_csv() self.assertTrue(os.path.isfile(self._filename))