-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtgimgupl.py
119 lines (100 loc) · 3.49 KB
/
tgimgupl.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
#!/usr/bin/env python3
## Tgimgupl: upload images to telegra.ph from command line.
## Copyright (C) 2018 BrainFucker <[email protected]>
##
## 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.
## This is the copy of this util: https://github.com/rekcuFniarB/TgImgUpl#readme
import requests, sys, os
info = '''
Usage:
tgimgupl image_file
Tgimgupl © 2018 BrainFucker
This program comes with ABSOLUTELY NO WARRANTY.
This is free software, and you are welcome
to redistribute it under certain conditions.
'''
class conf:
domain = 'https://telegra.ph'
headers = {
'User-Agent': 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:89.0) Gecko/20100101 Firefox/89.0',
'Referer': os.path.join(domain, ''),
}
uplurl = os.path.join(domain, 'upload');
types = {
'jpg': 'image/jpeg',
'svg': 'image/svg+xml',
#'ogg': 'application/ogg',
#'mp3': 'audio/mpeg',
#'txt': 'text/plain',
#'html': 'text/html'
}
class UploadError(BaseException):
pass
sys.path.insert(0, os.path.join(os.path.expanduser('~'), '.config', 'tgimgupl'))
try:
## Override config with local version if exists
import localconf
for key in localconf.__dict__.keys():
if not '__' in key:
setattr(conf, key, localconf.__dict__[key])
except ModuleNotFoundError:
pass
def err(msg):
sys.stderr.write(f'{msg}\n')
def getType(filename):
_f = filename.split('.')
ext = _f[len(_f) - 1].lower()
mime = f'image/{ext}'
if ext in conf.types:
mime = conf.types[ext]
return mime
def upload(filePath):
'''Uploads file'''
if not os.path.exists(filePath):
raise UploadError(f'File "{filePath}" does not exist.')
filename = os.path.basename(filePath)
## replace non ascii chars with '?' otherwise upload fails
ascii_filename = filename.encode('ascii', 'replace').decode('utf-8')
filetype = getType(filename)
fileToUpl = {'file': (ascii_filename, open(filePath, 'rb'), filetype)}
r = requests.post(conf.uplurl, files=fileToUpl, headers=conf.headers)
if r.ok:
result = r.json()
if type(result) is list:
return {'base': conf.headers['Referer'], 'src': result[0]['src']}
elif type(result) is dict:
if 'error' in result:
raise UploadError(result['error'])
else:
raise UploadError(result)
else:
raise UploadError(result)
else:
raise UploadError(r)
if __name__ == '__main__':
if len(sys.argv) < 2:
err('Error: no file specified.')
err(info)
sys.exit(1)
if sys.argv[1] == '-h' or sys.argv[1] == '--help':
## show help
err(info)
sys.exit(0)
if len(sys.argv) > 2:
err('Error: batch mode not supported.')
err(info)
sys.exit(1)
try:
result = upload(sys.argv[1])
print(os.path.join(result["base"].strip('/'), result["src"].strip('/')))
except BaseException as e:
err(f'ERROR: {e}')
sys.exit(1)