吾爱破解 - LCG - LSG |安卓破解|病毒分析|www.52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 1811|回复: 41
收起左侧

[Python 原创] [Python] 52破解的附件上传小工具

[复制链接]
QAQ~QL 发表于 2024-2-29 17:33
本帖最后由 QAQ~QL 于 2024-3-5 09:28 编辑

52破解的附件上传

起因

2024春节题,本来用Typora自动picgo传上自己的七牛oss,发到论坛后发现很多地区都无法查看图片,遂手动使用论坛的图片功能传了将近50张图(4个帖子),并从html代码中提取了img的src网址,回到MD中一一修改了img的网址(真 ** 累啊)(后来发现自己是真傻啊,哈哈哈哈哈)

https://www.52pojie.cn/misc.php?mod=faq&action=faq&id=29&messageid=36

但是,由于上传图片返回的链接是临时链接,需要传到附件/图片点击附件,插入的[attachimg]xxx[/attachimg]才是永久链接,遂又重新折腾了一遍,改到头皮发麻,于是诞生了该代码

代码

环境  pip install requests_toolbelt requests beautifulsoup4

import random
import string
import xml.etree.ElementTree as ET
from bs4 import BeautifulSoup
from time import sleep

import requests
import re
import os

from requests_toolbelt import MultipartEncoder

# 需要获取的两个数据
# <input type="hidden" name="formhash" value="30c6d8fa">
# <input type="hidden" name="hash" value="0a0d2c087fa9143d3c1eab8e3f3bc138">

# 服务器上传限制    *.rar;*.7z;*.zip;*.txt;*.jpg;*.png

class WuAiPojie:

    # 初始化 ua:浏览器标识,cookie:登录cookie,fid:板块id
    def __init__(self, ua, cookie, fid=2):
        if len(ua) <= 0 | len(cookie) <= 0:
            raise Exception("ua和cookie不能为空")
        self.UA = ua
        self.Cookie = cookie
        self.fid = fid
        self.FormHash = ""
        self.Hash = ""
        self.uid = ""
        self.session = requests.session()
        self.session.headers = {
            "User-Agent": self.UA,
            "Cookie": self.Cookie,
            "Referer": "https://www.52pojie.cn/forum.php?mod=post&action=newthread&fid=2",
            "Origin": "https://www.52pojie.cn",
            "Accept": "*/*"
        }
        # self.session.verify = False
        self.isInit = self.init()

    def init(self):
        try:
            url = 'https://www.52pojie.cn/forum.php?mod=post&action=newthread&fid=' + str(self.fid)
            headers = {
                "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,*/*;q=0.8"
            }
            response = self.session.get(url, headers=headers)
            self.FormHash = re.findall(r'<input type="hidden" name="formhash" value="(.*?)"[\s/]*>', response.text)[0]
            self.Hash = re.findall(r'<input type="hidden" name="hash" value="(.*?)"[\s/]*>', response.text)[0]
            self.uid = re.findall(r'<input type="hidden" name="uid" value="(.*?)"[\s/]*>', response.text)[0]
            if len(self.FormHash) | len(self.Hash) | len(self.uid) <= 0:
                raise Exception("获取初始数据失败", response.text)
            # print("初始化成功", self.FormHash, self.Hash, self.uid)
            return True
        except Exception as e:
            raise Exception("获取初始数据失败", e)

    def __init_check__(self):
        if self.isInit is False:
            raise Exception("请先初始化")

    # 上传文件,返回   (附件id,文件名,插入到帖子的标签)
    def upload(self, file, name=""):
        self.__init_check__()
        # 获取文件名和文件类型
        _name, filetype, _type = self.__can_update__(file)
        if len(name) <= 0:
            name = _name

        url = 'https://www.52pojie.cn/misc.php?mod=swfupload&action=swfupload&operation=upload&fid=' + str(self.fid)
        multipart_encoder = MultipartEncoder(
            fields={
                'Filedata': (name, open(file, 'rb'), _type),
                'Filename': (None, name),
                'filetype': (None, filetype),
                'formhash': (None, self.FormHash),
                'uid': (None, self.uid),
                'hash': (None, self.Hash),
            },
            boundary='-----------------------------' + ''.join([random.choice(string.digits) for _ in range(29)])
        )
        response = self.session.post(url, data=multipart_encoder, headers={'Content-Type': multipart_encoder.content_type})
        attach_id = response.text
        prefix = "attach"
        if filetype == ".jpg" or filetype == ".png":
            prefix = "attachimg"
        return attach_id, name, "[{0}]{1}[/{2}]".format(prefix, attach_id, prefix)

    # 判断文件是否可以上传,并且返回文件类型和文件名
    def __can_update__(self, file):
        self.__init_check__()
        if not os.path.exists(file):
            raise Exception("文件[" + file + "]不存在")
        file_type = os.path.splitext(file)[1]
        name = os.path.basename(file)
        if file_type in [".rar", ".7z", ".zip", ".txt", ".jpg", ".png"]:
            if file_type == ".zip":
                _type = "application/x-zip-compressed"
            elif file_type == ".png":
                _type = "image/png"
            elif file_type == ".jpg":
                _type = "image/jpeg"
            elif file_type == ".7z" or file_type == ".rar":
                _type = "application/octet-stream"
            else:
                # elif file_type == ".txt":
                _type = "text/plain"
            return name, file_type, _type
        else:
            raise Exception("文件[" + file + "]类型不支持")

    # 删除附件  aid: [附件id_1,附件id_2...]  返回删除数量
    def delete(self, aid):
        self.__init_check__()
        # 经测试,多文件一起删会导致所有文件都没了
        # url = 'https://www.52pojie.cn/forum.php?mod=ajax&action=deleteattach&inajax=yes&formhash=' + self.FormHash + '&tid=0&pid=0&aids[]=' + '&aids[]='.join(aid)
        num = 0
        for a in aid:
            # 错误重试3次
            for i in range(3):
                try:
                    url = 'https://www.52pojie.cn/forum.php?mod=ajax&action=deleteattach&inajax=yes&formhash=' + self.FormHash + '&tid=0&pid=0&aids[]=' + a
                    response = self.session.get(url)
                    tree = ET.fromstring(response.text)
                    if tree.text != "1":
                        raise Exception("删除附件[" + a + "]失败")
                    num += 1
                    break
                except Exception as e:
                    print(e)
                finally:
                    self.__delay__()
        return num

    # 获取新帖的所有附件     返回附件列表 [(文件名,附件id,是否是图片),...]
    def getfiles(self):
        self.__init_check__()
        url = "https://www.52pojie.cn/forum.php?mod=ajax&action=attachlist&fid=2&inajax=1&ajaxtarget=attachlist"
        response = self.session.get(url)
        _list = []
        # 取出html
        tree = BeautifulSoup(ET.fromstring(response.text).text.split('<script')[0], 'html.parser')
        # 找class为xi2的a标签,获取文本
        for a in tree.find_all("a", class_='xi2'):
            _list.append((a.text, a.get('id').replace("attachname", ""), bool(a.get('isimage'))))
        return _list

    # 上传文件夹下的所有文件    [(附件id,文件名,插入到帖子的标签),...]
    def upload_folder(self, folder):
        self.__init_check__()
        rs = []
        for file_name in os.listdir(folder):
            # 错误重试3次
            for i in range(3):
                try:
                    _t = self.upload(os.path.join(folder, file_name))
                    rs.append(_t)
                    print(_t)
                    break
                except Exception as e:
                    print(e)
                finally:
                    self.__delay__()
        return rs

    # 接口有限流,慢慢来
    @staticmethod
    def __delay__():
        sleep(1)

if __name__ == '__main__':
    cookie = "填你自己的,记得到发帖的页面拿,切记使用抓包的cookie,不然会报错"
    ua = "填你自己的"

    # 初始化上传类
    t = WuAiPojie(ua, cookie)

    # 遍历当前 ./upload 目录下的文件上传
    t.upload_folder(os.path.abspath('') + os.path.sep + ".." + os.path.sep + "assets")

    # 删除多个附件
    # print(t.delete(["2678878","2678872"]))

    # 获取新帖的所有附件
    print(t.getfiles())

成功样例

1709192223583.png

GUI

环境  pip install PyQt5 pyqt5-tools

界面如下,代码参看附件

image-20240305084429987.png

image-20240305084438298.png

image-20240305092622176.png

52.7z (134.07 KB, 下载次数: 1)

免费评分

参与人数 11吾爱币 +16 热心值 +10 收起 理由
Diamondzl + 1 + 1 谢谢@Thanks!
苏紫方璇 + 7 + 1 欢迎分析讨论交流,吾爱破解论坛有你更精彩!
XY1993 + 1 + 1 谢谢@Thanks!
461735945 + 1 + 1 用心讨论,共获提升!
邪天冥 + 1 + 1 欢迎分析讨论交流,吾爱破解论坛有你更精彩!
局外人K + 1 + 1 我很赞同!
io10086 + 1 此为违规行为,请遵守论坛版规!
zz030696 + 1 + 1 我很赞同!
Y2pj + 1 + 1 谢谢@Thanks!
scoxx1 + 1 + 1 用心讨论,共获提升!
XaoTao + 1 欢迎分析讨论交流,吾爱破解论坛有你更精彩!

查看全部评分

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

 楼主| QAQ~QL 发表于 2024-2-29 17:38
依赖环境  
pip install requests_toolbelt requests beautifulsoup4
 楼主| QAQ~QL 发表于 2024-2-29 17:41
Hmily 发表于 2024-2-29 17:38
没看懂,这和网页上有有什么区别?如果附件没有使用插入帖子,也会被定期清理的。

先把一堆文件传附件,然后把原文章的img全部替换上传后的[attachimg]
再开新帖,点击使用,把改完的文章贴进去
还是得把GUI整出来,写个img替换表达式,这两天加工一下

点评

把你手动搞错的替换成[attachimg]?那这种工具太小众了可能。  详情 回复 发表于 2024-2-29 17:44
你是神 发表于 2024-2-29 17:37
wasonglili 发表于 2024-2-29 17:38

感谢分享
gy001715 发表于 2024-2-29 17:38
谢谢分享
Hmily 发表于 2024-2-29 17:38
没看懂,这和网页上有有什么区别?如果附件没有使用插入帖子,也会被定期清理的。
xins666 发表于 2024-2-29 17:39
感谢分享
happywff 发表于 2024-2-29 17:44
先复制保存
Hmily 发表于 2024-2-29 17:44
QAQ~QL 发表于 2024-2-29 17:41
先把一堆文件传附件,然后把原文章的img全部替换上传后的[attachimg]
再开新帖,点击使用,把改完的文章贴 ...

把你手动搞错的替换成[attachimg]?那这种工具太小众了可能。
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则 警告:本版块禁止灌水或回复与主题无关内容,违者重罚!

快速回复 收藏帖子 返回列表 搜索

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-4-29 06:58

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表