-
Notifications
You must be signed in to change notification settings - Fork 1
/
ck_v2ex.py
109 lines (94 loc) · 4.27 KB
/
ck_v2ex.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
# -*- coding: utf-8 -*-
"""
cron: 10 11,14 * * *
new Env('V2EX 签到');
"""
import re
from utils import get_data
from notify_mtr import send
from datetime import datetime
from selenium import webdriver
from selenium_stealth import stealth
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import NoSuchElementException, TimeoutException, WebDriverException
class V2ex:
def __init__(self, check_items):
self.check_items = check_items
@staticmethod
def sign(cookie, i):
options = webdriver.ChromeOptions()
options.add_argument('--headless')
options.add_argument('--no-sandbox')
options.add_argument("start-maximized")
options.add_argument('--disable-dev-shm-usage')
service = webdriver.ChromeService(
log_output='/tmp/v2ex.log',
executable_path='/usr/bin/chromedriver',
service_args=['--readable-timestamp']
)
driver = webdriver.Chrome(service=service, options=options)
stealth(driver,
platform="Win32",
fix_hairline=True,
vendor="Google Inc.",
languages=["zh-CN", "zh"],
webgl_vendor="Intel Inc.",
renderer="Intel Iris OpenGL Engine",
)
res = ''
try:
driver.get('https://www.v2ex.com/signin')
for single_cookie in cookie.split('; '):
name, value = single_cookie.split('=', 1)
driver.add_cookie({'name': name, 'value': value})
driver.get('https://www.v2ex.com/mission/daily')
if '注册' in driver.page_source:
return f'账号({i})无法登录!可能Cookie失效,请重新修改'
sign_button = WebDriverWait(driver, 10).until(
EC.element_to_be_clickable((By.CSS_SELECTOR, 'input[type="button"]'))
)
name_element = driver.find_element(By.XPATH, '//span[@class="bigger"]')
msg = f"---- {name_element.text} V2EX 签到状态 ----\n"
res = f"{msg}<b><span style='color: green'>今天已经签到过了</span></b>"
if '领取 X 铜币' in sign_button.get_attribute('value'):
sign_button.click()
res = f"{msg}<b><span style='color: green'>签到成功</span></b>"
money_button = WebDriverWait(driver, 10).until(
EC.element_to_be_clickable((By.CSS_SELECTOR, "input[value='查看我的账户余额']"))
)
cell = re.findall(r'>(已连续登录.*?天)<', driver.page_source)[0]
money_button.click()
formatted_date = datetime.now().strftime('%Y%m%d')
gray = re.findall(f'{formatted_date}.*?(每日登录奖励.*?)</span>', driver.page_source)
money = driver.find_element(By.CSS_SELECTOR, "#money .balance_area").text.replace('\n', '').strip()
res += f"\n{cell}\n{gray[0]}\n当前账户余额:{money} 铜币"
except TimeoutException as e:
res = f"{msg}<b><span style='color: red'>超时异常:</span></b>\n{e}"
except NoSuchElementException as e:
res = f"{msg}<b><span style='color: red'>签到失败:</span></b>\n{e}"
except WebDriverException as e:
res = f"{msg}<b><span style='color: red'>WebDriver异常:</span></b>\n{e}"
except Exception as e:
res = f"{msg}<b><span style='color: red'>未知异常:</span></b>\n{e}"
finally:
# driver.get('https://bot.sannysoft.com/')
# total_height = driver.execute_script("return document.body.scrollHeight")
# driver.set_window_size(1920, total_height)
# driver.save_screenshot('/tmp/screenshot.png')
driver.quit()
return res
def main(self):
msg_all = ""
for i, check_item in enumerate(self.check_items, start=1):
cookie = check_item.get("cookie")
msg = self.sign(cookie, i)
msg_all += msg + "\n\n"
return msg_all
if __name__ == "__main__":
_data = get_data()
_check_items = _data.get("V2EX", [])
result = V2ex(check_items=_check_items).main()
send("V2EX", result)
# print(result)