-
Notifications
You must be signed in to change notification settings - Fork 1
/
toot.py
127 lines (118 loc) · 3.35 KB
/
toot.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
import sys
import re
import json
import requests
import unicodedata
from plistlib import load
i = sys.argv[1]
info = load(open('info.plist','rb'))
instance = info['variables']['instance']
access = info['variables']['access_key']
status_h = {'Authorization':'Bearer '+access}
keys = re.findall(" !.*?:",i)
if len(keys):
values = re.compile(" !.*?:.*?").split(i)
for j in range(len(keys)):
keys[j] = keys[j][2:-1]
keys.insert(0,'status')
p = dict()
for key in range(len(keys)):
p[keys[key]] = unicodedata.normalize('NFC',values[key].strip())
else:
p = {'status':unicodedata.normalize('NFC',i)}
def clipboard_image():
import io
from PIL import ImageGrab
buff = ImageGrab.grabclipboard()
img = io.BytesIO()
try:
buff.save(img,format='PNG')
except:
print('Image is not in the most recent clipboard')
return
img = img.getvalue()
files = {'file':img}
r = requests.post(instance+'/api/v1/media',headers=status_h, files=files)
print(r.status_code)
media_id = r.json()['id']
return media_id
def get_prev():
account_id = requests.get(instance+'/api/v1/accounts/verify_credentials',headers=status_h).json()['id']
prev_status = requests.get(instance+'/api/v1/accounts/'+account_id+'/statuses',headers=status_h).json()[0]
return prev_status
def get_url():
try:
import subprocess
currentTabUrl = str(subprocess.check_output(['osascript','browser.scpt']))[2:-3]
url = currentTabUrl
if currentTabUrl == 'browser not in front':
raise
return url # might need encoding. later
except:
pass
# trim every 500 char, returns list
def trim(t):
t_ = t.split(' ')
a = list()
s = ''
n = 0
for i in t_:
if n + 1 + len(i) > 500:
a.append(s.strip())
s = i
n = len(i)
else:
s += ' ' + i
n += len(i) + 1
a.append(s.strip())
return a
if 'web' in keys:
u = get_url()
p['web'] = u
if 'prev' in keys:
p['prev']=True
if 'cb' in keys:
p['cb'] = True
if 'base' in keys:
p['base'] = True
def sendtoot(status, cw=None, visib=None, web=None, cb=None, prev=None, to=None, base=None, *args):
da = dict()
da['status'] = status
if cw:
da['spoiler_text'] = cw
da['visibility'] = visib
if web: # input is url
da['status'] += '\n\n' + str(web)
if to:
da['in_reply_to_id'] = to
if prev:
prev_stat = get_prev()
da['in_reply_to_id'] = prev_stat['id']
if not cw:
da['spoiler_text'] = prev_stat['spoiler_text']
if not visib:
da['visibility'] = prev_stat['visibility']
else:
visib = info['variables']['visibility']
if cb:
media_id = clipboard_image()
da['media_ids[]'] = media_id
if base:
import base64
da['status'] = base64.encodestring(da['status'].encode('utf-8')).decode('utf-8')
r = requests.post(instance + '/api/v1/statuses', headers=status_h, data=da)
print(r.json()['id'])
if len(p['status']) < 500:
sendtoot(**p)
else:
t = p['status']
sl = trim(t)
for s in range(len(sl)):
p1 = p
if s == 0:
p1['status'] = sl[s]
sendtoot(**p1)
else:
p1['status'] = sl[s]
p1['prev'] = True
sendtoot(**p1)