Browse Source

update tester

pull/4/merge
Germey 7 years ago
parent
commit
5a17ac447a
  1. 1
      cookiespool/api.py
  2. 7
      cookiespool/generator.py
  3. 10
      cookiespool/scheduler.py
  4. 36
      cookiespool/tester.py
  5. 55
      login/weibo/cookies.py

1
cookiespool/api.py

@ -12,7 +12,6 @@ app = Flask(__name__)
def index(): def index():
return '<h2>Welcome to Cookie Pool System</h2>' return '<h2>Welcome to Cookie Pool System</h2>'
def get_conn(): def get_conn():
""" """
获取 获取

7
cookiespool/generator.py

@ -1,11 +1,6 @@
import json import json
import requests
from selenium import webdriver from selenium import webdriver
from selenium.common.exceptions import WebDriverException, TimeoutException
from selenium.webdriver import DesiredCapabilities from selenium.webdriver import DesiredCapabilities
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
from cookiespool.config import * from cookiespool.config import *
from cookiespool.db import RedisClient from cookiespool.db import RedisClient
from login.weibo.cookies import WeiboCookies from login.weibo.cookies import WeiboCookies
@ -85,6 +80,8 @@ class CookiesGenerator(object):
print('成功删除账号') print('成功删除账号')
else: else:
print(result.get('content')) print(result.get('content'))
else:
print('所有账号都已经成功获取Cookies')
def close(self): def close(self):
""" """

10
cookiespool/scheduler.py

@ -10,12 +10,12 @@ class Scheduler(object):
@staticmethod @staticmethod
def valid_cookie(cycle=CYCLE): def valid_cookie(cycle=CYCLE):
while True: while True:
print('Checking Cookies') print('Cookies检测进程开始运行')
try: try:
for name, cls in TESTER_MAP.items(): for name, cls in TESTER_MAP.items():
tester = eval(cls + '(name="' + name + '")') tester = eval(cls + '(name="' + name + '")')
tester.run() tester.run()
print('Tester Finished') print('Cookies检测完成')
del tester del tester
time.sleep(cycle) time.sleep(cycle)
except Exception as e: except Exception as e:
@ -24,20 +24,20 @@ class Scheduler(object):
@staticmethod @staticmethod
def generate_cookie(cycle=CYCLE): def generate_cookie(cycle=CYCLE):
while True: while True:
print('Generating Cookies') print('Cookies生成进程开始运行')
try: try:
for name, cls in GENERATOR_MAP.items(): for name, cls in GENERATOR_MAP.items():
generator = eval(cls + '(name="' + name + '")') generator = eval(cls + '(name="' + name + '")')
generator.run() generator.run()
print('Generator Finished') print('Cookies生成完成')
generator.close() generator.close()
print('Deleted Generator')
time.sleep(cycle) time.sleep(cycle)
except Exception as e: except Exception as e:
print(e.args) print(e.args)
@staticmethod @staticmethod
def api(): def api():
print('API接口开始运行')
app.run(host=API_HOST, port=API_PORT) app.run(host=API_HOST, port=API_PORT)
def run(self): def run(self):

36
cookiespool/tester.py

@ -10,42 +10,38 @@ class ValidTester(object):
self.cookies_db = RedisClient('cookies', self.name) self.cookies_db = RedisClient('cookies', self.name)
self.account_db = RedisClient('accounts', self.name) self.account_db = RedisClient('accounts', self.name)
def test(self, account, cookies): def test(self, username, cookies):
raise NotImplementedError raise NotImplementedError
def run(self): def run(self):
accounts = self.cookies_db.all() cookies_groups = self.cookies_db.all()
print(accounts) for username, cookies in cookies_groups.items():
for account in accounts: self.test(username, cookies)
username = account.get('username')
cookies = self.cookies_db.get(username)
self.test(account, cookies)
class WeiboValidTester(ValidTester): class WeiboValidTester(ValidTester):
def __init__(self, name='weibo'): def __init__(self, name='weibo'):
ValidTester.__init__(self, name) ValidTester.__init__(self, name)
def test(self, account, cookies): def test(self, username, cookies):
print('Testing Account', account.get('username')) print('正在测试Cookies', '用户名', username)
try: try:
cookies = json.loads(cookies) cookies = json.loads(cookies)
except TypeError: except TypeError:
print('Invalid Cookies Value', account.get('username')) print('Cookies不合法', username)
self.cookies_db.delete(account.get('username')) self.cookies_db.delete(username)
print('Deleted User', account.get('username')) print('删除Cookies', username)
return None return
try: try:
test_url = TEST_URL_MAP[self.name] test_url = TEST_URL_MAP[self.name]
response = requests.get(test_url, cookies=cookies, timeout=5, allow_redirects=False) response = requests.get(test_url, cookies=cookies, timeout=5, allow_redirects=False)
if response.status_code == 200: if response.status_code == 200:
print('Valid Cookies', account.get('username')) print('Cookies有效', username)
print('部分测试结果', response.text[0:50])
else: else:
print(response.status_code, response.headers) print(response.status_code, response.headers)
print('Invalid Cookies', account.get('username')) print('Cookies失效', username)
self.cookies_db.delete(account.get('username')) self.cookies_db.delete(username)
print('Deleted User', account.get('username')) print('删除Cookies', username)
except ConnectionError as e: except ConnectionError as e:
print('Error', e.args) print('发生异常', e.args)
print('Invalid Cookies', account.get('username'))

55
login/weibo/cookies.py

@ -1,8 +1,6 @@
import os
import time import time
from io import BytesIO from io import BytesIO
from PIL import Image from PIL import Image
from selenium import webdriver
from selenium.common.exceptions import TimeoutException from selenium.common.exceptions import TimeoutException
from selenium.webdriver import ActionChains from selenium.webdriver import ActionChains
from selenium.webdriver.common.by import By from selenium.webdriver.common.by import By
@ -158,31 +156,34 @@ class WeiboCookies():
:return: :return:
""" """
# 获得四个按点 # 获得四个按点
circles = self.browser.find_elements_by_css_selector('.patt-wrap .patt-circ') try:
dx = dy = 0 circles = self.browser.find_elements_by_css_selector('.patt-wrap .patt-circ')
for index in range(4): dx = dy = 0
circle = circles[numbers[index] - 1] for index in range(4):
# 如果是第一次循环 circle = circles[numbers[index] - 1]
if index == 0: # 如果是第一次循环
# 点击第一个按点 if index == 0:
ActionChains(self.browser) \ # 点击第一个按点
.move_to_element_with_offset(circle, circle.size['width'] / 2, circle.size['height'] / 2) \ ActionChains(self.browser) \
.click_and_hold().perform() .move_to_element_with_offset(circle, circle.size['width'] / 2, circle.size['height'] / 2) \
else: .click_and_hold().perform()
# 小幅移动次数 else:
times = 30 # 小幅移动次数
# 拖动 times = 30
for i in range(times): # 拖动
ActionChains(self.browser).move_by_offset(dx / times, dy / times).perform() for i in range(times):
time.sleep(1 / times) ActionChains(self.browser).move_by_offset(dx / times, dy / times).perform()
# 如果是最后一次循环 time.sleep(1 / times)
if index == 3: # 如果是最后一次循环
# 松开鼠标 if index == 3:
ActionChains(self.browser).release().perform() # 松开鼠标
else: ActionChains(self.browser).release().perform()
# 计算下一次偏移 else:
dx = circles[numbers[index + 1] - 1].location['x'] - circle.location['x'] # 计算下一次偏移
dy = circles[numbers[index + 1] - 1].location['y'] - circle.location['y'] dx = circles[numbers[index + 1] - 1].location['x'] - circle.location['x']
dy = circles[numbers[index + 1] - 1].location['y'] - circle.location['y']
except:
return False
def get_cookies(self): def get_cookies(self):
""" """

Loading…
Cancel
Save