No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

main.py 2.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. #!/usr/bin/env python
  2. import gtk
  3. import pygtk
  4. import time
  5. import sys
  6. from daemon import Daemon
  7. from subprocess import call
  8. from random import choice
  9. from string import ascii_letters, digits
  10. from os import listdir as ls, rename
  11. from os.path import exists, isfile, join, splitext
  12. pygtk.require('2.0')
  13. # Config
  14. SSH_PORT = 1337
  15. PATH = "/home/ian/Dropbox/projects/files/screenshots"
  16. BASE_URL = "http://files.ianonavy.com/"
  17. SCP_PATH = "vpn.ianonavy.com:sites/ianonavy.com/files"
  18. WAIT_INTERVAL = 1
  19. class FilesDaemon(Daemon):
  20. def run(self):
  21. """
  22. Constantly loops checking for a new file. It's not very efficient, but
  23. it gets the job done.
  24. """
  25. print "Loading list of all files..."
  26. # Get list of all files.
  27. files = [f for f in ls(PATH) if isfile(join(PATH, f))]
  28. print "Done! Waiting for some new files."
  29. while 1:
  30. for f in ls(PATH):
  31. filename = join(PATH, f) # Get the full file path.
  32. if isfile(filename):
  33. if f not in files:
  34. #print f, files
  35. base, extension = splitext(f)
  36. new_filename = self.random_filename(extension=extension)
  37. rename(filename, join(PATH, new_filename))
  38. files.append(new_filename)
  39. self.upload(join(PATH, new_filename))
  40. image_url = "%s%s" % (BASE_URL, new_filename)
  41. self.copy_to_clipboard(image_url)
  42. time.sleep(WAIT_INTERVAL)
  43. def upload(self, filename):
  44. """ Uploads a file to the remote host. """
  45. print "scp -P %d %s %s" % (SSH_PORT, filename, SCP_PATH)
  46. call("scp -P %d %s %s" % (SSH_PORT, filename, SCP_PATH), shell=True)
  47. def copy_to_clipboard(self, text):
  48. """ Copies the passed argument to the clipboard. """
  49. clipboard = gtk.clipboard_get()
  50. clipboard.set_text(text)
  51. clipboard.store() # Let other programs use it.
  52. def random_filename(self, length=4, extension='', attempts=10):
  53. """ Generates a random filename and verifies that it doesn't exist. """
  54. chars = ''.join([ascii_letters, digits])
  55. for attempt in range(attempts):
  56. filename = ''.join([choice(chars) for i in range(length)])
  57. filename = filename + extension
  58. if not exists(filename):
  59. return filename
  60. if __name__ == "__main__":
  61. daemon = FilesDaemon('/tmp/filesd.pid')
  62. if len(sys.argv) == 2:
  63. if 'start' == sys.argv[1]:
  64. daemon.start()
  65. elif 'stop' == sys.argv[1]:
  66. daemon.stop()
  67. elif 'restart' == sys.argv[1]:
  68. daemon.restart()
  69. else:
  70. print "Unknown command"
  71. sys.exit(2)
  72. sys.exit(0)
  73. else:
  74. print "usage: %s {start|stop|restart}" % sys.argv[0]
  75. sys.exit(2)