Skip to content

Commit

Permalink
Merge pull request #2 from gxitm/master
Browse files Browse the repository at this point in the history
添加本地转码api
  • Loading branch information
AragonSnow authored May 30, 2020
2 parents 7274821 + 8997dd5 commit ed3d4b6
Show file tree
Hide file tree
Showing 4 changed files with 186 additions and 3 deletions.
46 changes: 46 additions & 0 deletions web/handlers/about.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#!/usr/bin/env python
# -*- encoding: utf-8 -*-
# vim: set et sw=4 ts=4 sts=4 ff=unix fenc=utf8:
# Author: Binux<[email protected]>
# http://binux.me
# Created on 2014-08-08 21:06:02

import time
from base import *

import requests
import re

class AboutHandler(BaseHandler):
@tornado.web.addslash
def get(self):
user = self.current_user
tpls = []
url = "https://github.com/qiandao-today/templates/blob/master/about.md"

res = requests.get(url, verify=False)
if (res.status_code == 200):
content = res.content.decode(res.encoding, 'replace')
About_content = re.findall(r"<article([\w\W]+?)</article", content)[0]
tpls_temp = re.findall(r"tr>([\w\W]+?)</tr", About_content)

for cnt in range(1, len(tpls_temp)):
tpl_temp = re.findall(r"center\">(.+?)</td", tpls_temp[cnt])
apiurl = re.findall(r"href=\"(.+?)\"", tpl_temp[1])[0]
example = re.findall(r">(.+?)<", tpl_temp[5])[0]

tpls.append ({
"api":tpl_temp[0],
"url":apiurl,
"vars":tpl_temp[2],
"varsreq":tpl_temp[3],
"comments":tpl_temp[4],
"examples":example
})

self.render('about.html', tpls=tpls)

handlers = [
('/about/?', AboutHandler),
]

74 changes: 72 additions & 2 deletions web/handlers/util.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-

import re
import json
import datetime
import time
import urllib
import pytz
from base import *
from tornado import gen

import json
import re

class UtilDelayHandler(BaseHandler):
@gen.coroutine
Expand All @@ -18,6 +22,72 @@ def get(self, seconds):
yield gen.sleep(seconds)
self.write(u'delay %s second.' % seconds)

class TimeStampHandler(BaseHandler):
@gen.coroutine
def get(self):
Rtv = {}
try:
ts = self.get_argument("ts", "")
type = self.get_argument("form", "%Y-%m-%d %H:%M:%S")
cst_tz = pytz.timezone('Asia/Shanghai')

if not ts:
# 当前本机时间戳,本机时间和北京时间
Rtv[u"时间戳"] = int(time.time())
Rtv[u"本机时间"] = datetime.datetime.fromtimestamp(Rtv[u"时间戳"]).strftime(type)
Rtv[u"北京时间"] = datetime.datetime.fromtimestamp(Rtv[u"时间戳"], cst_tz).strftime(type)
else:
# 用户时间戳转北京时间
Rtv[u"时间戳"] = ts
Rtv[u"北京时间"] = datetime.datetime.fromtimestamp(int(ts), cst_tz).strftime(type)
Rtv[u"状态"] = "200"
except Exception as e:
Rtv[u"状态"] = str(e)

self.set_header('Content-Type', 'application/json; charset=UTF-8')
self.write(json.dumps(Rtv, ensure_ascii=False, indent=4))


class UniCodeHandler(BaseHandler):
@gen.coroutine
def get(self):
Rtv = {}
try:
content = self.get_argument("content", "")
tmp = ""
for cr in content:
if (cr=="u" or cr=="'"):
tmp = tmp + cr
continue
tmp = tmp + repr(cr).replace("u'", "").replace("'","").replace("\\\\", "\\")
Rtv[u"转换后"] = tmp.decode("unicode_escape")
print ("222222"+ Rtv[u"转换后"])
Rtv[u"状态"] = "200"
except Exception as e:
Rtv[u"状态"] = str(e)

self.set_header('Content-Type', 'application/json; charset=UTF-8')
self.write(json.dumps(Rtv, ensure_ascii=False, indent=4))


class UrlDecodeHandler(BaseHandler):
@gen.coroutine
def get(self):
Rtv = {}
try:
content = self.get_argument("content", "")
Rtv[u"转换后"] = urllib.unquote(content)
Rtv[u"状态"] = "200"
except Exception as e:
Rtv[u"状态"] = str(e)

self.set_header('Content-Type', 'application/json; charset=UTF-8')
self.write(json.dumps(Rtv, ensure_ascii=False, indent=4))


handlers = [
('/util/delay/(\d+)', UtilDelayHandler),
('/util/timestamp', TimeStampHandler),
('/util/unicode', UniCodeHandler),
('/util/urldecode', UrlDecodeHandler),
]
66 changes: 66 additions & 0 deletions web/tpl/about.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
{% extends "base.html" %}

{% block title %}关于{% endblock %}

{% block head %}
{{ super() }}
<link href="{{ static_url('my.css') }}" rel="stylesheet" type="text/css">
{% endblock %}

{% block body %}
{{ utils.header(current_user) }}

<section class="tpl">
<div class="container">
<h2>
常用Api
</h2>
<table class="table table-hover">
<thead>
<tr>
<th style="text-align:center">api</th>
<th style="text-align:center">地址</th>
<th style="text-align:center">参数</th>
<th style="text-align:center">参数是否必须</th>
<th style="text-align:center">说明</th>
<th style="text-align:center">用例</th>
</tr>
</thead>
<tbody>
{% for tpl in tpls %}
<tr>
<td style="text-align:center">{{ tpl.api }}</td>
<td style="text-align:center">{{ tpl.url }}</td>
<td style="text-align:center">{{ tpl.vars | safe }}</td>
<td style="text-align:center">{{ tpl.varsreq }}</td>
<td style="text-align:center">{{ tpl.comments | safe }}</td>
<td style="text-align:center">{{ tpl.examples | safe }}</td>
</tr>
{% endfor %}
</tbody>
</table>
<h2>
常用参数及过滤器
</h2>
<table class="table table-hover">
<thead>
<tr>
<th style="text-align:center">参数</th>
<th style="text-align:center">说明</th>
</tr>
</thead>
</table>
</div>
</section>

<script>
$(function() {
$('[data-toggle=popover]').popover({
trigger: 'hover'
});
})
</script>

{{ utils.modal_load() }}
{{ utils.apost() }}
{% endblock %}
3 changes: 2 additions & 1 deletion web/tpl/utils.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@
<h2 class="text-center">私有签到框架<sup>alpha</sup></h2>
<div class="buttons">
<a href="/" class="btn btn-default pull-left">首页</a>
<a href="/about" class="btn btn-default pull-left">常用API/过滤器</a>
{% if current_user %}
<a href="/logout" class="btn btn-default pull-right">登出</a>
{% else %}
<a href="/register" class="btn btn-default pull-right">注册</a>
{% endif %}

</div>
</div>
</header>
Expand Down

0 comments on commit ed3d4b6

Please sign in to comment.