-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.py
158 lines (137 loc) · 5.2 KB
/
main.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
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
import os
import sys
import requests
#import argparse
from urllib.parse import urlparse, urlencode, parse_qs
from youtube.video_comments import VideoComment
from time import sleep, time, asctime
from PIL import Image, ImageDraw, ImageFont, ImageTk
from clients.youtube_client import YouTubeClient
import datetime
import tkinter
import RPi.GPIO as GPIO
apikey = #REPLACE WITH YOUR API KEY
videolink = #REPLACE WITH YOUR VIDEO LINK e.g. "FV2OqOJcQRc"
numComments = 100
commentMaxPages = 1
commentsPerPage = 100
totalPixels = 64
IMAGE_INPUT_FILE = './draw_this_thumbnail.png'
IMAGE_BLANK_FILE = './blank.png'
IMAGE_CANVAS_FILE = './canvas.png'
IMAGE_FRAME_FILE = './frame.png'
IMAGE_OUTPUT_FILE = './draw_this_thumbnail.png'
IMAGE_UPLOAD = './thumbnail.png'
OPENSANS_FONT_FILE = './fonts/OpenSans-ExtraBold.ttf'
YOUTUBE_DATA_API_CREDENTIALS_LOCATION = './creds/client_secret.json'
YOUTUBE_VIDEO_ID = #REPLACE WITH YOUR VIDEO LINK e.g. "FV2OqOJcQRc"
def get_time():
print("Getting time")
ctime = asctime()
return ctime
def create_thumbnail(pixel_list):
print("Creating the thumbnail")
image = Image.open(IMAGE_BLANK_FILE)
canvas = Image.open(IMAGE_CANVAS_FILE)
for px in range(0,numComments*totalPixels,5):
x = pixel_list[px]
y = pixel_list[px+1]
r = pixel_list[px+2]
g = pixel_list[px+3]
b = pixel_list[px+4]
if((x+y) > 0):
#print("Trying: ", x, y, r, g, b)
try:
canvas.putpixel((x,y),(r,g,b))
pixel_list[px] = 0
pixel_list[px+1] = 0
pixel_list[px+2] = 0
pixel_list[px+3] = 0
pixel_list[px+4] = 0
except:
print("Bad Input")
canvas.save(IMAGE_CANVAS_FILE)
image.paste(canvas, (18,113))
frame = Image.open(IMAGE_FRAME_FILE)
image.paste(frame, (0,0), frame)
image.save(IMAGE_OUTPUT_FILE)
filename = ("./archive/thumbnail_" + datetime.datetime.now().replace(microsecond=0).isoformat() + ".png")
image.save(filename, "PNG", quality=100)
image.save(IMAGE_UPLOAD, "PNG", quality=100)
print(asctime(),f"Successfully generated the image and saved to {IMAGE_UPLOAD}")
def set_thumbnail_for_youtube_video(video_id, thumbnail):
youtube_client = YouTubeClient(YOUTUBE_DATA_API_CREDENTIALS_LOCATION)
response = youtube_client.set_thumbnail(video_id, thumbnail)
print(response)
def get_pixel_list():
vc = VideoComment(commentsPerPage, videolink, apikey)
pixel_list = vc.get_video_comments()
#print("The Next Move:", chosen_move)
return(pixel_list)
def send_thumbnail(pixel_list):
# Upload that thumbnail to your YouTube video
if (GPIO.input(17) == 0):
set_thumbnail_for_youtube_video(YOUTUBE_VIDEO_ID, IMAGE_UPLOAD)
else:
print("Emergency Stop Depressed")
def showPIL(pilImage, sleepTime):
showImage = Image.open(pilImage)
root = tkinter.Tk()
w, h = root.winfo_screenwidth(), root.winfo_screenheight()
root.overrideredirect(1)
root.geometry("%dx%d+0+0" % (w, h))
root.focus_set()
root.bind("<Escape>", lambda e: (e.widget.withdraw(), e.widget.quit()))
canvas = tkinter.Canvas(root,width=w,height=h)
canvas.pack()
canvas.configure(background='black')
imgWidth, imgHeight = showImage.size
if imgWidth > w or imgHeight > h:
ratio = min(w/imgWidth, h/imgHeight)
imgWidth = int(imgWidth*ratio)
imgHeight = int(imgHeight*ratio)
showImage = showImage.resize((imgWidth,imgHeight), Image.ANTIALIAS)
image = ImageTk.PhotoImage(showImage)
imagesprite = canvas.create_image(w/2,h/2,image=image)
root.after(sleepTime, root.destroy)
root.mainloop()
showImage.close()
GPIO.setmode(GPIO.BCM)
GPIO.setup(17, GPIO.IN, pull_up_down=GPIO.PUD_UP)
commentWaitTime = time()
uploadWaitTime = time()
list_of_pixels = get_pixel_list()
not_all_zeros = list_of_pixels.any()
if(not_all_zeros):
create_thumbnail(list_of_pixels)
send_thumbnail(list_of_pixels)
else:
print(asctime(), "No New Comments/Data")
showPIL(IMAGE_UPLOAD,15000)
run = 1
uploadFlag = 0
while(run==1):
try:
if (int(time() - commentWaitTime) > 60):
print(asctime(),"Collecting Comments")
list_of_pixels = get_pixel_list()
not_all_zeros = list_of_pixels.any()
if(not_all_zeros):
create_thumbnail(list_of_pixels)
showPIL(IMAGE_UPLOAD,15000)
uploadFlag = 1
commentWaitTime = time()
if (int(time() - uploadWaitTime) > 300):
if(uploadFlag == 1):
print("----------Uploading Image------------")
print("----------Uploading Image------------")
print("----------Uploading Image------------")
sleep(5)
showPIL(IMAGE_UPLOAD,15000)
send_thumbnail(list_of_pixels)
uploadFlag = 0
else:
print(asctime(),"No New Pixels")
uploadWaitTime = time()
except:
run = 0