-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
63 lines (48 loc) · 1.76 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
from flask import Flask, jsonify, request
from flask_cors import CORS, cross_origin
import datetime
import json
import requests
import sqlite3
def list_to_dict(list):
return {'name': list[0], 'updated': list[1], 'url': list[2]}
app = Flask(__name__)
CORS(app)
@app.route('/')
def default():
return '<a href="https://bme-notes.github.io">https://bme-notes.github.io</a>'
@app.route('/update/', methods=['POST'])
def update_note():
data = request.get_json(force=True)
if 'release' not in data or 'repository' not in data:
return 'Invalid request!'
if len(data['release']['assets']) == 0:
return 'No release!'
name = data['repository']['name']
updated = data['release']['published_at']
url = data['release']['assets'][0]['browser_download_url']
db_file = "database.sqlite"
conn = sqlite3.connect(db_file)
cur = conn.cursor()
rowcount = cur.execute("SELECT count(name) FROM subjects WHERE name = ?", (name,)).fetchone()[0]
if rowcount == 1:
cur.execute("UPDATE subjects SET updated = datetime(?), url = ? WHERE name = ?", (updated, url, name,))
else:
cur.execute("INSERT INTO subjects VALUES(?, ?, ?)", (name, updated, url,))
conn.commit()
conn.commit()
return 'Successful update!'
@app.route('/update-times/', methods=['GET'])
def list_notes():
db_file = "database.sqlite"
conn = sqlite3.connect(db_file)
cur = conn.cursor()
cur.execute("SELECT name, datetime(updated) AS updated, url FROM subjects")
subject_list = cur.fetchall()
result_list = []
for entry in subject_list:
result_list.append(list_to_dict(entry))
conn.close()
return jsonify(result_list)
if __name__ == "__main__":
app.run(debug=False, use_reloader=True, host='0.0.0.0')