Advertisement
andrewb

Parindom.py

Oct 22nd, 2011
484
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.31 KB | None | 0 0
  1. #!/usr/bin/env python
  2. import os, random, sys, web
  3. from dropbox import client, session
  4.  
  5. """ GLOBAL VARIABLES """
  6. APP_KEY = ''
  7. APP_SECRET = ''
  8. ACCESS_TYPE = 'dropbox'
  9. URLURL = ''
  10.  
  11. """ SESSION CLASS """
  12. class StoreSess(session.DropboxSession):
  13.     TOKEN_FILE = "token.txt"
  14.    
  15.     def load_creds(self):
  16.         try:
  17.             fh = open(self.TOKEN_FILE)
  18.             stored_creds = fh.read()
  19.             fh.close()
  20.             self.set_token(*stored_creds.split("|"))
  21.         except IOError:
  22.             pass
  23.    
  24.     def save_creds(self, token):
  25.         fh = open(self.TOKEN_FILE, "w")
  26.         fh.write("|".join([token.key, token.secret]))
  27.         fh.close()
  28.    
  29.     def link(self):
  30.         request_token = self.obtain_request_token()
  31.         url = self.build_authorize_url(request_token)
  32.         print "Go to:", url
  33.         print "Please authorize in the browser. After you're done, press enter."
  34.         raw_input()
  35.         self.obtain_access_token(request_token)
  36.         self.save_creds(self.token)
  37.  
  38. """ URLS """
  39. urls = (
  40.     '/', 'Index',
  41.     '/quit', 'Quit',
  42. )
  43.  
  44. """ HANDLER CLASSES """
  45. class Index:
  46.     def GET(self):
  47.         dbsess = StoreSess(APP_KEY, APP_SECRET, ACCESS_TYPE)
  48.         dbsess.load_creds()
  49.         dbclient = client.DropboxClient(dbsess)
  50.         resp = dbclient.metadata("Public/parity/sm")
  51.         dir = []
  52.         if 'contents' in resp:
  53.             for f in resp['contents']:
  54.                 name = os.path.basename(f['path'])
  55.                 dir.append(name)
  56.         return self.html(random.choice(dir))
  57.    
  58.     def html(self, image):
  59.         if len(image) == 0: return "No image"
  60.         html = """<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN">
  61. <html lang="en">
  62. <head>
  63.    <title>Parindom</title>
  64. </head>
  65. <body>
  66.    <center><img src="%s/%s" border="0" alt="Parindom"></center>
  67. </body>
  68. </html>""" % (URLURL, image)
  69.         return html
  70.    
  71. class Quit:
  72.     def GET(self):
  73.         sys.exit()
  74.  
  75. """ MAIN PROGRAM """
  76. if __name__ == '__main__':
  77.     if len(APP_KEY) == 0 or len(APP_SECRET) == 0:
  78.         exit("You need to set your APP_KEY and APP_SECRET!")
  79.  
  80.     dbsess = StoreSess(APP_KEY, APP_SECRET, ACCESS_TYPE)
  81.     dbsess.load_creds()
  82.    
  83.     if not dbsess.is_linked():
  84.         dbsess.link()
  85.  
  86.     app = web.application(urls, globals())
  87.     app.run()
  88.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement