-
Notifications
You must be signed in to change notification settings - Fork 248
/
Copy pathWebclip Creator.py
115 lines (95 loc) · 3.95 KB
/
Webclip Creator.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
from __future__ import print_function
import uuid, BaseHTTPServer, select, types, clipboard, console, photos, PIL, base64, urllib, webbrowser
from SimpleHTTPServer import SimpleHTTPRequestHandler
try: from cStringIO import StringIO
except ImportError: from StringIO import StringIO
keep_running = True
base_mobileconfig = """
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0"><dict>
<key>PayloadContent</key><array><dict>
<key>FullScreen</key><true/>
<key>Icon</key><data>%s</data>
<key>IsRemovable</key><true/>
<key>Label</key><string>%s</string>
<key>PayloadDescription</key><string>Configures Web Clip</string>
<key>PayloadDisplayName</key><string>Web Clip (Pythonista)</string>
<key>PayloadIdentifier</key><string>com.pudquick.profile.%s</string>
<key>PayloadOrganization</key><string>Pythonista</string>
<key>PayloadType</key><string>com.apple.webClip.managed</string>
<key>PayloadUUID</key><string>%s</string>
<key>PayloadVersion</key><integer>1</integer>
<key>Precomposed</key><true/>
<key>URL</key><string>pythonista://%s?action=run&args=%s</string>
</dict></array>
<key>PayloadDescription</key><string>Pythonista Web Clip</string>
<key>PayloadDisplayName</key><string>%s</string>
<key>PayloadIdentifier</key><string>com.pudquick.profile.%s</string>
<key>PayloadOrganization</key><string>%s</string>
<key>PayloadRemovalDisallowed</key><false/>
<key>PayloadType</key><string>Configuration</string>
<key>PayloadUUID</key><string>%s</string>
<key>PayloadVersion</key><integer>1</integer>
</dict></plist>
"""
class MobileConfigHTTPRequestHandler(SimpleHTTPRequestHandler):
def offer_generic(self):
self.send_response(200)
self.send_header("Content-type", "text/html")
self.send_header("Last-Modified", self.date_time_string())
self.end_headers()
f = StringIO()
f.write('<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">')
f.write('<html><body><a href="\webclip.mobileconfig">Something odd happened, click here instead</a></body></html>\n')
f.seek(0)
self.copyfile(f, self.wfile)
f.close()
def offer_mobileconfig(self):
global mobile_config_str
self.send_response(200)
self.send_header("Last-Modified", self.date_time_string())
self.send_header("Content-type", "application/x-apple-aspen-config")
self.send_header("Content-Length", len(mobile_config_str))
self.end_headers()
f = StringIO()
f.write(mobile_config_str)
f.seek(0)
self.copyfile(f, self.wfile)
f.close()
global keep_running
keep_running = False
def do_GET(self):
if (self.path.lower().endswith('.mobileconfig')):
return self.offer_mobileconfig()
return self.offer_generic()
class NicerHTTPServer(BaseHTTPServer.HTTPServer):
def serve_forever(self, poll_interval=0.5):
global keep_running
while keep_running:
self._handle_request_noblock()
ip = '127.0.0.1'
port = 8000
icon_label = console.input_alert('Homescreen name')
script_name = console.input_alert('Script path')
arg_str = console.input_alert('Script arguments')
payload_name = 'Pythonista Script'
console.alert('Please select an icon', '', 'Ok')
png_data = StringIO()
photos.pick_image().save(png_data, format='PNG')
png_str = base64.b64encode(png_data.getvalue())
png_data.close()
UUID1 = uuid.uuid4()
UUID2 = uuid.uuid4()
mobile_config_str = base_mobileconfig % (png_str, icon_label, UUID1, UUID1, urllib.quote(script_name), arg_str, payload_name, UUID2, script_name, UUID2)
clipboard.set('http://%s:%s/webclip.mobileconfig' % (ip, port))
console.clear()
console.set_font('Futura', 16)
console.set_color(0.2, 0.2, 1)
print("Safari will open automatically. Alternatively, you can open Safari manually and paste in the URL on your clipboard.\n")
console.set_font()
console.set_color()
my_httpd = NicerHTTPServer((ip, port), MobileConfigHTTPRequestHandler)
print("Serving HTTP on %s:%s ..." % (ip, port))
webbrowser.open('safari-http://%s:%s/webclip.mobileconfig' % (ip, port))
my_httpd.serve_forever()
print("\n*** Webclip installed! ***")