Guest User

Untitled

a guest
May 19th, 2018
268
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.17 KB | None | 0 0
  1. #!/usr/bin/env python
  2.  
  3. # LGPLv2 yadda yadda yadda, do whatever, yadda yadda yadda, bum it in the gob, yadda yadda yadda
  4.  
  5. import bluetooth, datetime, optparse, time, os, user, pynotify, tempfile, twitter, sys
  6.  
  7. # Install required packages on Ubuntu:
  8. # sudo aptitude install python-twitter python-notify python-bluetooth festival
  9.  
  10. # Set these values if you wish to use Twitter (you probably won't, but then I didn't really want too but it happended anyway)
  11. twitter_username = ''
  12. twitter_password = ''
  13.  
  14.  
  15. def speak_text(text):
  16. """Shell out to festival and mplayer to speak out text over sound system"""
  17. tf = tempfile.NamedTemporaryFile(delete=False)
  18. textfile = tf.name
  19. tf.write(text)
  20. tf.close()
  21.  
  22. wavefile = textfile + '.wav'
  23. os.system( 'text2wave -o %s %s' % (wavefile, textfile) )
  24. os.remove(textfile)
  25. os.system('mplayer %s' % wavefile)
  26. os.remove(wavefile)
  27.  
  28.  
  29. if __name__ == '__main__':
  30.  
  31. # Have a gander at the options
  32. parser = optparse.OptionParser()
  33. parser.add_option('-p', '--period', dest='period', default=10, help='Pause between scans')
  34. parser.add_option('-t', '--notification-timeout', dest='notification_timeout', default=10, help='Timeout for desktop notifications')
  35. parser.add_option('-s', '--speak', dest='speak', action='store_true', default=False, help='Speak names using festival and mplayer')
  36. parser.add_option('-w', '--twitter', dest='twitter', action='store_true', default=False, help='Upload names to twitter')
  37.  
  38. options, args = parser.parse_args()
  39.  
  40. # Initialise the bluetooth module
  41. if not pynotify.init("bluesearch"):
  42. parser.error("something went a bit wrong with initialising notify, sorry")
  43. sys.exit()
  44.  
  45. # We keep a list of the last found MAC addresses here so we know when we have found new ones.
  46. last_macs = set( [] )
  47.  
  48. print "starting bluesearch"
  49.  
  50. # authenticate twitter if required
  51. if options.twitter:
  52. twit = twitter.Api(username=twitter_username, password=twitter_password)
  53.  
  54.  
  55. # Loop until terminated
  56. while True:
  57.  
  58. print "searching for devices..."
  59.  
  60. try:
  61. # Search for devices
  62. devices = bluetooth.discover_devices(lookup_names=True)
  63. except:
  64. print "could not connect to bluetooth (or no devices found)"
  65. else:
  66.  
  67. if len(devices):
  68.  
  69.  
  70. # Get a set of mac current addresses
  71. macs = set( [ mac for mac, name in devices ] )
  72.  
  73. # See which ones have appeared since the last scan
  74. new_macs = list(macs - last_macs)
  75.  
  76. if len(new_macs):
  77. print "new devices found:"
  78.  
  79. # Display a notification for each
  80. for mac in new_macs:
  81. name = dict(devices)[mac]
  82.  
  83. print '\t%s' % name
  84.  
  85. # Make a desktop notification
  86. n = pynotify.Notification(name, mac)
  87. n.set_timeout(options.notification_timeout)
  88. n.show()
  89.  
  90. # Speak the text if required
  91. if options.speak:
  92. speak_text(name)
  93.  
  94. # Post to twitter if required
  95. if options.twitter:
  96. try:
  97. twit.PostUpdate(name)
  98. except:
  99. twit = twitter.Api(username=twitter_username, password=twitter_password)
  100. try:
  101. twit.PostUpdate(name)
  102. except:
  103. print "twitter error"
  104.  
  105. last_macs = macs
  106.  
  107. # Wait a bit
  108. print "waiting %d seconds..." % options.period
  109. time.sleep(options.period)
Add Comment
Please, Sign In to add comment