Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrate code to Nodriver lib #38

Open
wants to merge 50 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
50 commits
Select commit Hold shift + click to select a range
3bd702c
Update requirements.txt
JiuZ-Chn Oct 4, 2024
4a73ea8
Update zodgame.py
JiuZ-Chn Oct 4, 2024
1686ca8
Update zodgame.py
JiuZ-Chn Oct 4, 2024
3a38bc3
Update zodgame.py
JiuZ-Chn Oct 4, 2024
43db9c8
Update zodgame.py
JiuZ-Chn Oct 4, 2024
492737a
Update zodgame.py
JiuZ-Chn Oct 4, 2024
2be5688
Update zodgame.py
JiuZ-Chn Oct 13, 2024
3278d05
Update zodgame.py
JiuZ-Chn Oct 13, 2024
9c75bb9
Update zodgame.py
JiuZ-Chn Oct 13, 2024
0c74028
Update zodgame.py
JiuZ-Chn Oct 13, 2024
f768f2c
Update zodgame.py
JiuZ-Chn Oct 13, 2024
f3a0a05
Update zodgame.py
JiuZ-Chn Oct 13, 2024
f5b6ea2
Update zodgame.py
JiuZ-Chn Oct 13, 2024
9761d4c
Update zodgame.py
JiuZ-Chn Oct 13, 2024
29e1c52
Update zodgame.py
JiuZ-Chn Oct 13, 2024
347a015
Update zodgame.py
JiuZ-Chn Oct 13, 2024
16cc263
Update zodgame.py
JiuZ-Chn Oct 13, 2024
04f6223
Update zodgame.py
JiuZ-Chn Oct 13, 2024
02cd982
Update zodgame.py
JiuZ-Chn Oct 13, 2024
f05ea76
Update zodgame.py
JiuZ-Chn Oct 13, 2024
fd7544d
Update zodgame.py
JiuZ-Chn Oct 13, 2024
53f7816
Update zodgame.py
JiuZ-Chn Oct 13, 2024
fc1f623
Update zodgame.py
JiuZ-Chn Oct 13, 2024
9682b6a
Update zodgame.py
JiuZ-Chn Oct 13, 2024
f8f4ad7
Update zodgame.yml
JiuZ-Chn Oct 13, 2024
cba505c
Update zodgame.yml
JiuZ-Chn Oct 13, 2024
1ec0757
Update zodgame.py
JiuZ-Chn Oct 13, 2024
a20d162
Update zodgame.yml
JiuZ-Chn Dec 8, 2024
2716298
Update zodgame.py
JiuZ-Chn Dec 8, 2024
9d060bf
Update zodgame.yml
JiuZ-Chn Dec 8, 2024
2c16242
Update zodgame.py
JiuZ-Chn Dec 8, 2024
ad9d5a5
Update zodgame.py
JiuZ-Chn Dec 8, 2024
3847cb0
Update requirements.txt
JiuZ-Chn Dec 8, 2024
5c930f5
Update zodgame.yml
JiuZ-Chn Dec 8, 2024
dc0cc49
Update zodgame.yml
JiuZ-Chn Dec 8, 2024
5b2d0cb
Update zodgame.py
JiuZ-Chn Dec 8, 2024
fa8c55a
Update zodgame.yml
JiuZ-Chn Dec 9, 2024
565fea2
Update zodgame.yml
JiuZ-Chn Dec 9, 2024
ab2b2ac
Update zodgame.yml
JiuZ-Chn Dec 9, 2024
9f2dd78
Update zodgame.yml
JiuZ-Chn Dec 9, 2024
ff5937c
Update zodgame.py
JiuZ-Chn Dec 9, 2024
24b027a
Create entry.py
JiuZ-Chn Dec 9, 2024
34a1c61
Update requirements.txt
JiuZ-Chn Dec 9, 2024
f1f211e
Update zodgame.yml
JiuZ-Chn Dec 9, 2024
f4a6608
Update requirements.txt
JiuZ-Chn Dec 9, 2024
5776acf
Update entry.py
JiuZ-Chn Dec 9, 2024
5563e7e
Delete zodgame/entry.py
JiuZ-Chn Dec 9, 2024
ff7e089
Update zodgame.yml
JiuZ-Chn Dec 9, 2024
df63582
Update zodgame.py
JiuZ-Chn Dec 9, 2024
1827161
Update requirements.txt
JiuZ-Chn Dec 9, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .github/workflows/zodgame.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ jobs:
runs-on: windows-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-python@v5
with:
python-version: "3.13"
- name: Install requirements
run: |
pip3 install -r ./zodgame/requirements.txt
Expand Down
2 changes: 1 addition & 1 deletion zodgame/requirements.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
undetected-chromedriver
nodriver
197 changes: 79 additions & 118 deletions zodgame/zodgame.py
Original file line number Diff line number Diff line change
@@ -1,105 +1,79 @@
# encoding=utf8
import io
import re
import sys
import platform
import subprocess
sys.stdout = io.TextIOWrapper(sys.stdout.buffer,encoding='utf-8')
import asyncio
import nodriver
from nodriver import cdp
sys.stdout.reconfigure(encoding='utf-8')

import undetected_chromedriver as uc
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
import requests
async def zodgame_checkin(tab):

def zodgame_checkin(driver, formhash):
checkin_url = "https://zodgame.xyz/plugin.php?id=dsu_paulsign:sign&operation=qiandao&infloat=1&inajax=0"
formhash = (await tab.find('//input[@name="formhash"]', timeout=180)).attrs["value"]

checkin_query = """
(function (){
var request = new XMLHttpRequest();
var fd = new FormData();
fd.append("formhash","%s");
fd.append("qdxq","kx");
request.open("POST","%s",false);
request.withCredentials=true;
request.send(fd);
return request;
(async function() {
const formdata = new FormData();
formdata.append("formhash","%s");
formdata.append("qdxq","kx");

const result = await fetch('%s', {
method: 'POST',
body: formdata
})
.then(response => {
return response.text();
})

return result;
})();
""" % (formhash, checkin_url)
checkin_query = checkin_query.replace("\n", "")
driver.set_script_timeout(240)
resp = driver.execute_script("return " + checkin_query)
match = re.search('<div class="c">\n(.*?)</div>\n', resp["response"], re.S)

resp = await tab.evaluate(checkin_query, await_promise=True, return_by_value=True)
match = re.search('<div class="c">\n(.*?)</div>\n', resp, re.S)
message = match[1] if match is not None else "签到失败"
print(f"【签到】{message}")

return "恭喜你签到成功!" in message or "您今日已经签到,请明天再来" in message

def zodgame_task(driver, formhash):

def clear_handles(driver, main_handle):
handles = driver.window_handles[:]
for handle in handles:
if handle != main_handle:
driver.switch_to.window(handle)
driver.close()
driver.switch_to.window(main_handle)

def show_task_reward(driver):
driver.get("https://zodgame.xyz/plugin.php?id=jnbux")
async def zodgame_task(broswer):

async def show_task_reward(broswer):
tab = await broswer.get("https://zodgame.xyz/plugin.php?id=jnbux", new_tab=True)
try:
WebDriverWait(driver, 240).until(
lambda x: x.title != "Just a moment..."
)
reward = driver.find_element(By.XPATH, '//li[contains(text(), "点币: ")]').get_attribute("textContent")[:-2]
print(f"【Log】{reward}")
reward_li = (await tab.find('//li[contains(text(), "点币: ")]', timeout=60))
print(f"【Log】{reward_li.text}")
except:
pass
pass

driver.get("https://zodgame.xyz/plugin.php?id=jnbux")
WebDriverWait(driver, 240).until(
lambda x: x.title != "Just a moment..."
)
jnbux_tab = await broswer.get("https://zodgame.xyz/plugin.php?id=jnbux")

try:
join_task = await jnbux_tab.find_all('//a[text()="参与任务"]')
except:
join_task = []
success = True

join_bux = driver.find_elements(By.XPATH, '//font[text()="开始参与任务"]')
if len(join_bux) != 0 :
driver.get(f"https://zodgame.xyz/plugin.php?id=jnbux:jnbux&do=join&formhash={formhash}")
WebDriverWait(driver, 240).until(
lambda x: x.title != "Just a moment..."
)
driver.get("https://zodgame.xyz/plugin.php?id=jnbux")
WebDriverWait(driver, 240).until(
lambda x: x.title != "Just a moment..."
)

join_task_a = driver.find_elements(By.XPATH, '//a[text()="参与任务"]')
success = True

if len(join_task_a) == 0:
if len(join_task) == 0:
print("【任务】所有任务均已完成。")
return success
handle = driver.current_window_handle
for idx, a in enumerate(join_task_a):
on_click = a.get_attribute("onclick")
#return success

for idx, a in enumerate(join_task):
print(a.attrs)
on_click = a.attrs["onclick"]
print(on_click)
#await a.click()
try:
function = re.search("""openNewWindow(.*?)\(\)""", on_click, re.S)[0]
script = driver.find_element(By.XPATH, f'//script[contains(text(), "{function}")]').get_attribute("text")
task_url = re.search("""window.open\("(.*)", "newwindow"\)""", script, re.S)[1]
driver.execute_script(f"""window.open("https://zodgame.xyz/{task_url}")""")
driver.switch_to.window(driver.window_handles[-1])
tab = broswer.tabs[-1]
try:
WebDriverWait(driver, 240).until(
lambda x: x.find_elements(By.XPATH, '//div[text()="成功!"]')
)
await tab.find('//div[text()="成功!"', timeout=240)
except:
print(f"【Log】任务 {idx+1} 广告页检查失败。")
pass

try:
check_url = re.search("""showWindow\('check', '(.*)'\);""", on_click, re.S)[1]
driver.get(f"https://zodgame.xyz/{check_url}")
WebDriverWait(driver, 240).until(
lambda x: len(x.find_elements(By.XPATH, '//p[contains(text(), "检查成功, 积分已经加入您的帐户中")]')) != 0
or x.title == "BUX广告点击赚积分 - ZodGame论坛 - Powered by Discuz!"
)
check_url = re.search("""showWindow('check', '(.*)');""", on_click, re.S)[1]
tab = await tab.get(f"https://zodgame.xyz/{check_url}")
await tab.find('//p[contains(text(), "检查成功, 积分已经加入您的帐户中")] | //title[text()="BUX广告点击赚积分 - ZodGame论坛 - Powered by Discuz!"]')
except:
print(f"【Log】任务 {idx+1} 确认页检查失败。")
pass
Expand All @@ -108,57 +82,44 @@ def show_task_reward(driver):
except Exception as e:
success = False
print(f"【任务】任务 {idx+1} 失败。", type(e))
finally:
clear_handles(driver, handle)

show_task_reward(driver)

await show_task_reward(broswer)

return success

def zodgame(cookie_string):
options = uc.ChromeOptions()
options.add_argument("--disable-popup-blocking")
options.add_argument("--headless")
driver = uc.Chrome(driver_executable_path = """C:\SeleniumWebDrivers\ChromeDriver\chromedriver.exe""",
browser_executable_path = """C:\Program Files\Google\Chrome\Application\chrome.exe""",
options = options)
async def zodgame(cookie_string):

# Load cookie
driver.get("https://zodgame.xyz/")
browser = await nodriver.start(
headless = False,
sandbox = False,
browser_executable_path = """C:\Program Files\Google\Chrome\Application\chrome.exe""",
browser_args = ["--no-sandbox"]
)
tab = await browser.get('https://zodgame.xyz')

# Log in with cookie string
if cookie_string.startswith("cookie:"):
cookie_string = cookie_string[len("cookie:"):]
cookie_string = cookie_string.replace("/","%2")
cookie_dict = [
{"name" : x.split('=')[0].strip(), "value": x.split('=')[1].strip()}
cookie_dict = [
{"name": x.split('=')[0].strip(), "value": x.split('=')[1].strip()}
for x in cookie_string.split(';')
]

driver.delete_all_cookies()
for cookie in cookie_dict:
if cookie["name"] in ["qhMq_2132_saltkey", "qhMq_2132_auth"]:
driver.add_cookie({
"domain": "zodgame.xyz",
"name": cookie["name"],
"value": cookie["value"],
"path": "/",
})

driver.get("https://zodgame.xyz/")

WebDriverWait(driver, 240).until(
lambda x: x.title != "Just a moment..."
)
assert len(driver.find_elements(By.XPATH, '//a[text()="用户名"]')) == 0, "Login fails. Please check your cookie."

formhash = driver.find_element(By.XPATH, '//input[@name="formhash"]').get_attribute('value')
assert zodgame_checkin(driver, formhash) and zodgame_task(driver, formhash), "Checkin failed or task failed."

driver.close()
driver.quit()
cookies = [
cdp.network.CookieParam(domain = 'zodgame.xyz', name = cookie["name"], value = cookie["value"], path = '/')
for cookie in cookie_dict if cookie["name"] in ["qhMq_2132_saltkey", "qhMq_2132_auth"]
]
#await browser.cookies.set_all(cookies)
await tab.send(cdp.storage.set_cookies(cookies))
await tab.reload()

task_manager = []
task_manager.append(await zodgame_checkin(tab))
task_manager.append(await zodgame_task(browser))
assert False in task_manager, "部分任务失败,请检查相关Log。"

if __name__ == "__main__":
if __name__ == '__main__':
cookie_string = sys.argv[1]
assert cookie_string

zodgame(cookie_string)
asyncio.run(zodgame(cookie_string))