Compare commits

...

2 Commits

Author SHA1 Message Date
Ching
ab4ba9b75c feat: 增加 ff 登录逻辑 TUN-41 2024-03-28 15:03:12 +08:00
Ching
d6230d1c8c refactor: Fix file path issue in mas2ff2.py 2024-03-28 11:41:58 +08:00

View File

@ -7,6 +7,7 @@ from datetime import datetime
from loguru import logger from loguru import logger
import os import os
import sentry_sdk import sentry_sdk
from bs4 import BeautifulSoup
sentry_sdk.init( sentry_sdk.init(
dsn="https://c42de79f364a12228f184bc54fec4dfd@o4506942768021504.ingest.us.sentry.io/4506982441615360", dsn="https://c42de79f364a12228f184bc54fec4dfd@o4506942768021504.ingest.us.sentry.io/4506982441615360",
@ -15,6 +16,9 @@ sentry_sdk.init(
# cookie = "u=%7EBVoC1S9tgw4; uuid=24edfe42401cbb03cf1f.1676446846.20; PHPSESSID=4ev7ef0f9cim18lrest8l52kn6; m=looo.ching%40gmail.com; tgw_l7_route=f174d6f255742a6ee2e9a07cf9cca0fa" # cookie = "u=%7EBVoC1S9tgw4; uuid=24edfe42401cbb03cf1f.1676446846.20; PHPSESSID=4ev7ef0f9cim18lrest8l52kn6; m=looo.ching%40gmail.com; tgw_l7_route=f174d6f255742a6ee2e9a07cf9cca0fa"
# token = "9927e23b" # token = "9927e23b"
logger.add('/root/develop/log/mas2ff-hegui.log', level='INFO') logger.add('/root/develop/log/mas2ff-hegui.log', level='INFO')
file_path = os.path.dirname(os.path.abspath(__file__))
def post_fanfou(cookie, token, fanfou_status, images=None): def post_fanfou(cookie, token, fanfou_status, images=None):
if not images: if not images:
images = [] images = []
@ -56,9 +60,10 @@ def post_fanfou(cookie, token, fanfou_status, images=None):
response.status_code)) response.status_code))
if not '你在做什么?' in response.text: if not '你在做什么?' in response.text:
logger.error('Post %s maybe failed', fanfou_status) logger.error('Post %s maybe failed', fanfou_status)
raise requests.exceptions.RequestException
except requests.exceptions.RequestException: except requests.exceptions.RequestException:
logger.error('Post %s failed', fanfou_status) logger.error('Post %s failed', fanfou_status)
raise requests.exceptions.RequestException
else: else:
# post with images # post with images
image_urls = [media['url'] for media in images if media['type'] == 'image'] image_urls = [media['url'] for media in images if media['type'] == 'image']
@ -94,15 +99,16 @@ def post_fanfou(cookie, token, fanfou_status, images=None):
status = fanfou_status + ' ' + image_count % (x+1) status = fanfou_status + ' ' + image_count % (x+1)
if image_resp.status_code == 200: if image_resp.status_code == 200:
# 创建一个临时文件 # 创建一个临时文件
with open('temp.jpg', 'wb') as file: temp_file = file_path + '/temp.jpg'
with open(temp_file, 'wb') as file:
file.write(response.content) file.write(response.content)
# 确保文件存在 # 确保文件存在
if os.path.exists('temp.jpg'): if os.path.exists(temp_file):
with open('temp.jpg', 'rb') as file: with open(temp_file, 'rb') as file:
# 以下是您的multipart/form-data请求部分 # 以下是您的multipart/form-data请求部分
files = { files = {
'picture': ('temp.jpg', file, 'image/jpeg') # 注意这里仍然需要指定MIME类型 'picture': (temp_file, file, 'image/jpeg') # 注意这里仍然需要指定MIME类型
} }
data = { data = {
'desc': status, 'desc': status,
@ -120,6 +126,7 @@ def post_fanfou(cookie, token, fanfou_status, images=None):
response.status_code)) response.status_code))
except requests.exceptions.RequestException: except requests.exceptions.RequestException:
logger.error('Post %s failed', status) logger.error('Post %s failed', status)
sentry_sdk.capture_exception(requests.exceptions.RequestException)
except: except:
continue continue
@ -134,7 +141,8 @@ def run(instance, access_token, fanfou_cookie, fanfou_token):
# first time run # first time run
posted_ids = [] posted_ids = []
# if file not exist, create it # if file not exist, create it
with open('last_id.txt', 'r') as f: last_id_file = file_path + '/last_id.txt'
with open(last_id_file, 'r') as f:
# read lines # read lines
posted_ids = f.readlines() posted_ids = f.readlines()
# remove \n # remove \n
@ -159,7 +167,7 @@ def run(instance, access_token, fanfou_cookie, fanfou_token):
content = re.sub('<.*?>', '', content) content = re.sub('<.*?>', '', content)
logger.info(status['id'] + ' ' + content) logger.info(status['id'] + ' ' + content)
post_fanfou(fanfou_cookie, fanfou_token, content, status['media_attachments']) post_fanfou(fanfou_cookie, fanfou_token, content, status['media_attachments'])
with open('last_id.txt', 'a') as f: with open(last_id_file, 'a') as f:
f.write(str(status['id']) + '\n') f.write(str(status['id']) + '\n')
posted_ids.append(status['id']) posted_ids.append(status['id'])
logger.info('write toot %s', status['content']) logger.info('write toot %s', status['content'])
@ -169,14 +177,69 @@ def run(instance, access_token, fanfou_cookie, fanfou_token):
min_id = statuses[-1]['id'] min_id = statuses[-1]['id']
else: else:
if min_id: if min_id:
with open('last_id.txt', 'a') as f: with open(last_id_file, 'a') as f:
f.write(str(min_id) + '\n') f.write(str(min_id) + '\n')
min_id = None 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__': if __name__ == '__main__':
cookie = 'u=stokesia; uuid=24edfe42401cbb03cf1f.1676446846.21; PHPSESSID=d4l9m3tp09gasff6r3u8mv9p12; m=yvettezhong%40gmail.com; tgw_l7_route=f174d6f255742a6ee2e9a07cf9cca0fa' cookie = 'u=stokesia; uuid=24edfe42401cbb03cf1f.1676446846.21; PHPSESSID=d4l9m3tp09gasff6r3u8mv9p12; m=yvettezhong%40gmail.com; tgw_l7_route=f174d6f255742a6ee2e9a07cf9cca0fa'
token = '742ac481' 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_ins = 'https://nofan.xyz'
m_token = 'C5zKq2Mf54E7gaSDe47Bds080ySpsdvaykZBAtt5jvo' 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)