-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtasks.py
More file actions
74 lines (60 loc) · 1.84 KB
/
Copy pathtasks.py
File metadata and controls
74 lines (60 loc) · 1.84 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
from lib.exceptions import VideoTooLong, DownError, VideoNotFound
from tayyip_recognizer import TayyipRecognizer
from youtube import Downloader
from celery import Celery
from lib.video import Video
import logger
import os
import re
app = Celery('tasks', broker=os.environ['REDIS_SERVER'])
@app.task
def add_new_video(youtube_id):
Video.connect()
#
# iterate all the workers and all the running jobs
# and stop the ones that already running
#
def find_duplicated_job():
run_count = 0
workers = app.control.inspect()
active = workers.active()
for worker, jobs in active.iteritems():
for job in jobs:
# args is a string tuple, need to get the id
match = re.match(r'\(u\'(.{11})\',\)', job['args'])
if match and match.group(1) == youtube_id:
run_count += 1
return run_count > 1
if find_duplicated_job():
print "Already running "
return
dl = Downloader()
tr = TayyipRecognizer()
video = Video()
try:
(path_to_wave, thumbnail, title) = dl.start(youtube_id)
except VideoTooLong as e:
video.set(youtube_id=youtube_id, state="too_long")
video.save()
return
except VideoNotFound as e:
video.set(youtube_id=youtube_id, state="not_found")
video.save()
return
except DownError as e:
video.set(youtube_id=youtube_id, state="not_found")
video.save()
return
# Video might already exist if multiple jobs are in the queue
if video.fetch(youtube_id):
return
(rate, predictions) = tr.recognize(path_to_wave)
video.set(
youtube_id=youtube_id,
thumbnail=thumbnail,
title=title,
rate=rate,
predictions=predictions
)
video.save()
Video.disconnect()