-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathaway.py
More file actions
executable file
·139 lines (122 loc) · 5.36 KB
/
away.py
File metadata and controls
executable file
·139 lines (122 loc) · 5.36 KB
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
130
131
132
133
134
135
136
137
138
139
#!/usr/bin/env python3
import dbus
from dbus.mainloop.glib import DBusGMainLoop
import subprocess
import psutil
from gi.repository.GObject import MainLoop
class Away:
def __init__(self):
DBusGMainLoop(set_as_default=True)
self.mem = 'ActiveChanged'
self.dest = 'org.gnome.ScreenSaver'
self.bus = dbus.SessionBus()
self.loop = MainLoop()
self.bus.add_signal_receiver(self.catch, self.mem, self.dest)
self.paused_before = False
self.purple_prev = None
def _is_running(self, name):
processes = []
for p in psutil.process_iter():
try:
if p.name() == name:
processes.append(p)
except psutil.Error:
pass
if processes:
return True
else:
return False
def catch(self, away):
# Check if Spotify is running and set dbus
if self._is_running('spotify'):
mpris_obj = self.bus.get_object(
'org.mpris.MediaPlayer2.spotify',
'/org/mpris/MediaPlayer2')
mpris_manager = dbus.Interface(
mpris_obj, 'org.mpris.MediaPlayer2.Player')
dbus_properties_manager = dbus.Interface(
mpris_obj, 'org.freedesktop.DBus.Properties')
spotify_status = dbus_properties_manager.Get(
'org.mpris.MediaPlayer2.Player', 'PlaybackStatus')
else:
spotify_status = 'Not running'
# Check if Pidgin is running and set dbus
if self._is_running('pidgin.orig'):
purple_obj = self.bus.get_object('im.pidgin.purple.PurpleService',
'/im/pidgin/purple/PurpleObject')
purple_manager = dbus.Interface(purple_obj,
'im.pidgin.purple.PurpleInterface')
purple_away = purple_manager.PurpleSavedstatusGetIdleaway()
purple_current = purple_manager.PurpleSavedstatusGetCurrent()
pidgin_status = 'Running'
else:
pidgin_status = 'Not running'
if away == 1: # Screensaver turned on
print('INFO: Screen saver turned ON.')
# Lock down Mac
print('INFO: Locking down Mac.')
subprocess.call(
'ssh cruzseba-laptop.aka.amazon.com \'pmset displaysleepnow\'',
shell=True)
# Handle Spotify
print('INFO: Spotify status: {}'.format(spotify_status))
if spotify_status != 'Not running':
if spotify_status == 'Playing':
self.paused_before = False
mpris_manager.PlayPause()
else:
self.paused_before = True
# Handle Hexchat
if self._is_running('hexchat'):
print('INFO: hexchat running, marking as AWAY')
subprocess.call('hexchat -e -c AWAY', shell=True)
# Handle Pidgin
print('INFO: Pidgin status: {}'.format(pidgin_status))
if pidgin_status != 'Not running':
# STATUS_OFFLINE = 1
# STATUS_AVAILABLE = 2
# STATUS_UNAVAILABLE = 3
# STATUS_INVISIBLE = 4
# STATUS_AWAY = 5
# STATUS_EXTENDED_AWAY = 6
# STATUS_MOBILE = 7
# STATUS_TUNE = 8
# https://developer.pidgin.im/wiki/DbusHowto#CallingPidginmethods
# Don't touch status when offline
if purple_manager.PurpleSavedstatusGetType(
purple_current) >= 2:
self.purple_prev = purple_current
print('INFO: Pidgin status was: {}.'
' Marking as AWAY'.format(self.purple_prev))
purple_manager.PurpleSavedstatusActivate(purple_away)
else:
print('INFO: Pidgin status was: OFFLINE. '
'Not changing status.')
else: # Screensaver turned off
print('INFO: Screen saver turned OFF.')
# Handle Hexchat
if self._is_running('hexchat'):
print('INFO: hexchat running, marking as BACK')
subprocess.call('hexchat -e -c BACK', shell=True)
# Handle Spotify
print('INFO: Spotify status: {}'.format(spotify_status))
if spotify_status != 'Not running':
if spotify_status == 'Paused' and not self.paused_before:
mpris_manager.PlayPause()
# Handle Pidgin
print('INFO: Pidgin status: {}'.format(pidgin_status))
if pidgin_status != 'Not running':
if self.purple_prev:
print('INFO: Pidgin status was: {}. Restoring {}'
.format(purple_current, self.purple_prev))
purple_manager.PurpleSavedstatusActivate(self.purple_prev)
self.purple_prev = None
else:
print('INFO: Pidgin status was: OFFLINE. '
'Not changing status.')
# Light up Mac's screen
print('INFO: Lightning up Mac\'s screen.')
subprocess.call(
'ssh cruzseba-laptop.aka.amazon.com \'caffeinate -u -t 1\'',
shell=True)
Away().loop.run()