-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
15 changed files
with
342 additions
and
3 deletions.
There are no files selected for viewing
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
from django.contrib import admin | ||
from .models import YouTubeSearch | ||
|
||
admin.site.register(YouTubeSearch) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
from django.apps import AppConfig | ||
|
||
|
||
class ImportdataConfig(AppConfig): | ||
name = 'importdata' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
# Generated by Django 2.1.1 on 2018-09-11 18:44 | ||
|
||
from django.db import migrations, models | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
initial = True | ||
|
||
dependencies = [ | ||
] | ||
|
||
operations = [ | ||
migrations.CreateModel( | ||
name='YouTubeSearch', | ||
fields=[ | ||
('id', models.CharField(help_text='Video ID', max_length=100, primary_key=True, serialize=False, verbose_name='Video ID')), | ||
('name', models.CharField(help_text='Name of the game', max_length=100, null=True, verbose_name='Name')), | ||
('count_views', models.IntegerField(help_text='Numbers of video views', null=True, verbose_name='count_views')), | ||
('count_likes', models.IntegerField(help_text='Number of likes', null=True, verbose_name='count_likes')), | ||
('count_dislikes', models.IntegerField(help_text='Number of dislikes', null=True, verbose_name='count_dislikes')), | ||
('count_comments', models.IntegerField(help_text='Number of comment', null=True, verbose_name='count_comment')), | ||
('regionCode', models.CharField(max_length=10, null=True, verbose_name='Region Code')), | ||
], | ||
), | ||
] |
38 changes: 38 additions & 0 deletions
38
YoutubeService/importdata/migrations/0002_auto_20180911_2311.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
# Generated by Django 2.1.1 on 2018-09-11 23:11 | ||
|
||
from django.db import migrations, models | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
('importdata', '0001_initial'), | ||
] | ||
|
||
operations = [ | ||
migrations.RemoveField( | ||
model_name='youtubesearch', | ||
name='count_comments', | ||
), | ||
migrations.RemoveField( | ||
model_name='youtubesearch', | ||
name='count_dislikes', | ||
), | ||
migrations.RemoveField( | ||
model_name='youtubesearch', | ||
name='count_likes', | ||
), | ||
migrations.RemoveField( | ||
model_name='youtubesearch', | ||
name='count_views', | ||
), | ||
migrations.RemoveField( | ||
model_name='youtubesearch', | ||
name='name', | ||
), | ||
migrations.AlterField( | ||
model_name='youtubesearch', | ||
name='regionCode', | ||
field=models.CharField(max_length=10, null=True, verbose_name='regionCode'), | ||
), | ||
] |
18 changes: 18 additions & 0 deletions
18
YoutubeService/importdata/migrations/0003_auto_20180911_2345.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
# Generated by Django 2.1.1 on 2018-09-11 23:45 | ||
|
||
from django.db import migrations, models | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
('importdata', '0002_auto_20180911_2311'), | ||
] | ||
|
||
operations = [ | ||
migrations.AlterField( | ||
model_name='youtubesearch', | ||
name='id', | ||
field=models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID'), | ||
), | ||
] |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
from django.db import models | ||
|
||
|
||
class YouTubeSearch(models.Model): | ||
|
||
list_id = models.CharField( | ||
('Video ID'), | ||
help_text=("Video ID"), | ||
max_length=100, | ||
primary_key=True | ||
) | ||
|
||
name = models.CharField( | ||
('Name'), | ||
help_text=('Name of the game'), | ||
max_length=100, | ||
null=True | ||
) | ||
|
||
count_views = models.IntegerField( | ||
('count_views'), | ||
help_text=('Numbers of video views'), | ||
null=True | ||
) | ||
|
||
count_likes = models.IntegerField( | ||
('count_likes'), | ||
help_text=("Number of likes"), | ||
null=True | ||
) | ||
|
||
count_dislikes = models.IntegerField( | ||
('count_dislikes'), | ||
help_text=("Number of dislikes"), | ||
null=True | ||
) | ||
|
||
count_comments = models.IntegerField( | ||
('count_comment'), | ||
help_text=('Number of comment'), | ||
null=True | ||
) | ||
|
||
count_favorites = models.IntegerField( | ||
('count_favorites'), | ||
help_text=("Number favorites of video"), | ||
null=True | ||
) | ||
|
||
regionCode = models.CharField( | ||
('regionCode'), | ||
help_text=(''), | ||
max_length=10, | ||
null=True | ||
) | ||
|
||
def __str__(self): | ||
""" | ||
Returns the object as a string, the attribute that will represent | ||
the object. | ||
""" | ||
return self.name |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
from .models import YouTubeView | ||
from rest_framework import serializers | ||
|
||
class GameSerializer(serializers.ModelSerializer): | ||
|
||
class Meta: | ||
model = YouTubeSearch | ||
#fields = '__all__' | ||
fields = ('__all__') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
from django.test import TestCase | ||
|
||
# Create your tests here. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
from django.urls import include, path | ||
from .views import YouTubeView | ||
|
||
urlpatterns = [ | ||
path('get_youtube_search/', YouTubeView.as_view()) | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,162 @@ | ||
import requests | ||
from rest_framework.response import Response | ||
from rest_framework.views import APIView | ||
|
||
from .models import YouTubeSearch | ||
|
||
#YouTubeSearch.objects.all().delete() | ||
|
||
class YouTubeView(APIView): | ||
''' | ||
View that calls Youtube API | ||
and return some relevant | ||
information about a video | ||
and filter | ||
''' | ||
|
||
def get(self, request, format=None): | ||
|
||
igdb_header = {'Accept': 'application/json'} | ||
|
||
igdb_url = 'http://igdbweb:8000/api/get_igdb_games_list/name' | ||
igdb_data = requests.get(igdb_url, headers=igdb_header).json() | ||
|
||
for game in igdb_data: | ||
print('pesquisando jogo: {}\n'.format(game['name'])) | ||
header = {'user-key': 'AIzaSyDmDXP_gaB7cog4f0slbbdJ3RACsY5WQIw', | ||
'Accept': 'application/json'} | ||
url = 'https://www.googleapis.com/youtube/v3/search?part=snippet&maxResults=50&q={}&key={}'.format(game['name'], header['user-key']) | ||
data = requests.get(url) | ||
ndata = data.json() | ||
filtered_data = self.filter_data(ndata) | ||
for i in range(49): | ||
|
||
video_data = self.get_video(filtered_data['list_id'][i]) | ||
filter_data_video = self.filter_data_video(video_data) | ||
if filter_data_video: | ||
self.save_youtube_search(filtered_data, filter_data_video, i, game['name']) | ||
|
||
|
||
|
||
return Response(data=ndata) | ||
|
||
|
||
def get_video(self, idvideo): | ||
YouTubeSearch.objects.all().delete() | ||
header = {'user-key': 'AIzaSyDmDXP_gaB7cog4f0slbbdJ3RACsY5WQIw', | ||
'Accept': 'application/json'} | ||
|
||
url = 'https://www.googleapis.com/youtube/v3/videos?part=statistics&id={}&key=AIzaSyDmDXP_gaB7cog4f0slbbdJ3RACsY5WQIw'.format(idvideo) | ||
data = requests.get(url) | ||
ndata = data.json() | ||
|
||
return ndata | ||
|
||
def filter_data(self, videodata): | ||
|
||
list_id = [] | ||
|
||
for i in range(50): | ||
if 'items' in videodata: | ||
if 'id' in videodata['items'][i]: | ||
if 'videoId' in videodata['items'][i]['id']: | ||
id = videodata['items'][i]['id']['videoId'] | ||
list_id.append(id) | ||
else: | ||
id = None | ||
else: | ||
id = None | ||
else: | ||
id = None | ||
|
||
|
||
if 'regionCode' in videodata: | ||
regionCode = videodata['regionCode'] | ||
else: | ||
regionCode = None | ||
|
||
filtered_data = { | ||
'list_id': list_id, | ||
'regionCode': regionCode | ||
} | ||
return filtered_data | ||
|
||
def filter_data_video(self, videodata): | ||
|
||
count_views = videodata['items'][0]['statistics']['viewCount'] | ||
count_likes = videodata['items'][0]['statistics']['likeCount'] | ||
count_dislikes = videodata['items'][0]['statistics']['dislikeCount'] | ||
count_favorites = videodata['items'][0]['statistics']['favoriteCount'] | ||
count_comments = videodata['items'][0]['statistics']['commentCount'] | ||
|
||
''' | ||
print('----------------\n', videodata) | ||
if 'items' in videodata: | ||
print('tem item') | ||
if 'statistics' in videodata['items']: | ||
print('tem statistics\n') | ||
if 'viewCount' in videodata['items']['statistics']: | ||
count_views = videodata['items']['statistics']['viewCount'] | ||
print('viewCount aqui: ', count_views) | ||
else: | ||
count_views = None | ||
else: | ||
count_views = None | ||
else: | ||
count_views = None | ||
print('viewCount aqui: ', count_views) | ||
if 'likeCount' in statistics['items']: | ||
count_likes=statistics['items']['statistics']['likeCount'] | ||
else: | ||
count_likes=None | ||
if 'dislikeCount' in statistics: | ||
count_dislikes=statistics['items']['statistics']['dislikeCount'] | ||
else: | ||
count_dislikes = None | ||
if 'commentCount' in statistics: | ||
count_comments= statistics['items']['statistics']['commentCount'] | ||
else: | ||
count_comments = None | ||
else: | ||
count_views=None | ||
count_likes=None | ||
count_dislikes=None | ||
count_comments=None | ||
''' | ||
|
||
filtered_data = { | ||
'count_views': count_views, | ||
'count_likes': count_likes, | ||
'count_dislikes': count_dislikes, | ||
'count_favorites': count_favorites, | ||
'count_comments': count_comments | ||
} | ||
return filtered_data | ||
|
||
|
||
def save_youtube_search(self, filtered_data, filtered_data_video, id, game_name): | ||
|
||
results = YouTubeSearch( | ||
list_id = filtered_data['list_id'][id], | ||
#name = filtered_data['name'], | ||
count_views = filtered_data_video['count_views'], | ||
count_likes = filtered_data_video['count_likes'], | ||
count_dislikes = filtered_data_video['count_dislikes'], | ||
count_favorites = filtered_data_video['count_favorites'], | ||
count_comments = filtered_data_video['count_comments'] | ||
#regionCode = filtered_data['regionCode'] | ||
) | ||
|
||
results.save() | ||
|
||
print('-----RELACIONADO AO JOGO: {}---------\n'.format(game_name)) | ||
print('video id: {}\nviews: {}\nlikes: {}\ndislikes: {}\n'.format(results.list_id, results.count_views, results.count_likes, results.count_dislikes)) | ||
print('favorites: {}\ncomments: {}'.format(results.count_favorites, results.count_comments)) | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,3 +3,4 @@ psycopg2 | |
coverage | ||
coveralls | ||
djongo | ||
djangorestframework==3.8.2 |