This repository has been archived by the owner on Feb 21, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
96 lines (78 loc) · 2.62 KB
/
app.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
# OVERALL IMPORTS
from flask import Flask, request
from threading import Thread
import schedule
import time
import os
import pdb
import json
# PODCAST IMPORTS
from podcasts.series_driver import SeriesDriver
from podcasts.episodes_driver import EpisodesDriver
from podcasts.site_crawler import SiteCrawler
from utils.couchbase_storer import CouchbaseStorer
from utils.series_patcher import SeriesPatcher
from utils.constants import *
from utils.thread_pool import *
import utils.log
# Flask App
app = Flask(__name__)
logger = utils.log.logger
patcher = SeriesPatcher("lol")
def digest_podcasts():
"""
Digest most popular podcasts from
iTunes on a daily-basis
"""
# Grab all series first
SeriesDriver(BASE_DIR).get_series_from_urls(SiteCrawler().all_urls())
storer = \
(CouchbaseStorer(PODCASTS_BUCKET_URL)
if PODCASTS_BUCKET_PASSWORD == ''
else CouchbaseStorer(PODCASTS_BUCKET_URL, PODCASTS_BUCKET_PASSWORD))
# Grab all episodes once we have data stored
EpisodesDriver(DIRECTORY, storer).eps_from_series()
def start_rss_polling():
"""
Create a thread pool and a job queue to check the rss feeds of every series
in the podcasts bucket.
"""
logger.info("Starting RSS polling with {} threads and job queue of size {}".format(NUM_RSS_THREADS, JOB_QUEUE_SIZE))
thread_pool = ThreadPool(NUM_RSS_THREADS, JOB_QUEUE_SIZE)
limit = 100
offset = 0
for i in range(NUM_RSS_THREADS):
series_list = patcher.get_series_with_limit(limit, offset)
rss_feed_tups = patcher.create_rss_feed_tups(series_list)
args = (rss_feed_tups, CHECK_TIMESTAMP)
thread_pool.add_task(patcher.patch_multiple, args)
offset += limit
thread_pool.wait_for_all()
def run_schedule():
"""
Check schedule and run pending
"""
while 1:
schedule.run_pending()
time.sleep(1)
@app.route('/refresh/<series_id>')
def refresh(series_id):
"""
Params:
series_id [int] - id for the series as designated by apple
Returns:
JSON object with keys "success" (either 0 or 1) and "episode"
that contains the entire episode object.
Given a series_id, checks to see if we should request apple
to get new episodes. If there are new episodes, returns the new episode object.
"""
episode = {"series_id": series_id, "episode-id": 420, "description":"testing"}
response = {"success": 0, "episode": episode}
return json.dumps(response)
if __name__ == '__main__':
# schedule.every(ONE_DAY).seconds.do(digest_podcasts)
# schedule.every(15*MINUTES).seconds.do(start_rss_polling)
# t = Thread(target=run_schedule)
# t.start()
# start_rss_polling()
app.run(debug=True, host='0.0.0.0', port=5000, use_reloader=False)