二维图像FFT鲽形变换推导

复习DIP的时候想要搜索二位FFT蝶形运算的博客,发现百度搜不到,只能去啃老师的课堂板书,(字不太好看),总结如下

希望过两天考试能考好。

XJTU图书馆抢座位脚本--requests库

起因

图书馆逐渐严格,早上总不能每天六点起来预约吧,写脚本势在必行

关键点

上次那个web driver很不靠谱,速度慢还非内存,么的灵魂,这次直接request了,麻烦就麻烦点吧。感谢宗练61何咕咕同学的ehall登陆脚本,我参考ehall的认证写掉了图书馆的两层认证。

下面说关键点

前端加密

这里参考何咕咕的代码,对称加密,前端扒出来的。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18

def encrypt_pwd(raw_pwd, publicKey='0725@pwdorgopenp'):
''' AES-ECB encrypt '''
publicKey = publicKey.encode('utf-8')
# pkcs7 padding
BS = AES.block_size
pad = lambda s: s + (BS - len(s) % BS) * chr(BS - len(s) % BS)
pwd = pad(raw_pwd)
# zero padding
'''
pwd = raw_pwd
while len(raw_pwd.encode('utf-8')) % 16 != 0:
pwd += '\0'
'''
cipher = AES.new(publicKey, AES.MODE_ECB)
pwd = cipher.encrypt(pwd.encode('utf-8'))
return str(base64.b64encode(pwd), encoding='utf-8')

不加密也行,直接把自己通信时候发送的报抓住就行,反正加密是前端的。测试过,可行。

XJTU CAS认证

鬼知道这里我搞了多久,几乎一半的时间都在搞这里的跳转,没有任何技术,就是要理清认证的思路,三层还是四次跳转就完事,请求的接口均是通信中抓的。

1
2
3
4
5
6
7
8
9
10
11

url = 'https://org.xjtu.edu.cn/openplatform/g/admin/login'
cookie = {
'cur_appId_':'JL4oKidbLpQ='
}
data = {
"loginType": 1,
"username": self.config['username'],
"pwd": encrypt_pwd(self.config['password']),
"jcaptchaCode": ""
}

这里的appid是图书馆预约的id,如果同学要用其他的应用,这里必须改,因为我用何咕咕的代码直接就登录到ehall了。

中间的通信有些过程我是直接按照自己的账号邮箱写死了,其他账号可能需要改一改

预定座位的crsf_token

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20

def reserve(self,kid,sp):
r = self.session.get('http://rg.lib.xjtu.edu.cn:8010/ruguan')#入关
#print('--------------------------')
nn = r.text.find('<input id="csrf_token" name="csrf_token" type="hidden" value=')
tok = r.text[nn+1+len('<input id="csrf_token" name="csrf_token" type="hidden" value='):nn+56+len('<input id="csrf_token" name="csrf_token" type="hidden" value=')]
#print(r.text[nn+1+len('<input id="csrf_token" name="csrf_token" type="hidden" value='):nn+56+len('<input id="csrf_token" name="csrf_token" type="hidden" value=')])
data={
'csrf_token':tok,
'csrf_token':tok,
'service':'seat',
'submit':'%E6%8F%90%E4%BA%A4',
'rplace':'east'
}#入馆post,比较麻烦
r = self.session.post('http://rg.lib.xjtu.edu.cn:8010/ruguan',data = data)#提交

r = self.session.get('http://rg.lib.xjtu.edu.cn:8010/seat/?kid='+kid+'&sp='+sp)
#print(r.text)
return r.status_code

比较离谱,我直接字符串查找了,不会正则表达式,emmm,这个token是预约时候独立的,很不错。

结尾

终于花了两天时间吧XJTU的CAS搞清楚了,之后也可以去写更多的脚本了,之前许多脚本都是卡在了登录认证这里。

效果还不错,至少可以去图书馆了,使用方法,早上六点给个定时任务运行抢座,中午睡起来脚本捡漏,有人离开就直接抢过来,美滋滋。

全部代码

test.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
#!/usr/bin/env python3
import os
import time
import json
import base64
import requests
from Crypto.Cipher import AES
import re

class XJTUUser(object):

def __init__(self, config_file_path='./config.json'):
with open(config_file_path) as config_file:
config = json.loads(config_file.read())
self.config = config
self.is_login = False
self.session = requests.Session()
self.session.headers.update(config['headers'])
self.session.cookies.update(config['cookies'])

def login(self):
def reserve(self,kid,sp):
r = self.session.get('http://rg.lib.xjtu.edu.cn:8010/ruguan')#入关
#print('--------------------------')
nn = r.text.find('<input id="csrf_token" name="csrf_token" type="hidden" value=')
tok = r.text[nn+1+len('<input id="csrf_token" name="csrf_token" type="hidden" value='):nn+56+len('<input id="csrf_token" name="csrf_token" type="hidden" value=')]
#print(r.text[nn+1+len('<input id="csrf_token" name="csrf_token" type="hidden" value='):nn+56+len('<input id="csrf_token" name="csrf_token" type="hidden" value=')])
data={
'csrf_token':tok,
'csrf_token':tok,
'service':'seat',
'submit':'%E6%8F%90%E4%BA%A4',
'rplace':'east'
}#入关post,比较麻烦
r = self.session.post('http://rg.lib.xjtu.edu.cn:8010/ruguan',data = data)#提交

r = self.session.get('http://rg.lib.xjtu.edu.cn:8010/seat/?kid='+kid+'&sp='+sp)
#print(r.text)
return r.status_code
def encrypt_pwd(raw_pwd, publicKey='0725@pwdorgopenp'):
''' AES-ECB encrypt '''
publicKey = publicKey.encode('utf-8')
# pkcs7 padding
BS = AES.block_size
pad = lambda s: s + (BS - len(s) % BS) * chr(BS - len(s) % BS)
pwd = pad(raw_pwd)
# zero padding
'''
pwd = raw_pwd
while len(raw_pwd.encode('utf-8')) % 16 != 0:
pwd += '\0'
'''
cipher = AES.new(publicKey, AES.MODE_ECB)
pwd = cipher.encrypt(pwd.encode('utf-8'))
return str(base64.b64encode(pwd), encoding='utf-8')

_headers = self.config['headers']
_headers['Content-Type'] = 'application/x-www-form-urlencoded'

# start with 302 redirection from ehall
_r = self.session.get('http://rg.lib.xjtu.edu.cn:8010/auth/login/?next=%2Fseat%2F')

# get cookie route
self.session.get('https://org.xjtu.edu.cn/openplatform/login.html')

# get JcaptchaCode and cookie JSESSIONID & sid_code
r_JcaptchaCode = self.session.post('https://org.xjtu.edu.cn/openplatform/g/admin/getJcaptchaCode',
headers=_headers)

# is_JcaptchaCode_show
url = 'https://org.xjtu.edu.cn/openplatform/g/admin/getIsShowJcaptchaCode'
params = {
'userName': self.config['username'],
'_': str(int(time.time() * 1000))
}
r = self.session.get(url, params=params, headers=_headers)
print(r.text)
# login
url = 'https://org.xjtu.edu.cn/openplatform/g/admin/login'
cookie = {
'cur_appId_':'JL4oKidbLpQ='
}
data = {
"loginType": 1,
"username": self.config['username'],
"pwd": encrypt_pwd(self.config['password']),
"jcaptchaCode": ""
}
_headers['Content-Type'] = 'application/json;charset=UTF-8'
r = self.session.post(url, data=json.dumps(data), headers=_headers,cookies=cookie)
print(r.text)
token = json.loads(r.text)['data']['tokenKey']

cookie = {
'cur_appId_':'JL4oKidbLpQ=',
'open_Platform_User' : token
}
r=self.session.get('http://org.xjtu.edu.cn/openplatform/oauth/auth/getRedirectUrl?userType=1&personNo=2176112723&_=1590998261976',cookies = cookie)
print(r.text)
r=self.session.get(json.loads(r.text)['data'])


r=self.session.get('http://rg.lib.xjtu.edu.cn:8080/bxusr/link.jsp?uid=gwy867718012&cn=%E9%83%AD%E7%8E%8B%E6%87%BF&employeeNumber=2176112723&depId=%E7%94%B5%E5%AD%90%E4%B8%8E%E4%BF%A1%E6%81%AF%E5%AD%A6%E9%83%A8&email=867718012@qq.com&mobile')
#这一行可能要改。。。。。。

#---------------------登陆成功------------------------
r_w3=self.session.get('http://rg.lib.xjtu.edu.cn:8010/qseat?sp=west3B')#兴庆区的,别的区得自行修改了
r_e3=self.session.get('http://rg.lib.xjtu.edu.cn:8010/qseat?sp=east3A')
#print('各层座位数量',end = '')
#print(json.loads(r_e3.text)['scount'])
s1 = json.loads(r_e3.text)['seat']
s2 = json.loads(r_w3.text)['seat']
#s.update(json.loads(r_w3.text)['seat'])/seat/?kid=015&sp=north4southwest
#print(s1,s2)




while(1):
for i in s1:
if s1[i] == 0:
print(i)
if reserve(self,i,'east3A') == 200:
print('三楼东侧侧你的座位号是'+i)
exit(0)
for i in s2:
if s2[i] == 0:
print(i)
if reserve(self,i,'west3B') == 200:
print('三楼西侧你的座位号是'+i)
exit(0)
time.sleep(2)


if __name__ == '__main__':
mixcro = XJTUUser()
mixcro.login()

config.json

1
2
3
4
5
6
7
8
{
"username": "",
"password": "",
"headers": {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.130 Safari/537.36"
},
"cookies": {}
}

selenium的webdriver来自动填报每日健康日报

起因

健康平台每天要求填报两次,每次还有时间限制,一次不填就成黄🐎了,写一个脚本,这里偷懒了,懒得去弄cas登陆那一套跳转,就直接懒狗selenium去弄了。

关键点

安装selenium

首先是安装selenium,windows的话有chrome就问题不大,在设置里面看看自己的版本,然后去http://chromedriver.storage.googleapis.com/index.html下载相应的驱动

这里下载下来后我看网上说放到C盘谷歌服务文件夹地下,不太行,我最后是放在了python的PATH地下,anaconda的话就是根目录的Script文件夹下,直接把可执行文件放进去就行,然后使用以下代码测试,如果没有报错说明驱动安装成功了

1
2
3
4
from selenium import webdriver
option = webdriver.ChromeOptions()
option.add_argument('headless')
driver=webdriver.Chrome(chrome_options=option)

Linux版本的安装就搜教程即可,问题不大

延时的必要性

个人感觉这个是因为selenium本质是调用谷歌浏览器进行模拟,所以必定会有中间跳转的延时。

在登陆跳转的时候,延时是必须的,不然搜索不到下一个的界面。

跳转时的页面切换

1
2
3
4
windows = driver.window_handles

driver.switch_to.window(windows[-1])
print('当前页面title',driver.title)

这个可以检测是否跳转成功,有时候图形化界面看到的不一定是程序拿到的页面,这里相当于一个队列,直接跳转到最后一个,想要跳转回去就往前移。

iframe问题

iframe真是一个神奇的东西,我在浏览器复制好xpath,结果代码执行找不到,原因是用了iframe,这个相当于另外的一个htm,需要进行跳转

driver.switch_to.frame(driver.find_element_by_xpath("/html/body/div[2]/div[2]/table/tbody/tr/td[2]/div[2]/div/iframe"))

这里使用这个进行切换,xpath是新的iframe的xpath,这样就可以切换到iframe内部的htm了,然后填表,填完之后切换回去。

driver.switch_to.default_content()

代码

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
import time
import os
from selenium import webdriver
#先安装pywin32,才能导入下面两个包
import win32api
import win32con
#导入处理alert所需要的包
from selenium.common.exceptions import NoAlertPresentException
import traceback


option = webdriver.ChromeOptions()
option.add_argument('headless')
#这里是重点,增加一个参数即可实现在不打开浏览器的情况下完成系列操作

driver=webdriver.Chrome(chrome_options=option) # 选择Chrome浏览器
driver.get('http://one2020.xjtu.edu.cn/EIP/nonlogin/user/index.htm') # 打开网站

#driver.maximize_window() #最大化谷歌浏览器
driver.find_element_by_link_text('登录 / SIGN IN').click()
time.sleep(2)
windows = driver.window_handles

driver.switch_to.window(windows[-1])
print('当前页面title',driver.title)
driver.find_element_by_xpath('//*[@id="form1"]/input[1]').click() # 点击用户名输入框
driver.find_element_by_xpath('//*[@id="form1"]/input[1]').clear() #清空输入框
driver.find_element_by_xpath('//*[@id="form1"]/input[1]').send_keys('your_netid') # 自动敲入用户名

driver.find_element_by_name('pwd').click() # 点击密码输入框
driver.find_element_by_name('pwd').clear() #清空输入框
driver.find_element_by_name('pwd').send_keys('your_pwd') # 自动敲入密码

driver.find_element_by_xpath('//*[@id="account_login"]').click()


time.sleep(2)

#print('当前页面title',driver.page_source)
driver.get('http://one2020.xjtu.edu.cn/EIP/cooperative/openCooperative.htm?flowId=4af591a971bc5d460171e279f192078d')
time.sleep(2)
windows = driver.window_handles
driver.switch_to.window(windows[-1])

#time.sleep(2)
#driver.find_element_by_id('sendBtn').click()
#print('当前页面title',driver.page_source)
driver.switch_to.frame(driver.find_element_by_xpath("/html/body/div[2]/div[2]/table/tbody/tr/td[2]/div[2]/div/iframe"))

driver.find_element_by_xpath('/html/body/div/div/span[2]/div/table/tbody/tr/td/div[1]/div[3]/input').click()
driver.find_element_by_xpath('/html/body/div/div/table/tbody/tr[13]/td[2]/span/span[1]/input').click() # 点击用户名输入框
driver.find_element_by_xpath('/html/body/div/div/table/tbody/tr[13]/td[2]/span/span[1]/input').clear() #清空输入框
driver.find_element_by_xpath('/html/body/div/div/table/tbody/tr[13]/td[2]/span/span[1]/input').send_keys('36.5') # 自动敲入用户名
driver.switch_to.default_content()
driver.find_element_by_id('sendBtn').click()

driver.find_element_by_xpath('/html/body/div[5]/div/div[2]/div[2]/div[2]/a[1]/span').click()
driver.quit()
exit(0)


结尾

想起来很简单的脚本实现起来还是遇到了一定问题,动手实践才可以更好的了解web架构,学校这一套系统跟我之前见过的htm+php还真的不一样,跳转连接复制出来都是void(0),填报日报的通信路由还是bp抓包分析来的,可能大型项目现在都这样写了吧。

参考

一个随机禁言引起的算法问题

起因

小小功能里面最火爆的竟然是禁言套餐,群友疯狂索要禁言套餐,玩起了赌博

问题来了,现在的时间是9点,如果老罗每当禁言解禁就去申请,问老罗在十二点之前学习的概率。

题目总结一下:

给定三个正整数a,b,c, 当禁言时间大于a时,老罗立刻去学习。b为随即禁言的范围,每次申请禁言,随机范围为[1,b],取值为整数c为距离十二点的时间。现求老罗在十二点之前能去学习的概率。

个人思路

目测是动态规划,因为时间取值是正整数,这就自然的构成了不同的状态(不过如果取值不是整数的话,那就太难算了)。还有一个假设,每一次请求的随机数完全随机,每次请求独立,构成n重伯努利实验。根据这两个假设,写了一个递推式。

这里写错了,应该是从i+10开始到i+100,不过思路一样,debug时候改过去了。懒得改了。

1
2
3
4
5
6
7
8
9
10
11
study_time,top_time,rest_time = 10,100,180
# 小于等于10分钟则学习,禁言封顶100,还剩180分钟到12点。
dp = [0 for i in range(rest_time)]
dp[-10:] = [1-study_time/top_time]*10
for i in range(rest_time-10-1,-1,-1):
if(180<i+100):
dp[i] += (i+100-180-1)/100#超界,不学习的概率是1,加权加上去
for j in range(i+10,min(i+101,180),1):
dp[i] += 0.01*dp[j]
print(dp)
print("学习概率",1-dp[0])

python代码写了些,算出来结果是33.1%

智慧的群友1

宗濂61某个万能的医生用了个牛逼方法,random来模拟,并且模拟了很多次,一看就是老统计学派概率论了

1
2
3
4
5
6
7
8
9
10
11
12
13
import random

for raw_tl in range(180, 10, -1):
sp = 0
for i in range(1000000):
tl = raw_tl
while tl >= 0:
ct = random.randint(1, 100)
tl = tl-ct
if ct<10 and tl>0:
sp += 1
break
print('%s %s:%s %s' % (raw_tl, sp, 1000000-sp, sp/10000))

智慧的群友2

能动A某大佬直接就上数学了,概率估算,扔掉后面的余项。

算出也是29%左右

总结

群友的方法算出来都是29%左右,我那个递推算出来是33%,差距挺大,不过我找不出问题,希望朱军可以指导我一下,实在是debug不来了。

  • Copyrights © 2015-2023 galaxy
  • Visitors: | Views:

请我喝杯咖啡吧~

支付宝
微信