-
Notifications
You must be signed in to change notification settings - Fork 2
/
yt_wordcloud.py
50 lines (38 loc) · 1.32 KB
/
yt_wordcloud.py
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
#!/bin/python
import json
import collections as c
import sys
import numpy as np
from PIL import Image
from wordcloud import WordCloud
def red_color_func(word, font_size, position, orientation, random_state=None, **kwargs):
return "#FF0000"
# if no file is given, print usage and exit
if len(sys.argv) < 2:
print("Usage: wordcloud <path_to_json>")
exit()
try:
data = json.load( open(sys.argv[1], 'r') )
except FileNotFoundError:
print("Couldn't find file. Make sure to give the whole path like <directory>/<file>.json")
quit()
except json.decoder.JSONDecodeError:
print("Couldn't decode the JSON. It may be corrupted. Make sure you use the unaltered .json-file from google takeouts.")
quit()
except Error:
print("An unknown Error occured, please contact the project maintainer")
quit()
data = filter(lambda a: 'subtitles' in list(a.keys()) and a['time'].startswith('2020'), data)
yters = c.Counter( [a['subtitles'][0]['name'] for a in data] )
mask = np.array( Image.open("yt_logo.png") )
word_cloud = WordCloud( background_color="#282828"
, repeat=False
, relative_scaling=.75
, margin=4
, font_step=5
, random_state=1
, max_words=600
, mask=mask
).generate_from_frequencies(yters)
word_cloud.recolor(color_func=red_color_func)
word_cloud.to_file("wordcloud.png")