-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathmain.py
More file actions
76 lines (63 loc) · 2.06 KB
/
main.py
File metadata and controls
76 lines (63 loc) · 2.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import json
import urllib.request
from datetime import datetime
import os
import isodate
def checkYear(time, year):
try:
date_to_check = datetime.strptime(time, "%Y-%m-%dT%H:%M:%S.%fZ")
except ValueError:
date_to_check = datetime.strptime(time, "%Y-%m-%dT%H:%M:%SZ")
return date_to_check.year == year
def getVideoLength(url):
temp = url.split('?v=')
video_id = temp[-1]
api_key = "PutKeyHere!" # get a key for free from https://console.developers.google.com/
searchUrl = (
"https://www.googleapis.com/youtube/v3/videos?id="
+ video_id
+ "&key="
+ api_key
+ "&part=contentDetails"
)
response = urllib.request.urlopen(searchUrl).read()
data = json.loads(response)
all_data = data["items"]
try:
contentDetails = all_data[0]["contentDetails"]
duration = contentDetails["duration"]
duration = isodate.parse_duration(duration)
video_dur = duration.total_seconds()
return video_dur
except:
return 0
def run(year, file):
results = dict()
with open(file, encoding='utf-8') as f:
data = json.load(f)
for video in data:
try:
url = video["titleUrl"]
except KeyError:
pass # lazy handling, but usually video was deleted
if checkYear(video["time"], year):
if "subtitles" not in video:
pass
else:
youtuber = video["subtitles"][0]["name"]
if youtuber in results:
results[youtuber] += getVideoLength(url)
else:
results[youtuber] = getVideoLength(url)
top = sorted(results, key=results.get, reverse=True)
for v in top:
print(v, results[v])
#get the sum of the video lengths
sum = 0
for v in top:
sum += results[v]
print("Sum:", sum)
if __name__ == "__main__":
run(2021, "watch-history.json") # get it from google takeout and copy the file path