Last active
December 14, 2015 17:19
-
-
Save lavarini/5121812 to your computer and use it in GitHub Desktop.
Crawler dota youtube channels
This file contains hidden or 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
| # -*- coding:utf-8 -*- | |
| import requests | |
| from django.core.management.base import BaseCommand | |
| from django.conf import settings | |
| from palcomp3.website import purges | |
| from palcomp3.website.utils import varnish | |
| varnish.register(purges.resources) | |
| class Command(BaseCommand): | |
| help = u'Comando responsavel por atualizar as views dos videos' | |
| def handle(self, *args, **options): | |
| """ | |
| font: http://stackoverflow.com/questions/13504899/how-do-i-get-a-list-of-uploaded-videos-for-a-certain-channel-with-the-new-youtub | |
| """ | |
| lista = ['SheeverGaming', 'beyondthesummittv', 'ThePremierrLeague','joinDOTA'] | |
| self.teams = {} | |
| self.API_KEY = 'AIzaSyBLznD1SjRV-HHAZwAaCeIL5KnOM3eMds4' | |
| for i in lista: | |
| print '###########################'+i+'#####################################' | |
| url = ("https://www.googleapis.com/youtube/v3/search?part=snippet&q=%s&type=channel&key=%s")%(i,self.API_KEY) | |
| requisicao = requests.get(url) | |
| requisicao.raise_for_status() | |
| resposta = requisicao.json | |
| self.data = {} | |
| first = resposta['items'][0] | |
| self.data['channelId'] = first['id']['channelId'] | |
| snippet = first['snippet'] | |
| self.data['slug'] = snippet['title'] | |
| self.data['description'] = snippet['description'] | |
| self.data['thumb'] = { | |
| 'default':snippet['thumbnails']['default']['url'], | |
| 'medium':snippet['thumbnails']['medium']['url'], | |
| 'high':snippet['thumbnails']['high']['url'] | |
| } | |
| print self.data | |
| url = ("https://www.googleapis.com/youtube/v3/channels?part=snippet%%2CcontentDetails&id=%s&key=%s")%(self.data['channelId'],self.API_KEY) | |
| requisicao = requests.get(url) | |
| requisicao.raise_for_status() | |
| resposta = requisicao.json | |
| first = resposta['items'][0] | |
| contentDetails = first['contentDetails']['relatedPlaylists'] | |
| if 'likes' in contentDetails: | |
| self.data['likes'] = contentDetails['likes'] | |
| self.data['playlistId'] = contentDetails['uploads'] | |
| print self.data | |
| url = ("https://www.googleapis.com/youtube/v3/playlistItems?part=snippet&maxResults=50&playlistId=%s&key=%s")%(self.data['playlistId'],self.API_KEY) | |
| requisicao = requests.get(url) | |
| requisicao.raise_for_status() | |
| resposta = requisicao.json | |
| self.data['videos'] = {} | |
| nextPageToken = True | |
| while nextPageToken: | |
| print nextPageToken | |
| resposta = self.get_videos(resposta) | |
| nextPageToken = False | |
| if 'nextPageToken' in resposta: | |
| nextPageToken = resposta['nextPageToken'] | |
| def get_videos(self, resposta): | |
| nextPageToken = resposta['nextPageToken'] | |
| for i in resposta['items']: | |
| video = {} | |
| snippet = i['snippet'] | |
| video['title'] = snippet['title'] | |
| video['thumb'] = { | |
| 'default': snippet['thumbnails']['default']['url'], | |
| 'medium': snippet['thumbnails']['medium']['url'], | |
| 'high': snippet['thumbnails']['high']['url'] | |
| } | |
| video['id'] = snippet['resourceId']['videoId'] | |
| video['data'] = snippet['publishedAt'] | |
| video['pos'] = snippet['position'] | |
| if video['id'] in self.data['videos']: | |
| print '$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$' | |
| print video['id'] | |
| self.get_teams(video) | |
| self.data['videos'][video['id']] = video | |
| url = ("https://www.googleapis.com/youtube/v3/playlistItems?part=snippet&maxResults=50&pageToken=%s&playlistId=%s&key=%s")%(nextPageToken,self.data['playlistId'],self.API_KEY) | |
| requisicao = requests.get(url) | |
| requisicao.raise_for_status() | |
| return requisicao.json | |
| def get_teams(videos): | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment