-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfilebin.py
153 lines (117 loc) · 3.83 KB
/
filebin.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
if __name__ == "__main__":
print("This is a subclass. Please use the main bot.py file.")
exit()
from filebin_client import Client
from filebin_client.api.bin_ import get_bin, delete_bin, put_bin
from filebin_client.api.file import get_bin_filename, post_bin_filename
from filebin_client.types import File
import uuid
import json
import time
base_url = "https://filebin.net"
async def create_filebin(title: str = None):
_client = Client(base_url=base_url, headers={'accept': 'application/json'})
count = 0
while True:
MyBin = str(uuid.uuid4())
if await is_bin_empty(MyBin):
break
else:
print(f'Bin {MyBin} already exists. Trying again in .15 seconds...')
time.sleep(0.15)
if count >= 5:
print(f'Timeout. Tried creating new bin 5 times. Exiting...')
return None
count += 1
print(f'Created bin: {base_url}/{MyBin}')
payload = "Upload Label or nglyph file".encode('utf-8')
if title is not None:
payload += " for {title}".encode('utf-8')
body = File(
payload=payload,
)
result = post_bin_filename.sync_detailed(
bin_=MyBin,
filename=payload,
client=_client,
body=body,
)
return f'{MyBin}'
async def delete_filebin(bin):
_client = Client(base_url=base_url, headers={'accept': 'application/json'})
result = delete_bin.sync_detailed(
bin_=bin,
client=_client
)
print(f'Deleted bin: {bin}')
async def get_files_in_bin(bin):
_client = Client(base_url=base_url, headers={'accept': 'application/json'})
result = get_bin.sync_detailed(
bin_=bin,
client=_client
)
result = result.content.decode()
result = json.loads(result)
files = result['files']
filenames = []
for file in files:
filenames.append(file['filename'])
return filenames
async def check_for_glyph_or_txt_files_in_bin(bin):
files = await get_files_in_bin(bin)
if len(files) == 0:
return False
for file in files:
if file.endswith('.nglyph'):
return True
elif file.endswith('.txt'):
return True
return False
async def check_for_nglyph_file_in_bin(bin):
files = await get_files_in_bin(bin)
if len(files) == 0:
return False
for file in files:
if file.endswith('.nglyph'):
return True
return False
async def is_bin_empty(bin):
_client = Client(base_url=base_url, headers={'accept': 'application/json'})
result = get_bin.sync_detailed(
bin_=bin,
client=_client
)
result = result.content.decode()
result = json.loads(result)
number_of_files = result['bin']['files']
print(f'Number of files in bin: {number_of_files}')
if number_of_files == 0:
print(f'Bin {bin} is empty.')
return True
else:
print(f'Bin {bin} is not empty.')
return False
async def lock_filebin(bin):
_client = Client(base_url=base_url, headers={'accept': 'application/json'})
result = put_bin.sync_detailed(
bin_=bin,
client=_client
)
print(f'Locked bin: {bin}')
async def download_file_from_bin(bin, filename):
_client = Client(base_url=base_url, headers={'accept': 'application/json'})
result = get_bin_filename.sync_detailed(
bin_=bin,
filename=filename,
client=_client
)
location = result.headers['location']
print(location)
# get request at location and write to file
async with _client.get_async_httpx_client() as client:
async with client.stream('GET', location) as response:
with open(filename, 'wb') as f:
async for chunk in response.aiter_bytes():
f.write(chunk)
# return filename and path
return filename