-
Notifications
You must be signed in to change notification settings - Fork 0
/
webservice.py
80 lines (62 loc) · 1.84 KB
/
webservice.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
71
72
73
74
75
76
77
78
79
80
from bottle import route, run, template, request, static_file,get
import json
import sqlite3 as sq
@route('/')
def index():
return static_file('index.html', root='public')
@get("/items/<source_id:int>")
def source_items(source_id:int):
# get source items
conn = sq.connect('rss.db')
c = conn.cursor()
c.execute("""SELECT sources.name,items.title,items.url,items.content,items.date FROM sources,items WHERE sources.id = items.source_id AND sources.id = ?""", (source_id,))
source_name = c.fetchone()[0][0]
items = c.fetchall()
ret = {
"source": source_name,
"items": []
}
for item in items:
title = item[1]
url = item[2]
content = item[3]
published = item[4]
item_data = {
"title": title,
"url": url,
"content": content,
"published": published,
}
ret['items'].append(item_data)
c.close()
conn.close()
return json.dumps(ret, indent=2)
@get("/sources")
def get_sources():
conn = sq.connect('rss.db')
c = conn.cursor()
c.execute("SELECT id,name,url,category_id FROM sources")
sources = c.fetchall()
ret = {
"sources": []
}
# get category name
for i, source in enumerate(sources):
print(f"{source=}")
name = source[1]
url = source[2]
id = source[0]
category_id = source[3]
c.execute("SELECT name FROM categories WHERE id = ?", (category_id,))
category = c.fetchone()[0]
source_data = {
"id": id,
"name": name,
"url": url,
"category": category
}
ret['sources'].append(source_data)
c.close()
conn.close()
return json.dumps(ret, indent=2)
run(host='localhost', port=8080, debug=True,reloader=True)