望指正demo的定位,有时候会抽风无法接受我的定位信息
1 #! /usr/bin/python3
2 # -*- coding:UTF-8 -*-
3 # time : 2019/5/20 20:53
4 # file : chuangyue.py
5 # By 卤蛋
6 from urllib.parse import quote
7 import datetime
8 import requests
9 import calendar
10 import sys
11 import time
12 import random
13 import urllib3
14 urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
15
16 request = requests.session()
17
18 d = datetime.datetime.now()
19
20 StateId = 3
21
22 index_html = __file__.replace("requestforCH.py", "") + "index.html"
23 index_img = __file__.replace("requestforCH.py", "") + "index.jpg"
24
25 UserAgent_list = [r"Mozilla/5.0 (Linux; Android 8.1.0; Redmi 6 Build/O11019; wv) " +
26 r"AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/71.0.3578.99 Mobile Safari/537.36",
27 r"Mozilla/5.0 (Linux; Android 4.1.1; Nexus 7 Build/JRO03D) AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1025.166 Safari/535.19",
28 r"Mozilla/5.0 (Linux; U; Android 4.0.4; en-gb; GT-I9300 Build/IMM76D) AppleWebKit/534.30 (KHTML, like Gecko) Version/4.0 Mobile Safari/534.30",
29 r"Mozilla/5.0 (Linux; U; Android 6.2; en-gb; GT-P1000 Build/FROYO) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 Mobile Safari/533.1"]
30
31 with open(__file__.replace("requestforCH.py", "") + r'cookie.txt', 'r') as file:
32 cookie = file.read()
33 cookies = {} # 初始化cookies字典变量
34 for line in cookie.split(';'): # 按照字符:进行划分读取
35 # 其设置为1就会把字符串拆分成2份
36 name, value = line.strip().split('=', 1)
37 cookies[name] = value # 为字典cookies添加内容
38
39
40 def UserAgent():
41 return UserAgent_list[random.randint(0, len(UserAgent_list)-1)]
42
43
44 def locationStateAdd(userid):
45 headers = {
46 "Content-Type": "application/x-www-form-urlencoded",
47 "Host": "sqg.cydgsx.com",
48 "Connection": "Keep-Alive",
49 "Accept-Encoding": "gzip",
50 "User-Agent": "okhttp/3.10.0",
51 }
52 data = {"params": str({"state": StateId, "user_id": userid})}
53 response = request.get(
54 url="https://sqg.cydgsx.com/locationStateAdd",
55 headers=headers,
56 data=data,
57 timeout=60,
58 verify=False)
59 return response.json()
60
61
62 def studentLocationAdd(userid):
63 headers = {
64 "Content-Type": "application/x-www-form-urlencoded",
65 "Host": "sqg.cydgsx.com",
66 "Connection": "Keep-Alive",
67 "Accept-Encoding": "gzip",
68 "User-Agent": r"okhttp/3.10.0",
69 }
70 data = {"params": '{"address":"中国广东省广州市海珠区","latitude":"23.09610289210092","longitude":"113.33666163412784","user_id":%s}' % userid}
71 response = request.post(
72 url="https://sqg.cydgsx.com/studentLocationAdd",
73 data=data,
74 headers=headers,
75 timeout=60,
76 verify=False)
77 print("定位:",response.text)
78 for key, value in response.json().items():
79 if value == "成功" and key == "meg":
80 return True
81 return False
82
83
84 def Index(url="https://sqg.cydgsx.com/m/s/log/Index"):
85 headers = {
86 "Host": "sqg.cydgsx.com",
87 "Connection": "keep-alive",
88 "Upgrade-Insecure-Requests": "1",
89 "User-Agent": UserAgent(),
90 "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8",
91 "Referer": url,
92 "Accept-Encoding": "gzip, deflate",
93 "Accept-Language": "zh-CN,en-US;q=0.9",
94 "X-Requested-With": "com.gcykj.boss",
95 }
96 temp_cookies = {"chref": url}
97 temp_cookies.update(cookies)
98 response = request.get(
99 url=url,
100 headers=headers,
101 cookies=temp_cookies,
102 timeout=60,
103 verify=False)
104 html = response.text.replace('href="', 'href="https://sqg.cydgsx.com').replace("'get','", "'get','https://sqg.cydgsx.com").replace(r'class="lazy" data-src="', 'class="lazy" data-src="https://sqg.cydgsx.com')
105 with open(index_html, "w", encoding='utf-8') as file:
106 file.write(html)
107 print(locationStateAdd(cookies["LoginUser_Id"].split("&")[0])) # 等待服务器添加定位
108 # studentLocationAdd(cookies["LoginUser_Id"].split("&")[0]) # 添加定位
109 return studentLocationAdd(cookies["LoginUser_Id"].split("&")[0]) # 添加定位
110
111
112 def day(string=d.strftime('%Y-%m-%d')):
113
114 if d.day == getMonthFirstDayAndLastDay()[1].day:
115 month(d.strftime('%Y-%m-%d') + "\n无人值守\n" + "每月小结")
116 elif d.weekday() == 6:
117 week(d.strftime('%Y-%m-%d') + "\n无人值守\n" + "每周周报")
118 headers = {
119 "Host": "sqg.cydgsx.com",
120 "User-Agent": UserAgent(),
121 "Referer": "https://sqg.cydgsx.com/m/s/log/wLog",
122 "Accept-Encoding": "gzip, deflate",
123 "Accept-Language": "zh-CN,en-US;q=0.9",
124 "Accept": "application/json",
125 "Origin": "https://sqg.cydgsx.com",
126 "X-Requested-With": "XMLHttpRequest",
127 "Connection": "keep-alive",
128 "Content-Type": "application/x-www-form-urlencoded"
129 }
130
131 data = {"InternStateId": StateId,
132 "interContent": quote(string, 'utf-8'),
133 "logImg": "",
134 "newlogImg": "",
135 "posAddress": "",
136 "posLong": "",
137 "posLati": ""}
138 response = request.post(
139 url="https://sqg.cydgsx.com/m/s/Log/SaveWriteLog",
140 data=data,
141 headers=headers,
142 cookies=cookies,
143 timeout=60,
144 verify=False)
145 for key, value in response.json().items():
146 if value == "成功" and key == "meg":
147 print("每日小结: ",response.json())
148 return True
149 return False
150
151
152 def week(string=d.strftime('%Y-%m-%d')):
153 if d.weekday() != 6:
154 return False
155 else:
156 headers = {
157 "Host": "sqg.cydgsx.com",
158 "Connection": "keep-alive",
159 "Accept": "application/json",
160 "Origin": "https://sqg.cydgsx.com",
161 "X-Requested-With": "XMLHttpRequest",
162 "User-Agent": UserAgent(),
163 "Content-Type": "application/x-www-form-urlencoded",
164 "Referer": "https://sqg.cydgsx.com/m/s/log/wWeekSmy?date={}".format(d.strftime('%Y-%m-%d')),
165 "Accept-Encoding": "gzip, deflate",
166 "Accept-Language": "zh-CN,en-US;q=0.9",
167 }
168 data = {
169 "summaryInfo": string,
170 "logImg": "",
171 "newlogImg": "",
172 "smyDate": quote(d.strftime('%Y/%m/%d'),
173 'utf-8').replace("2F05%",
174 "2F5%") + "+" + quote("0:00:00", 'utf-8'),
175 "summaryType": "周小结",
176 }
177 response = request.post(
178 url="https://sqg.cydgsx.com/m/s/Log/SaveSmyJson",
179 data=data,
180 headers=headers,
181 cookies=cookies,
182 timeout=60,
183 verify=False)
184
185 for key, value in response.json().items():
186 if value == "成功" and key == "meg":
187 print("每周小结: ",response.json())
188 return True
189 return False
190
191
192 def month(string=d.strftime('%Y-%m-%d')):
193 if d.day != getMonthFirstDayAndLastDay()[1].day:
194 return False
195 else:
196 headers = {
197 "Host": "sqg.cydgsx.com",
198 "Connection": "keep-alive",
199 "Accept": "application/json",
200 "Origin": "https://sqg.cydgsx.com",
201 "X-Requested-With": "XMLHttpRequest",
202 "User-Agent": UserAgent(),
203 "Content-Type": "application/x-www-form-urlencoded",
204 "Referer": "https://sqg.cydgsx.com/m/s/log/wMonthSmy?date={}".format(d.strftime('%Y-%m-%d')),
205 "Accept-Encoding": "gzip, deflate",
206 "Accept-Language": "zh-CN,en-US;q=0.9",
207 }
208
209 data = {
210 "summaryInfo": string,
211 "logImg": "",
212 "newlogImg": "",
213 "smyDate": quote(d.strftime('%Y/%m/%d'),
214 'utf-8').replace("%2F0", "%2F") + "+" + quote("22:32:00", 'utf-8'),
215 "summaryType": "月小结",
216 }
217 response = request.post(
218 url="https://sqg.cydgsx.com/m/s/Log/SaveSmyJson",
219 data=data,
220 headers=headers,
221 cookies=cookies,
222 timeout=60,
223 verify=False)
224
225 for key, value in response.json().items():
226 if value == "成功" and key == "meg":
227 print("每月小结: ",response.json())
228 return True
229 return False
230
231
232 def getMonthFirstDayAndLastDay(year=None, month=None):
233 if year:
234 year = int(year)
235 else:
236 year = datetime.date.today().year
237
238 if month:
239 month = int(month)
240 else:
241 month = datetime.date.today().month
242
243 firstDayWeekDay, monthRange = calendar.monthrange(year, month)
244
245 return [
246 datetime.date(
247 year=year,
248 month=month,
249 day=1),
250 datetime.date(
251 year=year,
252 month=month,
253 day=monthRange)]
254
255
256 def main(data=""):
257 def setLog(data):
258 if sys.argv[1] == "day":
259 return day(data + "每日日志")
260 elif sys.argv[1] == "week":
261 return week(data + "每周周报")
262 elif sys.argv[1] == "month":
263 return month(data + "每月小结")
264 if len(sys.argv) > 2:
265 return setLog("".join(sys.argv[2:]))
266 elif len(sys.argv) > 1:
267 return setLog(data)
268 else:
269 return day(data + "每日日志")
270
271
272 if __name__ == '__main__':
273 print("##"*20)
274 # Index("https://sqg.cydgsx.com/m/s/Home/Index")
275 if main(d.strftime('%Y-%m-%d') + "\n无人值守\n"):
276 print(d.strftime('%Y-%m-%d %H:%M:%S') + "\t成功写入日志")
277 print(Index("https://sqg.cydgsx.com/m/s/log/Index"))
278 else:
279 print(d.strftime('%Y-%m-%d %H:%M:%S') + "\n写入日志失败")
280 print("==" * 20 + "\n闯越自动写日志系统(更新版)\tBy:卤蛋 \n2019.05.20·05.31\n" + "==" * 20)
281 print("##"*20)