· 5 years ago · Aug 06, 2020, 08:10 AM
1#!/usr/bin/python2
2
3import urllib #importing to use its urlencode function
4import urllib2 #for making http requests
5import json #for decoding a JSON response
6
7import sys
8import csv
9
10import time
11
12API_KEY = #hier API key eintragen
13
14try:
15 metadata = sys.argv[1]
16except IndexError:
17 print "Video-ID angeben!"
18 exit()
19
20
21f = open(metadata + '.csv', 'wb')
22
23csv_file = csv.writer(f,delimiter=';')
24csv_file.writerow(['Unix-Time','Date','Views','Likes','Dislikes','Comments'])
25
26
27while(1):
28 SpecificVideoID = metadata
29 SpecificVideoUrl = 'https://www.googleapis.com/youtube/v3/videos?part=snippet%2CcontentDetails%2Cstatistics&id='+SpecificVideoID+'&key='+API_KEY
30 response = urllib2.urlopen(SpecificVideoUrl) #makes the call to a specific YouTube
31 videos = json.load(response) #decodes the response so we can work with it
32 videoMetadata = [] #declaring our list
33 for video in videos['items']:
34
35 print "Number of views: "+video['statistics']['viewCount']
36 print "Number of likes: "+video['statistics']['likeCount']
37 print "Number of dislikes: "+video['statistics']['dislikeCount']
38 print "Number of comments: "+video['statistics']['commentCount']
39 print "\n"
40
41 current_time = time.time() + 3600*2 #zu Sommerzeit
42 csv_file.writerow([current_time,current_time/86400+25569,video['statistics']['viewCount'],video['statistics']['likeCount'],video['statistics']['dislikeCount'],video['statistics']['commentCount']])
43
44 f.close()
45 time.sleep(30)
46 f = open(metadata + '.csv', 'ab')
47 csv_file = csv.writer(f,delimiter=';')
48