From ab4ba9b75c696b2ab66ecd3a9d2a5e3f0811cb01 Mon Sep 17 00:00:00 2001 From: Ching Date: Thu, 28 Mar 2024 15:03:12 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=A2=9E=E5=8A=A0=20ff=20=E7=99=BB?= =?UTF-8?q?=E5=BD=95=E9=80=BB=E8=BE=91=20TUN-41?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- mas2ff2.py | 62 ++++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 60 insertions(+), 2 deletions(-) diff --git a/mas2ff2.py b/mas2ff2.py index 355551a..e12b04b 100644 --- a/mas2ff2.py +++ b/mas2ff2.py @@ -7,6 +7,7 @@ from datetime import datetime from loguru import logger import os import sentry_sdk +from bs4 import BeautifulSoup sentry_sdk.init( dsn="https://c42de79f364a12228f184bc54fec4dfd@o4506942768021504.ingest.us.sentry.io/4506982441615360", @@ -59,9 +60,10 @@ def post_fanfou(cookie, token, fanfou_status, images=None): response.status_code)) if not '你在做什么?' in response.text: logger.error('Post %s maybe failed', fanfou_status) + raise requests.exceptions.RequestException except requests.exceptions.RequestException: logger.error('Post %s failed', fanfou_status) - + raise requests.exceptions.RequestException else: # post with images image_urls = [media['url'] for media in images if media['type'] == 'image'] @@ -124,6 +126,7 @@ def post_fanfou(cookie, token, fanfou_status, images=None): response.status_code)) except requests.exceptions.RequestException: logger.error('Post %s failed', status) + sentry_sdk.capture_exception(requests.exceptions.RequestException) except: continue @@ -178,10 +181,65 @@ def run(instance, access_token, fanfou_cookie, fanfou_token): f.write(str(min_id) + '\n') min_id = None +def login_to_fanfou(email, password): + url = 'https://m.fanfou.com/home' + resp = requests.get(url) + soup = BeautifulSoup(resp.text, 'html.parser') + try: + token = soup.find('input', {'name': 'token'}).get('value') + except: + token = None + if not token: + logger.error('Get token failed') + return False + data = { + "loginname": email, + "loginpass": password, + "auto_login": "on", + "action": "login", + "token": token, + } + headers = { + "Content-Type": "application/x-www-form-urlencoded", + "Origin": "https://m.fanfou.com", + "Referer": "https://m.fanfou.com/home", + "Sec-Fetch-Dest": "document", + "Sec-Fetch-Mode": "navigate", + "Sec-Fetch-Site": "same-origin", + "Sec-Fetch-User": "?1", + "Upgrade-Insecure-Requests": "1", + "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/118.0.0.0 Safari/537.36", + } + resp = requests.post(url, data=data, headers=headers) + soup = BeautifulSoup(resp.text, 'html.parser') + if not '你在做什么?' in soup.text: + return False + cookie = resp.headers['Set-Cookie'] + token = None + if soup.find('input', {'name': 'token'}): + token = soup.find('input', {'name': 'token'}).get('value') + + with open(file_path + '/cookie.txt', 'w') as f: + f.write(cookie) + with open(file_path + '/token.txt', 'w') as f: + f.write(token) + return cookie, token + if __name__ == '__main__': cookie = 'u=stokesia; uuid=24edfe42401cbb03cf1f.1676446846.21; PHPSESSID=d4l9m3tp09gasff6r3u8mv9p12; m=yvettezhong%40gmail.com; tgw_l7_route=f174d6f255742a6ee2e9a07cf9cca0fa' token = '742ac481' + with open(file_path + '/cookie.txt', 'r') as f: + cookie = f.read() + with open(file_path + '/token.txt', 'r') as f: + token = f.read() m_ins = 'https://nofan.xyz' m_token = 'C5zKq2Mf54E7gaSDe47Bds080ySpsdvaykZBAtt5jvo' - run(m_ins, m_token, cookie, token) + email = '' + password = '' + try: + run(m_ins, m_token, cookie, token) + except Exception as e: + logger.error(e) + sentry_sdk.capture_exception(e) + login_to_fanfou(email, password)