-
Notifications
You must be signed in to change notification settings - Fork 2
/
netsarang.py
230 lines (192 loc) · 7.32 KB
/
netsarang.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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
#!/usr/bin/env python
# -*- encoding: utf-8 -*-
# @Author : bajins https://www.bajins.com
# @File : netsarang.py
# @Version: 1.0.0
# @Time : 2019/9/17 18:22
# @Project: scripts_python
# @Package:
# @Software: PyCharm
import base64
import json
import time
from datetime import datetime
import requests
from bs4 import BeautifulSoup
from selenium.webdriver.support import expected_conditions
from selenium.webdriver.support.wait import WebDriverWait
import mail
from utils import http_util, string_util, reptile_util, time_util
from utils.exception_util import MsgException
netsarang_info = {}
def send_mail_dp(mail: str, product: str):
"""
通过ChromeDP发送邮件
:param mail: 邮箱
:param product: 产品
:return:
"""
if product == "xshell":
url = "https://www.netsarang.com/zh/xshell-download"
elif product == "xftp":
url = "https://www.netsarang.com/zh/xftp-download"
elif product == "xlpd":
url = "https://www.netsarang.com/zh/Xlpd"
elif product == "xmanager":
url = "https://www.netsarang.com/zh/xmanager-download"
elif product == "xshellplus":
url = "https://www.netsarang.com/zh/xshell-plus-download"
elif product == "powersuite":
url = "https://www.netsarang.com/zh/xmanager-power-suite-download"
try:
driver = reptile_util.selenium_driver(url)
# 使用selenium通过id,name或class的方式来获取到这个input标签
# 查找元素,传入值(输入的内容)
driver.find_element_by_css_selector("input[name='user-name']").send_keys(mail.split("@")[0])
driver.find_element_by_css_selector("input[name='email']").send_keys(mail)
# 提交
driver.find_element_by_css_selector('input[value="开始试用"][type="submit"]').click()
# 显式等待设置一个等待时间,直到这个元素出现就停止等待,如果没出现就抛出异常
WebDriverWait(driver, 60).until(expected_conditions.title_contains(u"Thank You – 下载 – NetSarang Website"))
# 获取网页源代码
# innerText,textContent,innerHTML
html = driver.find_element_by_css_selector(".fusion-text h1").text
if "感谢您提交的下载我们软件的请求" not in html:
raise MsgException("邮件发送失败!")
finally:
# 关闭当前窗口。
driver.close()
# 关闭浏览器并关闭chreomedriver进程
driver.quit()
def get_url_dp(product):
"""
通过ChromeDP获取下载产品信息
:param product: 产品
:return:
"""
if product == "" or product is None:
raise MsgException("产品不能为空")
if netsarang_info is not None and netsarang_info and product in netsarang_info.keys():
info = netsarang_info[product]
# 如果数据不为空,并且日期为今天,这么做是为了避免消耗过多的性能,每天只查询一次
if len(info) > 1 and not info and time_util.date_compare_to(info[0], datetime.now()) == 0:
return info[1]
prefix = string_util.random_lowercase_alphanumeric(9)
suffix = mail.lin_shi_you_xiang_suffix()
mail.lin_shi_you_xiang_apply(prefix)
mail = prefix + suffix
send_mail_dp(mail, product)
time.sleep(10)
mail_list = mail.lin_shi_you_xiang_list(prefix)
mail_len = len(mail_list)
if mail_len == 0 or not mail_list:
raise MsgException("邮件列表为空!")
mailbox = mail_list[mail_len - 1]["mailbox"]
if mailbox is None or mailbox == "":
raise MsgException("邮件列表为空!")
mail_id = mail_list[mail_len - 1]["id"]
if mail_id is None or mail_id == "":
raise MsgException("邮件ID为空!")
# 获取最新一封邮件
mail_content = mail.lin_shi_you_xiang_get_mail(mailbox, mail_id)
# 解密,邮件协议Content-Transfer-Encoding指定了base64
html_text = base64.b64decode(mail_content.split("AmazonSES")[1])
# 解析解密后的HTML
html = BeautifulSoup(html_text, features="html.parser")
# 查找带token的标签的值
href = html.find("a", {"target": "_blank"}).text
# 请求token链接地址获取下载链接
# bs = ReptileUtil.selenium_bs(href)
# href = bs.find("a", {"target": "download_frame"})["href"]
try:
driver = reptile_util.selenium_driver(href)
driver.implicitly_wait(10)
href = driver.find_element_by_css_selector("a[target='download_frame']").get_attribute("href")
finally:
# 关闭当前窗口。
driver.close()
# 关闭浏览器并关闭chreomedriver进程
driver.quit()
href = href.replace(".exe", "r.exe")
# 把产品信息存储到变量
netsarang_info[product] = [datetime.now(), href]
return href
def send_mail(mail: str, product: str):
"""
使用requests发送邮件
:param mail:
:param product:
:return:
"""
product_code = ""
product_name = ""
if product == "xshell":
product_code = "4203"
product_name = "xshell-download"
elif product == "xftp":
product_code = "4242"
product_name = "xftp-download"
elif product == "xlpd":
product_code = "4280"
product_name = "xlpd-download"
elif product == "xmanager":
product_code = "4162"
product_name = "xmanager-download"
elif product == "xshellplus":
product_code = "4132"
product_name = "xshell-plus-download"
elif product == "powersuite":
product_code = "4066"
product_name = "xmanager-power-suite-download"
if product_code == "" or product_name == "":
raise MsgException("产品不匹配")
data = {
"_wpcf7": (None, "3016"),
"_wpcf7_version": (None, "5.1.1"),
"_wpcf7_locale": (None, "en_US"),
"_wpcf7_unit_tag": (None, f"wpcf7-f3016-p{product_code}-o2"),
"_wpcf7_container_post": (None, product_code),
"g-recaptcha-response": (None, ""),
"md": (None, "setDownload"),
"language": (None, "3"),
"downloadType": (None, "0"),
"licenseType": (None, "0"),
"action": (None, "/json/download/process.html"),
"user-name": (None, mail),
"email": (None, mail),
"company": (None, ""),
"product_name": (None, product_name),
}
res = requests.post("https://www.netsarang.com/json/download/process.html", data,
headers={"User-Agent": http_util.USER_AGENT}, verify=False, timeout=600)
res = json.load(res.text)
if not res or res["errorCounter"] != 0 or not res["result"]:
raise MsgException("邮箱发送失败!")
def get_url(lang: str, token: str):
"""
使用requests获取url
:param lang:
:param token:
:return:
"""
language = "2"
if lang == 'en':
language = '2'
if lang == 'ko':
language = '1'
if lang == 'zh':
language = '3'
if lang == 'ru':
language = '8'
if lang == 'pt':
language = '9'
data = {
'md': (None, 'checkDownload'),
'token': (None, token),
'language': (None, language)
}
res = requests.post("https://www.netsarang.com/json/download/process.html", data,
headers={"User-Agent": http_util.USER_AGENT}, verify=False, timeout=600)
return json.load(res.text)
if __name__ == '__main__':
print(get_url_dp("xshell"))