-
Notifications
You must be signed in to change notification settings - Fork 0
/
yafu.py
executable file
·278 lines (213 loc) · 8.6 KB
/
yafu.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
#!/usr/bin/env python3
# yafu - command line client for YaFU
# Copyright (C) 2016 Benjamin Abendroth
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import argparse, requests, re, os, pickle
from lxml import html
from time import time
from datetime import datetime, timedelta
from os.path import expanduser, isfile, basename, devnull
from urllib.parse import urlencode
from string import Template
DEFAULT_FORMAT = '''=== $basename $expired_str===
File: $file
Date: $date
Expires: $expire_date [$expires]
Type: $type
URL: $direct_url
Info: $info_url
Delete: $deletion_url
'''
RECORD_SHORTCUTS = {
'b': 'basename', 'f': 'file', 'h': 'hidden',
'i': 'info_url', 'd': 'deletion_url', 'u': 'direct_url',
'D': 'date', 'e': 'expire_date', 'x': 'expired',
'E': 'expires'
}
parser = argparse.ArgumentParser(prog='YaFU', description='Commandline client for YaFU')
# FILE OPTIONS
fileopts = parser.add_argument_group(title='File Options')
fileopts.add_argument('--db', help='Database file', type=expanduser)
fileopts.add_argument('--no-db', help='Disable database', dest='db', action='store_const', const=devnull)
# ACTIONS
actions = parser.add_argument_group(title='Actions')
actions = actions.add_mutually_exclusive_group(required=True)
actions.add_argument('-d', '--delete', help='Delete the given links', metavar='URL', nargs='+')
actions.add_argument('-u', '--upload', help='Files to upload', metavar='FILE', nargs='+')
actions.add_argument('-l', '--list', help='List the records in database', action='store_true')
actions.add_argument('-L', '--list-remote', help='List files on server', action='store_true')
# UPLOAD ACTION
ulcmd = parser.add_argument_group('Upload Options')
ulcmd.add_argument('-b', '--base-url', help='Specify base-url')
ulcmd.add_argument('-e', '--email', help='Specify email for file upload')
ulcmd.add_argument('-p', '--password', help='Specify password for file upload')
ulcmd.add_argument('-c', '--comment', help='Specify comment for file upload')
ulcmd.add_argument('-x', '--expires', help='Specify expire time',
choices=['30m', '1h', '6h', '1d', '3d', '1w', 'max'])
hide_group = ulcmd.add_mutually_exclusive_group()
hide_group.add_argument('--private', help='Hide file in public list [DEFAULT]', dest='hide', action='store_true')
hide_group.add_argument('--public', help='Show file in public list', dest='hide', action='store_false')
# LIST ACTION
listcmd = parser.add_argument_group('List Options')
listcmd.add_argument('--show-expired', help='List also expired records', action='store_true')
listcmd.add_argument('--date-format', help='Specify date format')
listcmd.add_argument('-n', '--number', help='Only list the last N records', type=int)
listcmd.add_argument('-f', '--format', help='Specify output format', type=Template)
# REMOTE LIST ACTION
rlistcmd = parser.add_argument_group('List Remote Options')
#rlistcmd.add_argument('-d', '--download', help='Download files')
#rlistcmd.add_argument('-p', '--password', help='Download files')
parser.set_defaults(
db = '~/.config/yafu.db',
base_url = 'http://pixelbanane.de/yafu',
format = DEFAULT_FORMAT,
date_format = '%Y-%m-%d %H:%M:%S',
hide = True,
expires = '1w'
)
class RecordExpander(dict):
def __init__(self, record, args):
self._record = record
self._cache = {}
self._args = args
def __getitem__(self, var):
if len(var) == 1:
try:
var = RECORD_SHORTCUTS[var]
except:
raise Exception('Record shortcut not found: ' + var)
if var in self._record:
return self._record[var]
if var not in self._cache:
self._cache[var] = self.getVar(var)
return self._cache[var]
def getVar(self, var):
if var == 'expired':
return (self._record['expires'] != 'max' and
time() > self._record['expire_ts'])
if var == 'expired_str':
return 'EXPIRED ' if self['expired'] else ''
if var == 'type':
return 'Private' if self._record['hide'] else 'Public'
if var == 'date':
return datetime.fromtimestamp(self._record['date_ts']).strftime(self._args.date_format)
if var == 'expire_date':
if self._record['expire_ts'] == 0:
return 'NEVER'
return datetime.fromtimestamp(self._record['expire_ts']).strftime(self._args.date_format)
if var == 'direct_url':
return '%s/%s/%s' % (
self['base_url'], self['id'], self['basename_escaped'] )
if var == 'info_url':
return '%s/info/%s/%s' % (
self['base_url'], self['id'], self['basename_escaped'] )
if var == 'deletion_url':
return '%s/delete/%s' % (self['base_url'], self['delete_id'])
if var == 'basename':
return basename(self._record['file'])
if var == 'basename_escaped':
return urlencode({'': self['basename']})[1:]
raise KeyError
def yafu_list(db, args):
offset = args.number - len(db) if args.number else 0
for record in db:
offset += 1
if offset < 0:
continue
record = RecordExpander(record, args)
if args.show_expired or not record['expired']:
print(args.format.safe_substitute(record))
def yafu_delete(url):
params = { 'confirm': 'do it!' }
response = requests.post(url, data=params)
def yafu_upload(file, args):
if os.path.getsize(file) > 95000000:
raise Exception("File too big")
ul_files = { 'upload': open(file, 'rb').read() }
# https://stackoverflow.com/questions/33717690/python-requests-post-with-unicode-filenames
ul_parameters = {
'filename': basename(file),
'expires': args.expires,
'email': args.email,
'password': args.password,
'comment': args.comment
}
if args.hide:
ul_parameters['hide'] = 'true'
response = requests.post(args.base_url + '/index.php', files=ul_files, data=ul_parameters)
try:
match = re.search('href="[^"]*/info/([0-9]+)/[^"]+', response.text)
ul_id = match.group(1)
except:
ul_id = 'NOT_AVAILABLE'
print('Could not extract file id')
try:
match = re.search('http://.+/delete/([^"]+)', response.text)
delete_id = match.group(1)
except:
delete_id = 'NOT_AVAILABLE'
print('Could not extract delete id')
record = {
'file': file,
'base_url': args.base_url,
'id': ul_id,
'delete_id': delete_id,
'expires': args.expires,
'email': args.email,
'date_ts': int( time() ),
'hide': args.hide,
'expire_ts': 0
}
if record['expires'] != 'max':
n = int(record['expires'][:-1])
t = { 'm':'minutes', 'h':'hours', 'd':'days', 'w':'weeks' }[ record['expires'][-1] ]
td = timedelta(**{t:n})
record['expire_ts'] = int( ( datetime.now() + td ).timestamp() )
return record
def yafu_list_remote(base_url):
r = requests.get(base_url + '/?action=listfiles')
t = html.fromstring(r.content)
for tr in t.xpath('//tbody/tr'):
tds = tr.xpath('.//td')
a = tds[1].xpath('.//a')[0]
url = '%s%s' % ('http://pixelbanane.de', a.attrib['href'])
name = a.text_content()
pw = True if tds[2].xpath('.//img') else False
size = tds[3].text_content()
expires = tds[4].xpath('text()')[0]
#print(name, url, pw, size, expires)
print(url)
args = parser.parse_args()
try:
record_storage = pickle.load(open(args.db, 'rb'))
except:
record_storage = []
if args.upload:
for file in args.upload:
try:
record = yafu_upload(file, args)
fmt_record = RecordExpander(record, args)
print(args.format.safe_substitute(fmt_record))
record_storage.append(record)
except Exception as e:
print(file + ':', e)
elif args.delete:
for url in args.delete:
yafu_delete(url)
elif args.list:
yafu_list(record_storage, args)
elif args.list_remote:
yafu_list_remote(args.base_url)
pickle.dump(record_storage, open(args.db, 'wb'))