-
Notifications
You must be signed in to change notification settings - Fork 1
/
cccfeed.py
64 lines (52 loc) · 1.78 KB
/
cccfeed.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
#!/usr/bin/env python
from flask import Flask, request, render_template
from werkzeug.contrib.atom import AtomFeed
from flask.ext.cache import Cache
import requests
import feedparser
from datetime import datetime
from reverseproxy import ReverseProxied
from time import mktime
app = Flask(__name__)
app.wsgi_app = ReverseProxied(app.wsgi_app)
app.config['FEED_URL_BASE'] = "https://media.ccc.de/c/33c3/podcast/"
app.config['REQUEST_HEADERS'] = {
"User-Agent": "CCC Torrent Feed Maker"
}
app.config['CONTENT_TYPES'] = [
'video/webm',
'video/mp4',
'audio/mpeg',
'audio/opus'
]
app.config.from_pyfile('config.py', silent=True)
cache = Cache(app, config={'CACHE_TYPE': 'simple'})
@cache.cached(timeout=300)
def fetch(url):
return requests.get(url, headers=app.config['REQUEST_HEADERS']).content
def scrape(url):
source = feedparser.parse(fetch(url))
title = source.feed.title
out = AtomFeed(title, feed_url=request.url, url=request.url_root)
for entry in source.entries:
for link in entry.links:
if link.type in app.config['CONTENT_TYPES']:
torrent_url = "%s.torrent" % link.url
out.add(entry.title, summary=entry.summary, url=torrent_url, xml_base='',
updated=datetime.fromtimestamp(mktime(entry.updated_parsed)),
published=datetime.fromtimestamp(mktime(entry.published_parsed)))
return out.get_response()
@app.route("/")
def hello():
feeds = {
'webm': ['webm'],
'mp4': ['mp4'],
'mp3': ['mp3'],
'opus': ['opus']
}
return render_template('index.html', feeds=feeds)
@app.route("/feed/<name>.atom")
def feed(name):
return scrape("%s/%s.xml" % (app.config['FEED_URL_BASE'], name))
if __name__ == "__main__":
app.run()