|
1 |
| -import requests |
2 | 1 | import os
|
3 | 2 | import random
|
4 |
| - |
| 3 | +import urllib3 |
5 | 4 | from .site_data import Site_Data_CLSS, sites_data_dict
|
6 | 5 | from .pretty_print import *
|
7 | 6 | from .logger import Logger
|
8 |
| - |
9 |
| -site = "Pixeldrain" |
| 7 | +import json |
10 | 8 |
|
11 | 9 | class Pixeldrain:
|
12 | 10 |
|
13 |
| - def Uploader(file, proxy_list, user_agents, api_keys): |
| 11 | + @staticmethod |
| 12 | + def Uploader(file, proxy_list, user_agents, api_keys): |
14 | 13 | try:
|
| 14 | + site = "Pixeldrain" # Moved the 'site' definition inside the method |
| 15 | + |
15 | 16 | ua = random.choice(user_agents)
|
16 | 17 | size_limit = f'{sites_data_dict[site]["size_limit"]} {sites_data_dict[site]["size_unit"]}'
|
17 | 18 |
|
18 | 19 | base_url = sites_data_dict[site]["download_url_base"]
|
19 | 20 |
|
20 | 21 | file_size = os.stat(file).st_size
|
21 | 22 | file_name = os.path.basename(file)
|
22 |
| - file_name = (file_name[:240] + '..') if len(file_name) > 240 else file_name # Changed from 255 to 240 as an additional safety net. |
23 |
| - |
| 23 | + file_name = (file_name[:240] + '..') if len(file_name) > 240 else file_name |
| 24 | + |
24 | 25 | upload_url = sites_data_dict[site]["url"].format(file_name=file_name)
|
25 | 26 |
|
26 | 27 | calc_size = Site_Data_CLSS.size_unit_calc(site, file_size)
|
27 | 28 |
|
28 | 29 | headers = {"User-Agent": ua, "Content-Type": "application/octet-stream"}
|
29 |
| - proxies = random.choice(proxy_list) if proxy_list else None |
| 30 | + proxy = random.choice(proxy_list) if proxy_list else None |
30 | 31 |
|
31 | 32 | if calc_size == "OK":
|
| 33 | + http = urllib3.PoolManager() |
32 | 34 | with open(file, "rb") as file_upload:
|
33 |
| - req = requests.put(url=upload_url, data=file_upload, headers=headers, proxies=proxies, timeout=300, stream=True).json() |
34 |
| - file_upload.close() |
35 |
| - return {"status": "ok", "file_name": file_name, "file_url": base_url + req['id']} |
| 35 | + req = http.urlopen('PUT', upload_url, body=file_upload, headers=headers, timeout=300) |
| 36 | + if req.status != 201: |
| 37 | + raise Exception(f"HTTP Error {req.status}: {req.reason}") |
| 38 | + response_json = json.loads(req.data.decode('utf-8')) |
| 39 | + return {"status": "ok", "file_name": file_name, "file_url": base_url + response_json['id']} |
36 | 40 | else:
|
37 | 41 | return {"status": "size_error", "file_name": file_name, "exception": "SIZE_ERROR", "size_limit": size_limit}
|
38 | 42 |
|
39 | 43 | except Exception as e:
|
40 |
| - return {"status": "error", "file_name": file_name, "exception": str(e), "extra": raw_req} |
41 |
| - |
| 44 | + return {"status": "error", "file_name": file_name, "exception": str(e)} |
0 commit comments