This repository has been archived by the owner on Sep 15, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
executable file
·125 lines (99 loc) · 3.48 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
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
import datetime
import urllib
from bs4 import BeautifulSoup
from flask import Flask
from flask import jsonify
from flask import render_template
from flask import make_response
from flask_cors import CORS, cross_origin
OCAT_URL = 'https://overclockers.at'
RECENT_RES = 'search.php?action=getdaily'
app = Flask(__name__)
cors = CORS(app)
app.config.update(
JSONIFY_PRETTYPRINT_REGULAR=False,
CORS_HEADERS='Content-Type',
)
def get_news_html():
response = urllib.request.urlopen(OCAT_URL)
return response.read()
def get_recent_html():
response = urllib.request.urlopen('{}/{}'.format(OCAT_URL, RECENT_RES))
return response.read()
def parse_news():
html = BeautifulSoup(get_news_html())
news = []
for item in html.find_all(class_='news'):
if 'mobilead' in item.attrs['class']:
# Fix for an ad container that uses the news class
continue
a = item.find("h2").find("a")
title = a.string
link = OCAT_URL + a.get('href')
description = item.find(class_='previewtext').text.strip()
details = item.find(class_='details')
author = details.contents[2].strip()
date_string = details.contents[4].strip()
date = datetime.datetime.strptime(date_string, '%d.%m.%Y')
news.append({
'title': title,
'link': link,
'description': description,
'author': author,
'date': date.date().isoformat(),
})
return news
def parse_recent():
recent_posts = BeautifulSoup(get_recent_html())
recent = []
for row in recent_posts.find(id='idThreadTable').find('tbody').find_all('tr'):
a = row.find(class_='title').find('h5').find('a')
title = a.text
link = OCAT_URL + a.get('href')
forum = row.find(class_='forum').find('a').text.strip()
forum_link = OCAT_URL + row.find(class_='forum').find('a').get('href')
starter = row.find(class_='starter').find('a').text.strip()
starter_link = OCAT_URL + row.find(class_='starter').find('a').get('href')
last_post = row.find(class_='lastpost')
last_post_date_string = last_post.contents[0].strip()
last_post_date = datetime.datetime.strptime(last_post_date_string, '%d.%m.%Y %H:%M')
last_post_by = last_post.contents[1].contents[1].text
last_post_by_link = OCAT_URL + last_post.contents[1].contents[1].get('href')
last_post_link = OCAT_URL + last_post.contents[1].contents[3].get('href')
recent.append({
'title': title,
'link': link,
'forum': {
'name': forum,
'link': forum_link,
},
'starter': {
'name': starter,
'link': starter_link,
},
'lastPost': {
'by': {
'name': last_post_by,
'link': last_post_by_link,
},
'date': last_post_date.isoformat(),
'link': last_post_link,
},
})
return recent
@app.route('/')
def index():
news = parse_news()
response = make_response(render_template('rss2.0.xml', news=news))
response.mimetype = 'application/rss+xml'
return response
@app.route('/news.json')
@cross_origin()
def news():
return jsonify(news=parse_news())
@app.route('/recent.json')
@cross_origin()
def recent():
return jsonify(threads=parse_recent())
if __name__ == '__main__':
app.run(debug=True)