def _request(fcode, tims=0): url = 'https://' + getconfig('dbweb', 'url') + '/' + fcode logging.info("request:" + url) try: res = opener().open(url, timeout=20) html = res.read() except Exception as e: return json(-1, '网络错误:' + str(e)) # tims += 1 # if tims == 3: # return json(-1, '网络错误') # return _request(fcode, tims) html = html.decode('UTF-8') title = _domatch(psTitle, html) starcode = _domatch(psStarCode, html) star = _domatch(psStar, html) imgsrc = _domatch(ptImg, html) filename = fcode + '.jpg' # os.path.splitext(imgsrc)[1] return json( 0, ',', { 'code': fcode, 'title': title, 'starcode': starcode, 'star': star, 'imgsrc': imgsrc, 'filename': filename })
def opener(): ssl._create_default_https_context = ssl._create_unverified_context proxy_handler = request.ProxyHandler({ 'http': getconfig('proxy', 'http'), 'https': getconfig('proxy', 'https') }) topener = request.build_opener(proxy_handler) opener.addheaders = [("authority", getconfig('dbweb', 'url'))] # opener.addheaders = [("method", "GET")] # opener.addheaders = [("path", "/HEYZO-0282")] # opener.addheaders = [("scheme", "https")] # opener.addheaders = [("accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8")] # opener.addheaders = [("dnt", "1")] # opener.addheaders = [("upgrade-insecure-requests", "1")] topener.addheaders = [( "user-agent", "Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.116 Safari/537.36" )] return topener
def index(request): requestdata = {} pageindex = 1 if 'pageindex' in request.query: pageindex = int(request.query['pageindex']) requestdata['pageindex'] = pageindex pagesize = 15 if 'pagesize' in request.query: pagesize = int(request.query['pagesize']) requestdata['pagesize'] = pagesize ps = fanhao.select() if 'downed' in request.query and request.query['downed']: downed = request.query['downed'] ps = ps.where(fanhao.downed == downed) requestdata['downed'] = downed if 'code' in request.query and request.query['code']: code = request.query['code'] ps = ps.where(fanhao.code == code) requestdata['code'] = code if 'ma' in request.query and request.query['ma']: ma = request.query['ma'] if ma == '0' or ma == '1' or ma == '2': ps = ps.where(fanhao.ima == ma) requestdata['ma'] = ma if 'star' in request.query and request.query['star']: star = request.query['star'] if star == '-1': star = '' ps = ps.where(fanhao.star == star) requestdata['star'] = star count = ps.count() allcode = fanhao.select(fanhao.code).order_by(fanhao.code.asc()) allstar = fanhao.select(fanhao.star).group_by(fanhao.star) ps = ps.order_by(fanhao.id.desc()).paginate(pageindex, pagesize) pager = Pager(count, pageindex, pagesize) html = render( 'index.html', { 'allcode': allcode, 'allstar': allstar, 'ps': ps, 'requestdata': requestdata, 'dbweb': getconfig('dbweb', 'url'), 'pagehtml': pager.render() }) db.close() return web.Response(body=bytes(html, encoding="utf-8"), content_type='text/html')
#!/usr/bin/env python3 # -*- coding: utf-8 -*- __author__ = 'SunCoder' import time from peewee import MySQLDatabase, Model, PrimaryKeyField, CharField, CharField, IntegerField from Base import getconfig db = MySQLDatabase(host=getconfig('db', 'host'), user=getconfig('db', 'user'), passwd=getconfig('db', 'passwd'), database=getconfig('db', 'database'), charset=getconfig('db', 'charset'), port=int(getconfig('db', 'port'))) class BaseModel(Model): class Meta: database = db class fanhao(BaseModel): id = PrimaryKeyField(primary_key=True) code = CharField(64) title = CharField(128) star = CharField(64, null=True) starcode = CharField(64, null=True) img = CharField(128, null=True) fname = CharField(128, null=True) ima = IntegerField(default=0)
rs.delete_instance() if os.path.exists(os.path.join(PHOTO_PATH, rs.code + '.jpg')): os.remove(os.path.join(PHOTO_PATH, rs.code + '.jpg')) db.close() return jsonres(0, 'ok', '删除成功') except fanhao.DoesNotExist: pass def initsys(): if not os.path.exists(PHOTO_PATH): os.mkdir(PHOTO_PATH) if not fanhao.table_exists(): fanhao.create_table() db.close() if __name__ == '__main__': initsys() app = web.Application(debug=True) app.add_routes([ web.route("*", '/', index), web.route("*", '/search', search), web.route("*", '/recode', recode), web.route("*", '/deimg', deimg), web.route("GET", '/set/{type}/{id}/{val}', set), web.static('/static', STATIC_PATH), web.static('/photos', PHOTO_PATH) ]) web.run_app(app, port=getconfig('web', 'port'))