Created
June 24, 2016 08:14
-
-
Save paradoxxxzero/daa482ed49381fef4c6ccb59f9d632fe to your computer and use it in GitHub Desktop.
Serve random non-nsfw anime wallpaper from wallhaven to localhost:42424/img
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
from flask import Flask, redirect | |
from random import choice | |
from pyquery import PyQuery as pq | |
app = Flask(__name__) | |
usable_images = [] | |
for page in range(1, 10): | |
d = pq(url='https://alpha.wallhaven.cc/search?categories=010&purity=100&ratios=16x9&sorting=views&order=desc&page=%d' % page) | |
usable_images.extend(list(d.find('[data-wallpaper-id]').map(lambda _, x: x.attrib['data-wallpaper-id']))) | |
@app.route('/img') | |
def img(): | |
url = None | |
while url is None: | |
url = 'https://wallpapers.wallhaven.cc/wallpapers/full/wallhaven-%s.jpg' % choice(usable_images) | |
try: | |
pq(url) | |
except Exception: | |
url = None | |
return redirect(url) | |
app.run() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment