-
Notifications
You must be signed in to change notification settings - Fork 5
/
utility.py
155 lines (131 loc) · 4.85 KB
/
utility.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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# -*- coding: utf-8 -*-
import re, time, datetime, json, logging, pymysql
import requests
from pixivpy3 import *
from config import *
if DEBUG and DEBUG_SHOW_REQUEST_DETAIL:
import httplib as http_client
http_client.HTTPConnection.debuglevel = 1
logging.basicConfig()
logging.getLogger().setLevel(logging.DEBUG)
requests_log = logging.getLogger("requests.packages.urllib3")
requests_log.setLevel(logging.DEBUG)
requests_log.propagate = True
def FormatTime(time_original, format_new = '%a, %d %b %Y %H:%M:%S +0900'):
date = datetime.datetime.strptime(time_original, '%Y-%m-%dT%H:%M:%S+09:00')
return date.strftime(format_new)
def GetCurrentTime():
return time.strftime('%a, %d %b %Y %H:%M:%S +0800', time.localtime(time.time()))
def Get(url):
headers = {
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
'Accept-Language': 'zh-CN,zh;q=0.8',
'Accept-Charset': 'UTF-8,*;q=0.8',
'Accept-Encoding': 'gzip, deflate, sdch',
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.98 Safari/537.36'
}
# 防止海外访问weibo变英文版
cookies = {
'lang': 'zh-cn',
'SUB': 'Af3TZPWScES9bnItTjr2Ahd5zd6Niw2rzxab0hB4mX3uLwL2MikEk1FZIrAi5RvgAfCWhPyBL4jbuHRggucLT4hUQowTTAZ0ta7TYSBaNttSmZr6c7UIFYgtxRirRyJ6Ww%3D%3D',
'UV5PAGE': 'usr512_114',
'UV5': 'usrmdins311164'
}
debug('[Network] new http request: get ' + url)
try:
r = requests.get(url, headers = headers, cookies = cookies, timeout = 6)
debug('[Network] response status code: %s' % r.status_code)
except Exception as e:
log('unable to get %s, error message:' % url)
log(e)
return False
# 判断返回内容是不是纯文本
if 'text/html' in r.headers['Content-Type']:
return r.text
else:
return r.content
# 有报错时发一个提醒给我,不然这玩意儿挂了真是注意不到
def Notify(message):
requests.post(PUSHOVER_API, data={
'token': PUSHOVER_APP,
'user': PUSHOVER_USER,
'message': message,
})
__LOG_LEVEL = 0
def SetLogLevel(delta):
global __LOG_LEVEL
__LOG_LEVEL += delta
# DEBUG
def debug(message):
global DEBUG
if DEBUG:
print(__LOG_LEVEL * ' ' + message)
def log(pixiv_id, message = None):
if not message:
message = pixiv_id
pixiv_id = -1
try:
f = open(os.path.join(LOG_PATH, time.strftime('%Y-%m-%d.log', time.localtime(time.time()))), 'a+')
except:
f = open(os.path.join(LOG_PATH, time.strftime('%Y-%m-%d.log', time.localtime(time.time()))), 'w+')
finally:
debug(message)
f.write('%s %s, %s\n' % (time.strftime('[%H:%M:%S] ',time.localtime(time.time())), pixiv_id, message))
f.close()
Notify(message)
# 数据库操作
class DB:
# 构造函数时连接数据库
def __init__(self):
try:
self._ = pymysql.connect( CONFIG['DB_HOST'], CONFIG['DB_USER'], CONFIG['DB_PASS'], CONFIG['DB_NAME'], charset="utf8" )
self.c = self._.cursor( pymysql.cursors.DictCursor ) # 使fetchall的返回值为带key的字典形式
except Exception as e:
log(-1, '数据库连接出错 : %s' % e)
exit(1)
# 析构时关闭数据库
def __del__(self):
self.c.close()
self._.close()
# 查询
def Query(self, sql, data = None):
try:
if data:
self.c.execute(sql, data)
else :
self.c.execute(sql)
return self.c.fetchall()
except Exception as e:
log('Error in DB Query')
log(str(e))
return False
# 执行
def Run(self, sql, data):
try:
self.c.execute(sql, data)
return self._.insert_id()
except Exception as e:
log('Error in DB execute')
log(str(e))
return False
class ExtendedPixivPy(AppPixivAPI):
'''扩展ppy'''
# 实例化的时候自动从本地文件读取token
def __init__(self):
debug('Init ppy class')
super(self.__class__, self).__init__()
# load token
try:
f = open(TOKEN_FILE, 'r')
tokens = json.load(f)
f.close()
self.access_token = tokens['access_token']
self.refresh_token = tokens['refresh_token']
debug('Local token loaded')
except BaseException as err:
log('Failed to load access_token from file')
log(str(err))
# 不知道为什么ppy用的ranking name和p站原生的不一致,在illust_ranking里自动转一下
def illust_ranking(self, rank_name):
ppyName = MODE[rank_name]['ppyName']
return super(self.__class__, self).illust_ranking(ppyName)