/
video.py
253 lines (226 loc) · 10.6 KB
/
video.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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
# -*- coding: utf-8 -*-
from urlparse import urlparse, parse_qs
import socket
import requests
from flask import render_template, url_for, g, flash, abort, redirect, Markup, request, json, escape
from coaster.views import load_models
from baseframe.forms import render_form, render_redirect, render_delete_sqla, render_message
from hgtv import app
from hgtv.forms import VideoAddForm, VideoEditForm, VideoVideoForm, VideoSlidesForm, PlaylistAddForm
from hgtv.models import Channel, Video, Playlist, PlaylistVideo, db
from hgtv.views.login import lastuser
# helpers
def process_video(video, new=False):
"""
Get metadata for the video from the corresponding site
"""
# Parse the video url
if video.video_url:
parsed = urlparse(video.video_url)
# Check video source and get corresponding data
if parsed.netloc in ['youtube.com', 'www.youtube.com']:
video_id = parse_qs(parsed.query)['v'][0]
try:
r = requests.get('https://gdata.youtube.com/feeds/api/videos/%s?v=2&alt=json' % video_id)
if r.json is None:
raise RuntimeError("Unable to fectch data")
else:
if new:
video.title = r.json['entry']['title']['$t']
video.description = escape(r.json['entry']['media$group']['media$description']['$t'])
for item in r.json['entry']['media$group']['media$thumbnail']:
if item['yt$name'] == 'mqdefault':
video.thumbnail_url = item['url'] # .replace('hqdefault', 'mqdefault')
video.video_sourceid = video_id
video.video_source = u"youtube"
except requests.ConnectionError:
raise RuntimeError("Unable to establish connection")
except socket.gaierror:
raise RuntimeError("Unable to resolve the URL")
else:
raise ValueError("Unsupported video site")
else:
raise ValueError("Video URL is missing")
def process_slides(video):
"""
Get metadata for slides from the corresponding site
"""
if video.slides_url:
parsed = urlparse(video.slides_url)
if parsed.netloc in ['slideshare.net', 'www.slideshare.net']:
try:
r = requests.get('http://www.slideshare.net/api/oembed/2?url=%s&format=json' % video.slides_url)
if r.json:
video.slides_source = u'slideshare'
video.slides_sourceid = r.json['slideshow_id']
video.slides_html = r.json['html']
else:
raise RuntimeError("Unable to fetch data")
except requests.ConnectionError:
raise RuntimeError("Unable to establish connection")
except socket.gaierror:
raise RuntimeError("Unable to resolve the URL")
elif parsed.netloc in ['speakerdeck.com', 'www.speakerdeck.com']:
try:
r = requests.get('http://speakerdeck.com/oembed.json?url=%s' % video.slides_url)
video.slides_source = u'speakerdeck'
video.slides_html = r.json['html']
except requests.ConnectionError:
raise RuntimeError("Unable to establish connection")
except socket.gaierror:
raise RuntimeError("Unable to resolve the URL")
else:
raise ValueError("Unsupported slides site")
else:
video.slides_url = u''
video.slides_source = u''
video.slides_html = u''
@app.route('/<channel>/<playlist>/new', methods=['GET', 'POST'])
@lastuser.requires_login
@load_models(
(Channel, {'name': 'channel'}, 'channel'),
(Playlist, {'name': 'playlist', 'channel': 'channel'}, 'playlist'),
permission='new-video')
def video_new(channel, playlist):
"""
Add a new video
"""
if channel.userid not in g.user.user_organizations_owned_ids():
"""
User doesn't own this playlist
"""
abort(403)
form = VideoAddForm()
if form.validate_on_submit():
video = Video(playlist=playlist)
form.populate_obj(video)
process_video(video, new=True)
process_slides(video)
video.make_name()
playlist.videos.append(video)
db.session.commit()
flash(u"Added video '%s'." % video.title, 'success')
return render_redirect(url_for('video_edit', channel=channel.name, playlist=playlist.name, video=video.url_name))
return render_form(form=form, title="New Video", submit="Add", cancel_url=url_for('channel_view', channel=channel.name), ajax=False)
# Use /view as a temp workaround to a Werkzeug URLmap sorting bug
@app.route('/<channel>/<playlist>/<video>/view', methods=['GET', 'POST'])
@load_models(
(Channel, {'name': 'channel'}, 'channel'),
(Playlist, {'name': 'playlist', 'channel': 'channel'}, 'playlist'),
(Video, {'url_name': 'video'}, 'video'),
permission='view')
def video_view(channel, playlist, video):
"""
View video
"""
if playlist not in video.playlists:
# This video isn't in this playlist. Redirect to canonical URL
return redirect(url_for('video_view', channel=video.channel.name, playlist=video.playlist.name, video=video.url_name))
#form = PlaylistAddForm()
#playlists = set(channel.playlists) - set(video.playlists)
#form.playlist.choices = [(p.id, p.title) for p in playlists]
#if form.validate_on_submit():
# if channel.userid not in g.user.user_organizations_owned_ids():
# abort(403)
# playlist = Playlist.query.get(form.data['playlist'])
# playlist.videos.append(video)
# db.session.commit()
# flash(u"Added video '%s'." % video.title, 'success')
return render_template('video.html', title=video.title, channel=channel, playlist=playlist, video=video)
@app.route('/<channel>/<playlist>/<video>/edit', methods=['GET', 'POST'])
@lastuser.requires_login
@load_models(
(Channel, {'name': 'channel'}, 'channel'),
(Playlist, {'name': 'playlist', 'channel': 'channel'}, 'playlist'),
(Video, {'url_name': 'video'}, 'video'),
permission='edit')
def video_edit(channel, playlist, video):
"""
Edit video
"""
if video.channel.userid not in g.user.user_organizations_owned_ids():
# User isn't authorized to edit
abort(403)
if playlist != video.playlist:
# This video isn't in this playlist. Redirect to canonical URL
return redirect(url_for('video_edit', channel=video.channel.name, playlist=video.playlist.name, video=video.url_name))
form = VideoEditForm(obj=video)
formvideo = VideoVideoForm(obj=video)
formslides = VideoSlidesForm(obj=video)
form_id = request.form.get('form.id')
if request.method == "POST":
if form_id == u'video': # check whether done button is clicked
if form.validate_on_submit():
form.populate_obj(video)
video.make_name()
db.session.commit()
flash(u"Edited video '%s'." % video.title, 'success')
return render_redirect(url_for('video_view', channel=channel.name, playlist=playlist.name, video=video.url_name))
elif form_id == u'video_url': # check video_url was updated
if formvideo.validate_on_submit():
formvideo.populate_obj(video)
process_video(video, new=False)
db.session.commit()
return render_redirect(url_for('video_edit', channel=channel.name, playlist=playlist.name, video=video.url_name))
elif form_id == u'slide_url': # check slides_url was updated
if formslides.validate_on_submit():
formslides.populate_obj(video)
process_slides(video)
db.session.commit()
return render_redirect(url_for('video_edit', channel=channel.name, playlist=playlist.name, video=video.url_name))
return render_template('videoedit.html',
channel=channel,
playlist=playlist,
video=video,
form=form,
formvideo=formvideo,
formslides=formslides)
@app.route('/<channel>/<playlist>/<video>/delete', methods=['GET', 'POST'])
@lastuser.requires_login
@load_models(
(Channel, {'name': 'channel'}, 'channel'),
(Playlist, {'name': 'playlist', 'channel': 'channel'}, 'playlist'),
(Video, {'url_name': 'video'}, 'video'),
permission='delete')
def video_delete(channel, playlist, video):
"""
Delete video
"""
if video.channel.userid not in g.user.user_organizations_owned_ids():
# User isn't authorized to delete
abort(403)
if playlist != video.playlist:
# This video isn't in this playlist. Redirect to canonical URL
return redirect(url_for('video_delete', channel=video.channel.name, playlist=video.playlist.name, video=video.url_name))
return render_delete_sqla(video, db, title=u"Confirm delete",
message=u"Delete video '%s'? This will remove the video from all playlists it appears in." % video.title,
success=u"You have deleted video '%s'." % video.title,
next=url_for('playlist_view', channel=channel.name, playlist=playlist.name))
@app.route('/<channel>/<playlist>/<video>/remove', methods=['GET', 'POST'])
@lastuser.requires_login
@load_models(
(Channel, {'name': 'channel'}, 'channel'),
(Playlist, {'name': 'playlist', 'channel': 'channel'}, 'playlist'),
(Video, {'url_name': 'video'}, 'video'),
permission='remove-video')
def video_remove(channel, playlist, video):
"""
Remove video from playlist
"""
if playlist not in video.playlists:
# This video isn't in this playlist
abort(404)
if channel.userid not in g.user.user_organizations_owned_ids():
# User doesn't own this playlist
abort(403)
# If this is the primary playlist for this video, refuse to remove it.
if playlist == video.playlist:
return render_message(title="Cannot remove",
message=Markup("Videos cannot be removed from their primary playlist. "
'<a href="%s">Return to video</a>.' % url_for('video_view',
channel=channel.name, playlist=playlist.name, video=video.url_name)))
connection = PlaylistVideo.query.filter_by(playlist_id=playlist.id, video_id=video.id).first_or_404()
return render_delete_sqla(connection, db, title="Confirm remove",
message=u"Remove video '%s' from playlist '%s'?" % (video.title, playlist.title),
success=u"You have removed video '%s' from playlist '%s'." % (video.title, playlist.title),
next=url_for('playlist_view', channel=channel.name, playlist=playlist.name))