forked from rahulbot/GV-GetToKnow-flask
/
get-to-know.py
53 lines (42 loc) · 1.36 KB
/
get-to-know.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
from flask import Flask, render_template, g
import json
import feedparser
import globalvoices
import sqlite3
from tweets import country_tweets
from world_db import connect_db
from world_db import query_db
from world_db import DATABASE
app = Flask(__name__)
def connect_db():
return sqlite3.connect(DATABASE)
@app.route("/")
def index():
return render_template("stories.html",
country_list_json_text=json.dumps(globalvoices.country_list())
)
@app.route("/country/<country>")
def country(country):
stories = globalvoices.recent_stories_from( country )
tweet_list = country_tweets(country)
country_entry = query_db('select * from Country where Name = ?', [country], one=True)
language = query_db('select Language from CountryLanguage where CountryCode = ?', [country_entry['Code']], one=True)
return render_template("stories.html",
country_list_json_text=json.dumps(globalvoices.country_list()),
country_name=country,
stories=stories,
tweets=tweet_list,
language=language,
population=country_entry['Population']
)
return render_template("tweets.html", )
@app.before_request
def before_request():
g.db = connect_db()
@app.teardown_request
def teardown_request(exception):
if hasattr(g, 'db'):
g.db.close()
if __name__ == "__main__":
app.debug = True
app.run()