对Solar图床的代码复刻
前些日子在逛论坛发现有大佬对picshak图床做了可视化软件可供上传,链接如下https://www.52pojie.cn/thread-1812672-1-1.html
然后本人对这个很感兴趣,于是写了代码进行复刻这个项目
温馨提示:本文需要一定的Python基础进行开发,不是成品文件,如果是小白,请访问上面的链接直接下载大佬的成品软件
由于我比较擅长于Python,因此这个代码也是使用Python写的,也顺便对代码进行讲解,
import json
import requests
import os
from mimetypes import guess_type
def upload_file(file_to_upload, headers, cookies):
url = "https://picshack.net/upload"
response = requests.post(url, headers=headers, files=, cookies=cookies)
return response
print("1: (默认选项)")
print("2: 一次性账号")
config_choice = input("请选择上传配置: ")
if config_choice == '1':
config_file_name = 'config_20_1.json'
elif config_choice == '2':
config_file_name = 'config_1.json'
elif config_choice == '':
config_file_name = 'config_20_1.json'
else:
print("无效的选择。请重新运行程序并输入有效的选择。")
exit()
with open(config_file_name, 'r') as config_file:
config = json.load(config_file)
headers = config['headers']
cookies = config['cookies']
max_files_per_batch = config.get('max_files_per_batch', 20)
folder_path = input("请输入文件夹路径: ")
file_paths = [os.path.join(folder_path, file_name) for file_name in os.listdir(folder_path) if
file_name.endswith(('.jpg', '.png', '.gif'))]
all_responses = []
for i in range(0, len(file_paths), max_files_per_batch):
for path in file_paths:
mime_type, _ = guess_type(path)
if mime_type in ('image/jpeg', 'image/png', 'image/gif'):
with open(path, 'rb') as file:
file_tuple = ('uploads', (os.path.basename(path), file.read(), mime_type))
response = upload_file(file_tuple, headers, cookies)
if response.status_code == 200:
try:
response_data = json.loads(response.text)
if response_data['type'] == 'success':
file_name_without_extension, file_extension = os.path.splitext(os.path.basename(path))
file_data = {
"file_name": file_name_without_extension,
"file_extension": file_extension.lstrip('.'),
"ID": response_data['data']['id']
}
all_responses.append(file_data)
except Exception as e:
print(f"解析文件响应时出错 {path}: {e}")
folder_name = os.path.basename(os.path.normpath(folder_path))
output_file_path = f'{folder_name}_response_data.json'
with open(output_file_path, 'w', encoding='utf-8') as output_file:
json.dump(all_responses, output_file, indent=4, ensure_ascii=False)
上面链接提到的大佬的原理是对于图床进行抓包,然后上传,
我的代码能做到的是用户通过配置文件登录自己的账号,然后进行自动化上传并且写入到JSON文件当中进行后续开发,
然后我们来看一下效果
{
"file_name": "",
"file_extension": "",
"ID": ""
},
上传成功以后就会写入到JSON文件当中成为这种格式,然后就可以自由编辑成其他任何想要的格式了
想要进行配置我们可以看到配置文件这里
下面是第一种
{
"headers": {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36",
"accept": "application/json",
"accept-encoding": "gzip, deflate, br",
"accept-language": "zh-CN,zh;q=0.9,en;q=0.8",
"cache-control": "no-cache",
"origin": "https://picshack.net",
"referer": "https://picshack.net/",
"x-csrf-token": "",
"x-requested-with": "XMLHttpRequest"
},
"cookies": {
"_ga": "1",
"": "",
"XSRF-TOKEN": "",
"_session": ""
}
}
这个是需要登录以后获取cookie然后填入的版本
然后下面是不登录获取cookie的版本
{
"headers": {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36",
"accept": "application/json",
"accept-encoding": "gzip, deflate, br",
"accept-language": "zh-CN,zh;q=0.9,en;q=0.8",
"cache-control": "no-cache",
"origin": "https://picshack.net",
"referer": "https://picshack.net/",
"x-csrf-token": "",
"x-requested-with": ""
},
"cookies": {
"_ga": "",
"PPA_CI": "",
"XSRF-TOKEN": "",
"_session": "",
"_ga_X1XJ6XC1S8": ""
}
}
大家可以自行登录网站获取cookie以后填入,图床网址如下
https://picshack.net/ 这个容量有没有限制 asd124689 发表于 2023-8-13 06:45
这个容量有没有限制
没限制,但是对图片大小有限制,最大只能上传100MB的图片 这个图片链接是永久有效的吗? QIUJIAN 发表于 2023-9-11 09:42
这个图片链接是永久有效的吗?
目前看来应该是可以稳定运行的,我也没有用很久
页:
[1]