-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
128 lines (90 loc) · 2.6 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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
"""
Copyright Oz N Tiram <[email protected]>
This file is distributed under the terms of the
GNU AFFERO GENERAL PUBLIC LICENSE version 3.
"""
import datetime as dt
import json
import logging
import os
import time
from urllib.request import urlopen
import bottle
from peewee import SqliteDatabase
from bottle import request
from bottle_peewee import PeeweePlugin
from cachetools.func import ttl_cache
from peewee import Model, CharField, DateField
def get_logger(name):
logger = logging.getLogger(name)
logger.setLevel(logging.DEBUG)
fh = logging.StreamHandler()
fh.setLevel(logging.DEBUG)
# add fh to logger
logger.addHandler(fh)
return logger
logger = get_logger('pwman3')
db = SqliteDatabase(os.getenv("PRODUCTION_DB", 'all.db'))
db_plugin = PeeweePlugin(db)
app = application = bottle.Bottle()
class User(Model):
hashinfo = CharField()
os = CharField()
version = CharField()
date = DateField()
class Meta(object):
database = db
app.install(db)
with db:
db.create_tables([User], safe=True)
@ttl_cache(maxsize=2, ttl=3600, timer=time.time, typed=False)
def pypi_version():
"""check current version againt latest version"""
logger.debug(
"%s fetching ..." % dt.datetime.utcnow().strftime("%Y-%m-%d %H:%m"))
pypi_url = "https://pypi.org/pypi/pwman3/json"
try:
res = urlopen(pypi_url, timeout=0.5)
if res.status != 200:
logger.debug("res: %s" % res.status)
return 'x.x.x'
info = json.loads(res.read().decode())
return info['info']['version']
except Exception as E:
logger.warn("Exception: %s" % E)
return 'x.x.x'
@app.route('/')
def index():
return """
<!DOCTYPE html>
<head>
<meta charset="utf-8">
<title>Pwman3 Web</title>
<meta name="description" content="">
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<main>
</main>
<script type="text/javascript">
window.location.href="https://github.com/pwman3/pwman3";
</script>
</html>"""
@app.route('/static/<filename:path>')
def static(filename):
'''
Serve static files
'''
return bottle.static_file(filename, root='./static')
@app.route('/is_latest/')
def show_version():
'''
'''
hashinfo = request.GET.get("hash", "")
user_os = request.GET.get("os", "")
pwman_version = request.GET.get("current_version", "")
date = dt.date.today()
User.create(hashinfo=hashinfo, os=user_os, version=pwman_version,
date=date)
return pypi_version()
if __name__ == "__main__":
bottle.run(app)