forked from sezuan/uploadr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
uploadr.py
executable file
·129 lines (107 loc) · 3.93 KB
/
uploadr.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
#!/usr/bin/env python
import os
import sys
import logging
import requests
import argparse
from sleekxmpp import ClientXMPP
from sleekxmpp.exceptions import IqError, IqTimeout, XMPPError
from pprint import pprint
class InvalidRecipient(Exception):
pass
class Uploadr(ClientXMPP):
filename = None
def __init__(self, jid, password, filename, shorten_url, notify):
ClientXMPP.__init__(self, jid, password)
self.add_event_handler("session_start", self.session_start)
self.register_plugin('xep_0030') # Service Discovery
self.register_plugin("xep_0363") # HTTP File Upload
self.filename = filename
self.shorten_url = shorten_url
self.notify = notify
def good_recipient(self, jid):
if jid == self.boundjid.bare:
return True
# FIXME: Eigentlich sollte der Roster schon da sein.
roster = self.get_roster(block=True, timeout = 10)
jids = roster['roster']['items']
if jid in jids:
if jids[jid]['subscription'] == 'both':
return True
else:
return False
else:
return False
def session_start(self, event):
self.send_presence()
self.get_roster()
# Upload File
try:
if self.notify:
if self.good_recipient(self.notify):
get_url = self['xep_0363'].upload_file(self.filename)
self.send_message(mto=self.notify, mbody=get_url, mtype="chat")
else:
raise InvalidRecipient
else:
get_url = self['xep_0363'].upload_file(self.filename)
if self.shorten_url:
print self.short(get_url)
else:
print get_url
except IqError as err:
logging.error('There was an error getting the roster')
logging.error(err.iq['error']['condition'])
self.disconnect()
return
except IqTimeout:
logging.error('Server is taking too long to respond')
self.disconnect()
return
except XMPPError:
logging.error('Server doesn\'t support http_upload. Or something else went wrong.')
self.disconnect()
return
except InvalidRecipient:
logging.error('Invalid recipient. The recipient must be in the roster and subscribed.')
self.disconnect()
return
except:
raise
logging.error('Something went very wrong.')
self.disconnect()
return
self.disconnect(wait=True)
def short(self, url):
r = requests.post("https://yerl.org/", data = '"' + url + '"')
if r.status_code == 200:
return r.text.strip('"')
else:
return "url shortener failed."
if __name__ == '__main__':
logging.basicConfig(level=logging.WARN,
format='%(levelname)-8s %(message)s')
# Read Config
jid = None
password = None
try:
f = open(os.path.expanduser("~") + "/" + ".uploadrc", "rb")
jid = f.readline().strip()
password = f.readline().strip()
except:
pass
# Parse Opts
parser = argparse.ArgumentParser()
parser.add_argument("-j", "--jid", help="JID", required=not(jid))
parser.add_argument("-p", "--password", help="Password", required=not(password))
parser.add_argument("-s", "--short", help="Use https://yerl.org to shorten URL", action="store_true", required=False, )
parser.add_argument("-n", "--notify", help="Send a notification.", default = False, required=False, )
parser.add_argument("filename", help="File to upload")
args = parser.parse_args()
if args.jid:
jid = args.jid
if args.password:
password = args.password
xmpp = Uploadr(jid, password, args.filename, args.short, args.notify)
xmpp.connect()
xmpp.process(block=True)