-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsetup.py
67 lines (57 loc) · 2.56 KB
/
setup.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
from flask import g
import sqlite3
import urllib.request, json
DATABASE = './database.db'
def get_db():
db = getattr(g, '_database', None)
if db is None:
db = g._database = sqlite3.connect(DATABASE)
db.row_factory = sqlite3.Row
return db
# db_select() Takes the SQL statement and the connection to the database as paramaters.
def query_db(query, db, *args, **kwargs):
one = kwargs.get('one', False)
cursor = db.execute(query, (args))
rv = cursor.fetchall()
cursor.close()
db.commit()
return (rv[0] if rv else None) if one else rv
# db_update() Updates the information
def db_update(username):
result_list = []
if username != "":
link = "https://api.github.com/users/" + username + "/repos"
with urllib.request.urlopen(link) as url:
data = json.loads(url.read().decode())
for repo in data:
try:
tmp = dict()
tmp["name"] = repo["name"]
tmp["created"] = repo["created_at"].split("T")[0]
tmp["updated"] = repo["updated_at"].split("T")[0]
tmp["description"] = repo["description"]
tmp["website"] = repo["html_url"]
tmp["link"] = repo["html_url"]
tmp["githubid"] = repo["id"]
result_list.append(tmp)
except:
print("Maybe not a repo?")
print(repo)
return result_list
return []
# Find new repos from github and add them to the database
def update_insert(repos, userid):
oldIDsSqlite = query_db("SELECT githubid from projects where userid=?", get_db(), userid)
oldIDs = []
if len(oldIDsSqlite) != 0:
for i, oldid in enumerate(oldIDsSqlite):
oldIDs.append(oldid["githubid"])
for repo in repos:
if repo["githubid"] not in oldIDs:
sql = f"INSERT INTO projects (userid, githubid ,name, created, updated, description, website, link) VALUES(?, ?, ?, ?, ?, ?, ?, ?);"
query_db(sql, get_db(), userid, repo["githubid"], repo["name"], repo["created"], repo["updated"], repo["description"] ,repo["website"] ,repo["link"])
id = query_db("SELECT MAX(ID) FROM Projects", get_db(), one=True)[0]
sql = f"INSERT INTO posts (type, text, userid, projectid, date) VALUES(?, ?, ?, ?, ?);"
query_db(sql, get_db(),"created a new project", None, userid, id, repo["created"])
else:
print("Project already exists")