|  | #!/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) |