/
wheresmahintertubes.py
executable file
·214 lines (187 loc) · 7.25 KB
/
wheresmahintertubes.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
#!/usr/bin/python
# -*- coding: utf-8 -*-
#
# Where's Mah Intertubes?!
#
# A simple script that lets you know when your connection goes down or comes
# back up with sounds.
#
# Depends:
# espeak (if you don't want to use sound files)
# pygame (if you do)
#
# Options:
# -h, --help show this help message and exit
# --speak recite a message when connection goes on or off
# (default)
# --off=AUDIO_OFF, --boo=AUDIO_OFF
# set a sound played when connection is lost
# --on=AUDIO_ON, --yay=AUDIO_ON
# set a sound played when connection is back on
# -t TIMEOUT, --timeout=TIMEOUT
# set timeout for checking if connection works (default:
# 10s)
# -d DELAY, --delay=DELAY
# set delay between connection checks (default: 30s)
# -u URI, --uri=URI, --url=URI
# ping this URI to see if connection works (default:
# http://74.125.77.147)
# -v, --verbose display information about things done by the program
#
# Examples:
# Let's say you want to check if you can connect and you're fine with the
# espeak dude to moan about it instead fo using a cool sound you can use one
# of the following (let it run in the background):
#
# ./wheresmahintertubes.py &
# ./wheresmahintertubes.py --speak &
#
# If you want some proper fun sounds then all you need is point them out:
#
# ./wheresmahintertubes.py --yay=file/for_on.ogg --boo=file/for_off.mp3 &
#
# License:
# Copyright (C) 2010 Konrad Siek <konrad.siek@gmail.com>
#
# This program is free software: you can redistribute it and/or modify it
# under the terms of the GNU General Public License version 3, as published
# by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranties of
# MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
# PURPOSE. See the GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program. If not, see <http://www.gnu.org/licenses/>.
#
import time
import sys
import pygame
quiet = False
uri = "http://74.125.77.147"
def printerr(*args):
if quiet:
return
from sys import argv, stderr
from os.path import basename
stderr.write("%s:" % basename(argv[0]))
for arg in args:
stderr.write(" %s" % arg)
stderr.write("\n")
def printout(*args):
if quiet:
return
from sys import argv, stdout
from os.path import basename
stdout.write("%s:" % basename(argv[0]))
for arg in args:
stdout.write(" %s" % arg)
stdout.write("\n")
def can_has_connection(timeout):
import urllib2
printout("Checking ping to", uri)
try:
urllib2.urlopen(uri, timeout=timeout) # Google
except urllib2.URLError as error:
return False
return True
class Speaker:
def __init__(self):
self.library = {}
def add_to_library(self, key, path):
self.library[key] = path
def play_from_library(self, key):
if not key in self.library:
return False
self.play(self.library[key])
return True
def play(self, message):
from os import system
system('espeak %s' % message)
class PyGamePlayer:
def __init__(self):
pygame.init()
self.library = {}
def add_to_library(self, key, path):
self.library[key] = path
def play_from_library(self, key):
if not key in self.library:
return False
self.play(self.library[key])
return True
def play(self, path):
pygame.mixer.Sound(path).play()
class Checker:
def __init__(self, delay, timeout):
self.delay = delay
self.timeout = timeout
def run(self):
connected = can_has_connection(self.timeout)
printout('Initially the connection is', 'on' if connected else 'off')
while True:
time.sleep(self.delay)
current = can_has_connection(self.timeout)
if connected != current:
connected = current
self.react(connected)
def react(self, connected):
from threading import Thread
printout('The connection just went', 'on' if connected else 'off')
def run():
self.player.play_from_library(connected)
thread = Thread()
thread.run = run
thread.start()
if __name__ == '__main__':
from optparse import OptionParser
from os.path import basename
from sys import argv
usage = '\n%s [OPTIONS] ' % basename(argv[0]) + \
'--on=[SOUND FILE] --off=[SOUND FILE]\n' + \
'\tplay a sound when network connection goes up or down' + \
'\n%s [OPTIONS] ' % basename(argv[0]) + '--speak\n' + \
'\trecite a message when network connection goes up or down (boring...)'
description = 'Wait around and periodically check if the connection ' + \
'went up or down, and if that happens play an appropriate sound to ' + \
'indicate it to the user. Network connectivity is check by the ' + \
'the simple method of connecting a specific host address, and ' + \
'assuming that the entwork is down if it takes too much time for ' + \
'that host to respond.'
parser = OptionParser(usage=usage, description=description)
parser.add_option('--speak', action='store_true', dest='speak', \
help='recite a message when connection goes on or off (default)')
parser.add_option('--off', '--boo', action='store', dest='audio_off', \
help='set a sound played when connection is lost')
parser.add_option('--on', '--yay', action='store', dest='audio_on', \
help='set a sound played when connection is back on')
parser.add_option('-t', '--timeout', action='store', dest='timeout', \
help='set timeout for checking if connection works (default: 10s)', \
default=10, type='int')
parser.add_option('-d', '--delay', action='store', dest='delay', \
help='set delay between connection checks (default: 30s)', \
default=30, type='int')
parser.add_option('-u', '--uri', '--url', action='store', dest='uri', \
help='ping this URI to see if connection works (default: %s)' % uri, \
default=uri)
parser.add_option('-v', '--verbose', action='store_true', dest='verbose', \
help='display information about things done by the program')
opts, args = parser.parse_args()
quiet = not opts.verbose
uri = opts.uri
player = None
if opts.speak or not opts.audio_on or not opts.audio_off:
player = Speaker()
player.add_to_library(True, "Connection just went up")
player.add_to_library(False, "Connection just went down")
else:
player = PyGamePlayer()
player.add_to_library(True, opts.audio_on)
player.add_to_library(False, opts.audio_off)
checker = Checker(delay=opts.delay, timeout=opts.timeout)
checker.player = player
try:
checker.run()
except (KeyboardInterrupt, SystemExit):
printout('Exiting...')
running = False