A very simple Gio iPhone connection monitor in Python 3. I wrote a crappier version of this awhile ago and revamped it. Hope it’s helpful to someone.
Python
x
71
71
1
#!/usr/bin/env python3
2
3
'''
4
Simple iPhone connection monitor for Linux.
5
Requires Gtk (GLib, Gio) along with the Python 3 bindings.
6
Author: C. Nichols <mohawke@gmail.com>
7
'''
8
9
from gi.repository import Gio, GLib
10
import os
11
12
def notify(appname, message):
13
14
'''Gnome notification.'''
15
16
cmd = 'zenity --name="{0}" --notification --text="{1}" "$THREAT\n$STRING"'.format(appname, message)
17
os.system(cmd)
18
19
20
def on_iphone_added(monitor, volume):
21
22
'''List iPhone Document contents when connected.'''
23
24
#notify("Simple iPhone Monitor", "{0} connected.".format(volume.get_name()))
25
print("{0} connected.".format(volume.get_name()))
26
gvfs_path = "/run/user/1000/gvfs/afc:host={0},port=3/".format(volume.get_uuid())
27
not_ready = True
28
while not_ready:
29
if os.path.exists(gvfs_path):
30
for doc_contents in os.listdir(gvfs_path):
31
print(doc_contents)
32
not_ready = False
33
34
35
def on_iphone_removed(monitor, volume):
36
37
'''Display disconnected message.'''
38
39
#notify("Simple iPhone Monitor", "iPhone disconnected.")
40
print("disconnected")
41
42
43
def monitor():
44
45
'''Uses GTK Gio to access Gnome virtual filesystem and determine the
46
path to iPhone Documents. Return a path we can use to push files
47
over.'''
48
49
connection_loop = GLib.MainLoop()
50
51
monitor_iphone = Gio.VolumeMonitor.get()
52
53
monitor_iphone.connect("volume-added", on_iphone_added)
54
monitor_iphone.connect("volume-removed", on_iphone_removed)
55
for volume in monitor_iphone.get_volumes():
56
try:
57
activation_root = volume.get_activation_root()
58
if activation_root:
59
if activation_root.get_uri_scheme() == 'afc':
60
break
61
except:
62
pass # Something went wrong.
63
64
connection_loop.run()
65
66
67
# Main :: Start monitoring
68
def main():
69
monitor()
70
71
main() # Run program.