forked from rahulbot/MediaCloud-Flask-Example
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mcserver.py
64 lines (47 loc) · 2.07 KB
/
mcserver.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
import ConfigParser, logging, datetime, os
from flask import Flask, render_template, request
import mediacloud
CONFIG_FILE = 'settings.config'
basedir = os.path.dirname(os.path.realpath(__file__))
# load the settings file
config = ConfigParser.ConfigParser()
config.read(os.path.join(basedir, 'settings.config'))
# set up logging
log_file_path = os.path.join(basedir,'logs','mcserver.log')
logging.basicConfig(filename=log_file_path,level=logging.DEBUG)
logging.info("Starting the MediaCloud example Flask app!")
# clean a mediacloud api client
mc = mediacloud.api.MediaCloud( config.get('mediacloud','api_key') )
app = Flask(__name__)
@app.route("/")
def home():
return render_template("search-form.html")
@app.route("/search",methods=['POST'])
def search_results():
keywords = request.form['keywords']
startyear = request.form['startyear']
startyear = int(startyear)
startmonth = request.form['startmonth']
startmonth = int(startmonth)
startday = request.form['startday']
startday = int(startday)
endyear = request.form['endyear']
endyear = int(endyear)
endmonth = request.form['endmonth']
endmonth = int(endmonth)
endday = request.form['endday']
endday = int(endday)
results = mc.sentenceCount(keywords,
solr_filter=[mc.publish_date_query( datetime.date( startyear, startmonth, startday),
datetime.date( endyear, endmonth, endday) ),
'media_sets_id:1' ] )
chartresults = mc.sentenceCount(keywords,
solr_filter=[mc.publish_date_query( datetime.date( startyear, startmonth, startday),
datetime.date( endyear, endmonth, endday) ),
'media_sets_id:1' ], split = 1)
chartresultsarray = chartresults.items()
return render_template("search-results.html",
keywords=keywords, sentenceCount=results['count'], chartresultsarray=chartresultsarray )
if __name__ == "__main__":
app.debug = True
app.run()