-
Notifications
You must be signed in to change notification settings - Fork 0
/
codeChanServer.py
70 lines (60 loc) · 2.31 KB
/
codeChanServer.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
import os
import sys
import flask
import threadParse
import parseForWeb
import urllib2
from loadConfig import loadConfig
from htmlParse import stripTags
# make sure config file exists
try:
cfg = loadConfig()
except IOError, err:
print 'Error reading config file:', err
print ' (Maybe you didn\'t copy sampleConfig.yml to config.yml?)'
sys.exit()
# read yaml config: server devel mode, port
develMode = cfg['server']['develMode']
serverPort = cfg['server']['port']
analyticsEnabled = cfg['server']['analytics']
app = flask.Flask(__name__)
app.debug = develMode
boardList = [['a', 'c', 'w', 'm', 'cgl', 'cm', 'f', 'n', 'jp', 'vp'], ['v', 'vg', 'co', 'g', 'tv', 'k', 'o', 'an', 'tg', 'sp', 'sci', 'int'], ['i', 'po', 'p', 'ck', 'ic', 'wg', 'mu', 'fa', 'toy', '3', 'diy', 'wsg'], ['s', 'hc', 'hm', 'h', 'e', 'u', 'd', 'y', 't', 'hr', 'gif'], ['q', 'trv', 'fit', 'x', 'lit', 'adv', 'mlp'], ['b', 'r', 'r9k', 'pol', 'soc']]
@app.route('/')
def index():
if analyticsEnabled:
return flask.render_template('index_analytics.html', boards = boardList)
else:
return flask.render_template('index.html', boards = boardList)
@app.route('/favicon.ico')
def favicon():
return flask.send_from_directory(os.path.join(app.root_path, 'static'), 'favicon.ico', mimetype='image/vnd.microsoft.icon')
@app.route('/<board>/')
def selectBoard(board):
index = threadParse.Index(board)
try:
index.refresh()
except urllib2.HTTPError:
flask.abort(404)
processedIndex = parseForWeb.parseIndex(index)
if analyticsEnabled:
return flask.render_template('getBoard_analytics.html', boardAbbr = board, indexData = processedIndex)
else:
return flask.render_template('getBoard.html', boardAbbr = board, indexData = processedIndex)
@app.route('/<board>/<threadNum>/')
def selectThread(board, threadNum):
thread = threadParse.Thread()
thread.setBoard(board)
thread.setNum(threadNum)
try:
thread.refresh()
except urllib2.HTTPError:
flask.abort(404)
processedThread = parseForWeb.parseThread(thread)
return flask.render_template('getThread.html', boardAbbr = board, threadData = processedThread)
if __name__ == '__main__':
if develMode:
app.run(port = serverPort)
else: # production mode; serve on all interfaces
app.run(host = '0.0.0.0', port = serverPort)
app.add_url_rule('/favicon.ico', redirect_to = flask.url_for('static', filename='favicon.ico'))