Skip to content

Instantly share code, notes, and snippets.

@paradoxxxzero
Created June 24, 2016 08:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save paradoxxxzero/daa482ed49381fef4c6ccb59f9d632fe to your computer and use it in GitHub Desktop.
Save paradoxxxzero/daa482ed49381fef4c6ccb59f9d632fe to your computer and use it in GitHub Desktop.
Serve random non-nsfw anime wallpaper from wallhaven to localhost:42424/img
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