Skip to content

Instantly share code, notes, and snippets.

@windsting
Created November 26, 2019 05:55
Show Gist options
  • Save windsting/a4f20575568182d7996a1c4dae1ded50 to your computer and use it in GitHub Desktop.
Save windsting/a4f20575568182d7996a1c4dae1ded50 to your computer and use it in GitHub Desktop.
Discuz! forum onliner
#!/usr/bin/env python
#coding=utf-8
from config import *
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from collections import namedtuple
import os
import sys
import time
import traceback
import logging
Record = namedtuple('Record',['point','time'])
def getting_started():
driver = webdriver.Chrome('./chromedriver.exe')
driver.get("https://www.python.org")
print(driver.title)
search_bar = driver.find_element_by_name("q")
search_bar.clear()
search_bar.send_keys("getting started with python")
search_bar.send_keys(Keys.RETURN)
print(driver.current_url)
driver.close()
pass
def clear_scr():
if (os.name == 'nt'):
os.system("cls")
else:
os.system('clear')
def start_bot(page):
t = 0 # nap times
page_url = "http://www.gyhj.org/home.php?mod=spacecp&ac=credit&showcredit=1"
need_get = True
last_point = None
points = []
while True:
try:
# flush page
clear_scr()
print("page refreshing...", flush=True)
if need_get:
page.get(page_url)
else :
page.refresh()
page.implicitly_wait(30)
total_point = page.find_element_by_id('extcreditmenu').text
user_group = page.find_element_by_id('g_upmine').text
if total_point != last_point:
points.insert(0, Record(point=total_point,time=time.localtime()))
last_point = total_point
# print log
clear_scr()
total_seconds = t * nap_time
cur_time = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
print(f"已经挂机 {total_seconds} 秒, ", end='')
print(f"累计 {str(int(total_seconds / 60 / 60))}小时{str(int(total_seconds / 60 % 60))}分钟{str(int(total_seconds % 60))}秒")
print(f"当前 {total_point} , {user_group}", flush=True)
print(f"刷新间隔: {nap_time} 秒", flush=True)
print(f"最后刷新时刻: {cur_time}", flush=True)
print("")
print("积分变化记录:")
for rec in points:
rec_time = time.strftime("%Y-%m-%d %H:%M:%S", rec.time)
print(f" [{rec.point}] at {rec_time}")
# wait for next flush
t += 1
time.sleep(nap_time)
need_get = False
except Exception as e:
# inf page error
clear_scr()
print(e)
logging.error(traceback.format_exc())
print("web page error, gonna try after 60 seconds...", flush=True)
page.implicitly_wait(30)
need_get = True
time.sleep(60)
pass
def main(u, p):
# init
clear_scr()
print("initializing...", flush=True)
# get page
page = webdriver.Chrome('./chromedriver.exe')
page.set_window_size(1200, 800)
page.set_window_position(200, 100)
page.get("http://www.gyhj.org/forum.php?mod=forumdisplay&fid=50")
page.implicitly_wait(30)
try:
# Login
page.find_element_by_id('ls_username').send_keys(u)
page.find_element_by_id('ls_password').send_keys(p)
page.find_element_by_xpath('//*[@id="lsform"]/div/div/table/tbody/tr[2]/td[3]/button/em').click()
page.implicitly_wait(30)
except:
print("error occured in initializing, please try again later", flush=True)
return
print(page.title)
time.sleep(3)
start_bot(page)
pass
def help():
print(f"Usage:\n python app.py username password")
pass
test_sleep = 5
if __name__ == "__main__":
print(f"should call main({username}, {password})")
time.sleep(test_sleep)
main(username, password)
#coding=utf-8
username = "Your username"
password = "Password of username"
nap_time = 60 # secs

Discuz! onliner

This script was created to make a user of Discuz! forum(gyhj.org for now) stay online, with python3.

Usage

  1. Create a virtual environment, and activate it

  2. install webdriver follow the document, and put chromedriver.exe in the folder contain app.py

  3. install requirements:

     pip install -r requirements.txt
    
  4. edit config.py to set your username and password

  5. start scrpit with

     python app.py
    
astroid==2.3.3
colorama==0.4.1
isort==4.3.21
lazy-object-proxy==1.4.3
mccabe==0.6.1
pylint==2.4.4
selenium==3.141.0
six==1.13.0
typed-ast==1.4.0
urllib3==1.25.6
wrapt==1.11.2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment