#!/usr/bin/env python
import os, random, sys, web
from dropbox import client, session
""" GLOBAL VARIABLES """
APP_KEY = ''
APP_SECRET = ''
ACCESS_TYPE = 'dropbox'
URLURL = ''
""" SESSION CLASS """
class StoreSess(session.DropboxSession):
TOKEN_FILE = "token.txt"
def load_creds(self):
try:
fh = open(self.TOKEN_FILE)
stored_creds = fh.read()
fh.close()
self.set_token(*stored_creds.split("|"))
except IOError:
pass
def save_creds(self, token):
fh = open(self.TOKEN_FILE, "w")
fh.write("|".join([token.key, token.secret]))
fh.close()
def link(self):
request_token = self.obtain_request_token()
url = self.build_authorize_url(request_token)
print "Go to:", url
print "Please authorize in the browser. After you're done, press enter."
raw_input()
self.obtain_access_token(request_token)
self.save_creds(self.token)
""" URLS """
urls = (
'/', 'Index',
'/quit', 'Quit',
)
""" HANDLER CLASSES """
class Index:
def GET(self):
dbsess = StoreSess(APP_KEY, APP_SECRET, ACCESS_TYPE)
dbsess.load_creds()
dbclient = client.DropboxClient(dbsess)
resp = dbclient.metadata("Public/parity/sm")
dir = []
if 'contents' in resp:
for f in resp['contents']:
name = os.path.basename(f['path'])
dir.append(name)
return self.html(random.choice(dir))
def html(self, image):
if len(image) == 0: return "No image"
html = """<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN">
<html lang="en">
<head>
<title>Parindom</title>
</head>
<body>
<center><img src="%s/%s" border="0" alt="Parindom"></center>
</body>
</html>""" % (URLURL, image)
return html
class Quit:
def GET(self):
sys.exit()
""" MAIN PROGRAM """
if __name__ == '__main__':
if len(APP_KEY) == 0 or len(APP_SECRET) == 0:
exit("You need to set your APP_KEY and APP_SECRET!")
dbsess = StoreSess(APP_KEY, APP_SECRET, ACCESS_TYPE)
dbsess.load_creds()
if not dbsess.is_linked():
dbsess.link()
app = web.application(urls, globals())
app.run()
Comments