Created
April 22, 2017 17:00
-
-
Save mastensg/b88ee575aed7d4edb9cebeb07a89de2f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
import psycopg2 | |
import tornado.ioloop | |
import tornado.web | |
import tornado.options | |
LISTEN = 2000 | |
def random_img(db): | |
cur = db.cursor() | |
q = """ | |
SELECT i.path, i.thumbnail_path | |
FROM images i | |
ORDER BY random() | |
LIMIT 1 | |
""" | |
cur.execute(q, ()) | |
img = cur.fetchone() | |
cur.close() | |
return img | |
class MainHandler(tornado.web.RequestHandler): | |
def initialize(self, db): | |
self.db = db | |
def get(self): | |
img = random_img(self.db) | |
url = "https://drawsdraws.com/{}".format(img[1]) | |
self.redirect(url) | |
def main(): | |
tornado.options.parse_command_line() | |
db = psycopg2.connect("dbname=draws") | |
app = tornado.web.Application([ | |
(r"/random_thumbnail", MainHandler, dict(db=db)), | |
], debug=True) | |
app.listen(LISTEN) | |
tornado.ioloop.IOLoop.current().start() | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment