-
Notifications
You must be signed in to change notification settings - Fork 7
/
youtube-upload-fisl16.py
executable file
·162 lines (147 loc) · 4.78 KB
/
youtube-upload-fisl16.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
159
160
161
162
#! /usr/bin/python
# -*- coding: utf-8 -*-
"""
This script gets all schedule from FISL16, tries to find the video online,
download it and upload onto Youtube..
"""
import simplejson as json
import requests
import re
import os
import sys
from time import sleep
import logging
logging.captureWarnings('InsecurePlatformWarning')
DRYRUN = False
ROOMS = xrange(1,13)
DAYS = [ "08", "09", "10", "11" ]
SERVER = "http://schedule.fisl16.softwarelivre.org/api16"
SECRET = "/home/helio/pytube-client_secret.json"
"""
Rooms
http://schedule.fisl16.softwarelivre.org/api/rooms/1/slots/of-day/2015-07-08
http://schedule.fisl16.softwarelivre.org/api16/rooms/1/slots/of-day/2015-07-11
Talks
http://schedule.fisl16.softwarelivre.org/api16/talks/299
"""
def build_url(room, day):
url = "%s/rooms/%s/slots/of-day/2015-07-%s" % (SERVER, room, day)
return url
def build_talk(talkid):
url = "%s/talks/%s" % (SERVER, talkid)
return url
def youtube(title, descr, author, tags, video):
descr = "%s\n\n%s" % (descr, author)
descr = re.sub("\"", "\\\"", descr)
title = re.sub("\"", "\\\"", title)
cmd = "youtube-upload " + \
u"--title=\"%s\" " % title + \
u"--description=\"%s\" " % descr + \
"--client-secrets=%s " % SECRET + \
u"--tags=\"%s\" " % tags + \
"%s" % video
print cmd
if not DRYRUN:
os.system(cmd)
def wget(video):
print "Running wget"
cmd = "wget %s" % video
if not DRYRUN:
os.system(cmd)
else:
print "Saving fake video file"
touch(video)
def processed(video):
videoname = os.path.basename(video)
# saving space instead
# shutil.move(video, "done/%s" % videoname)
os.unlink(video)
target = "done/%s" % videoname
fd = open(target, "w")
fd.write("done\n")
fd.flush()
fd.close()
def touch(filename):
filename = re.sub(".*/", "", filename)
fd = open(filename, "w")
fd.flush()
fd.close()
print "File %s created" % filename
def info():
print "Getting help..."
print "Usage: %s [dry-run]" % sys.argv[0]
print "\tdry-run: it doesn't download real video, neither tries to " + \
"upload to youtube"
def build_listing():
global DRYRUN
if re.search("help", sys.argv[-1]):
info()
sys.exit(0)
if sys.argv[-1] == "dry-run":
print "Running in dry-run mode"
DRYRUN = True
if not os.path.exists("done"):
os.mkdir("done")
for room in ROOMS:
room = str(room)
for day in DAYS:
print "Retrieving info from room %s at day %s" % (room, day)
url = build_url(room, day)
#print url
resp = requests.get(url)
j = json.loads(resp.text)
for presentation in j["items"]:
print "PRESENTATION"
#print presentation
#return
if presentation["status"] != "confirmed":
continue
timestamp = presentation["begins"]
title = presentation["talk"]["title"]
authors = presentation["talk"]["owner"]
if presentation["talk"]["coauthors"]:
a = ",".join(presentation["talk"]["coauthors"])
authors = "%s e %s" %(a, authors)
#print authors
track = presentation["talk"]["track"]
track = re.sub("[-/]",",", track)
track = re.sub(" e ", ",", track)
track = re.sub(",,", ",", track)
tags = "FISL16, %s" % track
talkid = presentation["talk"]["id"]
url2 = build_talk(talkid)
try:
video = presentation["recordings"][-1]
except IndexError:
continue
#print url2
r = requests.get(url2)
j2 = json.loads(r.text)
full = j2["resource"]["full"]
#print full
#print ""
text = u"""Title: %s
Author(s): %s
Description: %s
Video: %s
Tags: %s
Timestamp: %s
""" % (title, authors, full, video, tags, timestamp)
print text.encode("utf-8")
#return
videoname = os.path.basename(video)
print "Video=%s" % video
print "VideoName=%s" % videoname
videopath = "done/%s" % videoname
if os.path.exists(videopath):
print "Already processed. Skipping..."
#return
continue
wget(video)
youtube(title, full, authors, tags, videoname)
processed(videoname)
#return
# sleep a bit in case to SIGSTOP is needed
sleep(5)
if __name__ == '__main__':
build_listing()