def list_actor_related_videos(date, actor, video): raise_date_error, raise_actor_error, raise_video_error = True, True, True db_date = check_date(date) raise_date_error = db_date is None if raise_date_error: status_code = 450 raise InvalidUsage(get_error_message(status_code), status_code=status_code) actor = actor.replace('_', ' ') result_actor = DBYouTube.get_info_actor(db_date, actor) raise_actor_error = result_actor is None if raise_actor_error: status_code = 460 raise InvalidUsage(get_error_message(status_code), status_code=status_code) result = DBYouTube.get_video_by_actor(db_date, result_actor['channel_id'], video) raise_video_error = result is None if raise_video_error: status_code = 470 raise InvalidUsage(get_error_message(status_code), status_code=status_code) return jsonify(result)
def check_date(date): all_dates = DBYouTube.get_dates()['dates'] if date == 'latest': return all_dates[0] else: for item in all_dates: if date == item: return date return None
def list_all_videos(date): raise_date_error = True db_date = check_date(date) raise_date_error = db_date is None if raise_date_error: status_code = 450 raise InvalidUsage(get_error_message(status_code), status_code=status_code) result = DBYouTube.get_all_videos(db_date) return jsonify(result)
def list_actor_videos_info(date, actor): raise_date_error, raise_actor_error = True, True db_date = check_date(date) raise_date_error = db_date is None if raise_date_error: status_code = 450 raise InvalidUsage(get_error_message(status_code), status_code=status_code) actor = actor.replace('_', ' ') result_actor = DBYouTube.get_info_actor(db_date, actor) raise_actor_error = result_actor is None if raise_actor_error: status_code = 460 raise InvalidUsage(get_error_message(status_code), status_code=status_code) all_videos = DBYouTube.get_actor_videos(db_date, result_actor['channel_id']) return jsonify({'videos': all_videos})
def setUp(self): app.app_context().push() db.create_all() collected_date_value = datetime.strptime('2018-06-14', '%Y-%m-%d').date() actor_db = { 'actor_name': 'Marina Silva', 'actor_username': '******', 'channel_id': 'channel_id_value', 'title': 'Marina Silva', 'subscribers': 13515, 'video_count': 876, 'view_count': 4307555, 'created_date': '2010-01-26', 'keywords': 'keywords_value', 'collected_date': collected_date_value, 'thumbnail_url': 'thumbnail_url_value', 'description': 'description_value', 'banner_url': 'banner_url_value', 'hundred_thousand': False } DBYouTube.add_actor(actor_db) video_db = { 'views': '1', 'title': 'Video Marina Silva', 'likes': '1', 'dislikes': '1', 'comments': '1', 'favorites': '1', 'url': 'url Marina Silva', 'publishedAt': 'data publicação', 'description': 'descrição', 'tags': 'tags', 'embeddable': 'embeddable', 'duration': 'duration', 'thumbnail': 'thumbnail', 'category': 'category', 'collected_date': collected_date_value, 'channel_id': 'channel_id_value', 'video_id': '1' } DBYouTube.add_videos(video_db) related_video_db = { 'title': 'Related title', 'likes': '34', 'views': '8234', 'dislikes': '3', 'comments': '2', 'favorites': '10', 'url': 'https://www.youtube.com/watch?v=hFc_scYRasdY', 'publishedAt': '2015-12-13T17:37:01.000Z', 'description': '', 'tags': 'disabled', 'embeddable': 'True', 'duration': 'PT4H24M20S', 'thumbnail': 'https://i.ytimg.com/vi/hFc_sasdRpQY/hqdefault.jpg', 'category': 'Entretenimento', 'video_id': '2', 'collected_date': collected_date_value, 'channel_id': 'channel_id_value' } DBYouTube.add_videos(related_video_db) DBYouTube.add_relationship_videos('2', collected_date_value, '1') DBYouTube.add_actor_video_relationship('1', 'channel_id_value', collected_date_value) self.app = app.test_client() # Propaga as exceções para o cliente de teste self.app.testing = True
def test_is_not_video_in_db_false_by_date(self): bool = DBYouTube.is_not_video_in_db('1', '2018-06-14') self.assertEqual(bool, False)
def test_is_not_video_in_db_true_by_date(self): bool = DBYouTube.is_not_video_in_db('1', '2018-06-01') self.assertEqual(bool, True)
def test_is_not_video_in_db_true_by_id(self): bool = DBYouTube.is_not_video_in_db('fake_id', '2018-06-14') self.assertEqual(bool, True)
def test_db_get_actor_videos(self): videos = DBYouTube.get_actor_videos('2018-06-14', 'channel_id_value') self.assertEqual(videos[0]['likes'], '1')
def test_db_get_dates_error(self): dates = DBYouTube.get_dates() self.assertNotEqual(dates['dates'], ['2019-07-14'])
def test_db_get_info_actor_none(self): actor = DBYouTube.get_info_actor('2018-06-14', 'Bolsonaro') self.assertEqual(actor, None)
def list_dates(): return jsonify(DBYouTube.get_dates())
def list_actors(): list_actors_original = DBYouTube.get_all_actors_name() return jsonify({'actors': list_actors_original})
def test_get_video_by_actor_video_error(self): video = DBYouTube.get_video_by_actor('2018-06-14', 'channel_id_value', 'fake_id') self.assertEqual(video, None)
def test_db_get_actor_videos(self): videos = DBYouTube.get_actor_videos('2018-06-14', 'channel_id_value') self.assertEqual(videos[0]['title'], 'Video Marina Silva')
def test_db_get_dates(self): dates = DBYouTube.get_dates() self.assertEqual(dates['dates'], ['2018-06-14'])
def test_db_get_actor_videos_empty(self): videos = DBYouTube.get_actor_videos('2018-06-15', 'channel_id_value') self.assertEqual(videos, [])
def test_db_get_info_actor_name(self): actor = DBYouTube.get_info_actor('2018-06-14', 'Marina Silva') self.assertEqual(actor['actor_name'], 'Marina Silva')
collected_date=YoutubeAPI.start_time, thumbnail_url=channel_thumbnail, description=description, keywords=keywords, banner_url=banner_thumbnail, above_one_hundred_thousand=hundred_thousand) db.session.add(actor_db) db.session.commit() time.sleep(0.02) videos_views = video.get_all_video_views_user_id( response, parameters['video_limit'], parameters['related_video_limit']) if videos_views: for item in videos_views: related_videos = item['related_to_video'] item['collected_date'] = YoutubeAPI.start_time item['channel_id'] = channel_id DBYouTube.add_videos(item) DBYouTube.add_actor_video_relationship( item['video_id'], channel_id, YoutubeAPI.start_time) for related_video in related_videos: related_video['collected_date'] = YoutubeAPI.start_time related_video['channel_id'] = channel_id DBYouTube.add_videos(related_video) DBYouTube.add_relationship_videos( child_video_id=related_video['video_id'], parent_date=YoutubeAPI.start_time, parent_id=item['video_id'])