From 8bcbcea5580818661eebcd17190780feb6e5bb86 Mon Sep 17 00:00:00 2001 From: User Erdgeist Date: Wed, 30 Oct 2019 14:23:58 +0000 Subject: standardize config access --- rater.py | 34 +++++++++++++++++----------------- templates/index.html | 14 +++++++------- 2 files changed, 24 insertions(+), 24 deletions(-) diff --git a/rater.py b/rater.py index e1ed9c5..247c1d2 100755 --- a/rater.py +++ b/rater.py @@ -17,10 +17,10 @@ parser.add_argument("-c", "--config", help="Config file location", default="./co args = parser.parse_args() with open(args.config, mode="r", encoding="utf-8") as json_file: - config_data = json.load(json_file) + config = json.load(json_file) app = Flask(__name__) -app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///'+config_data.get('frab-conference')+'-'+config_data.get('track')+'.db' +app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///' + config['frab-conference'] + '-' + config['track'] + '.db' app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False app.config['SECRET_KEY'] = 'Silence is golden. Gerd Eist.' app.jinja_env.trim_blocks = True @@ -28,6 +28,8 @@ app.jinja_env.lstrip_blocks = True db = SQLAlchemy(app) +config['frab-conf-url'] = config['frab-url'] + config['frab-conference'] + class Event(db.Model): """An event as dumped from frab""" frab_id = db.Column(db.Integer, primary_key=True) @@ -53,7 +55,7 @@ class EventRating(db.Model): @app.route("/") def root(): events = Event.query.all() - return render_template('index.html', events=events, json=json, config=config_data, cat=config_data.get('categories')) + return render_template('index.html', events=events, json=json, config=config) @app.route('/api/ratings') def get_ratings(): @@ -111,23 +113,19 @@ def add_rating(eventid): db.session.commit() return jsonify({"result":"ok"}) -def fetch_talks(config): +def fetch_talks(): sess = requests.Session() - new_session_page = sess.get(config.get('frab-url')) + new_session_page = sess.get(config['frab-url']) tree = etree.HTML(new_session_page.text) auth_token = tree.xpath("//meta[@name='csrf-token']")[0].get("content") login_data = dict() - login_data['user[email]'] = config.get('frab-user') - login_data['user[password]'] = config.get('frab-password') + login_data['user[email]'] = config['frab-user'] + login_data['user[password]'] = config['frab-password'] login_data['user[remember_me]'] = 1 login_data['authenticity_token'] = auth_token - frab = config.get('frab-url') - conf = config.get('frab-conference') - track = config.get('track-name') - - sess.post(frab + 'users/sign_in?conference_acronym=' + conf + '&locale=en', login_data, verify=False) - response = sess.get(frab + 'en/'+conf+'/events?track_name=' + track + '&format=json', verify=False, stream=True) + sess.post(config['frab-url'] + 'users/sign_in?conference_acronym=' + config['frab-conference'] + '&locale=en', login_data, verify=False) + response = sess.get(config['frab-conf-url'] + '/events?track_name=' + config['track-name'] + '&format=json', verify=False, stream=True) talks_json = json.loads(response.text) @@ -137,7 +135,7 @@ def fetch_talks(config): imported = 0 for json_event in talks_json['events']: # print (json_event) - rawhtml = sess.get(frab + 'en/' + conf + '/events/'+ str(json_event['id']), verify=False, stream=True) + rawhtml = sess.get(config['frab-conf-url'] + '/events/'+ str(json_event['id']), verify=False, stream=True) tree = etree.HTML(rawhtml.text) submission_notes = tree.xpath('//b[text()="Submission Notes(user and admin):"]')[0].tail.strip() @@ -157,16 +155,18 @@ def fetch_talks(config): else: db.session.add( Event( frab_id = json_event['id'], title = json_event['title'], subtitle = json_event['subtitle'], abstract = json_event['abstract'], description = json_event['description'], speakers = json.dumps(speakers), state = json_event.get('state', 'new'), event_type = json_event['type'], notes = submission_notes) ) imported += 1 + for goner in Event.query.filter( Event.frab_id.notin_([ ev['id'] for ev in talks_json['events'] ])).all(): goner.state = 'gone' + db.session.commit() - print ('Conference: ' + conf + ', track: ' + track + ', imported ' + str(len(talks_json['events'])) + ' events, ' + str(imported) + ' new.') + print ('Conference: ' + config['track'] + ', track: ' + config['track'] + ', imported ' + str(len(talks_json['events'])) + ' events, ' + str(imported) + ' new.') if __name__ == "__main__": db.create_all() if args.frab_import: - fetch_talks(config_data) + fetch_talks() else: - app.run(host=config_data.get('host'), port=int(config_data.get('port'))) + app.run(host=config.get('host', '127.0.0.1'), port=int(config.get('port', '8080'))) diff --git a/templates/index.html b/templates/index.html index 13a8aec..c8ccf71 100644 --- a/templates/index.html +++ b/templates/index.html @@ -2,7 +2,7 @@ {{ config.get('frab-conference') }} {{ config.get('track') }} rating helper