Skip to content

Instantly share code, notes, and snippets.

@blha303
Last active August 29, 2015 14:17
Show Gist options
  • Save blha303/e8e7568767cea6d12352 to your computer and use it in GitHub Desktop.
Save blha303/e8e7568767cea6d12352 to your computer and use it in GitHub Desktop.
Web service to search celebritymoviearchive for movies with nudes, with a nice json interface http://hasnudes.blha303.biz
from flask import Flask, jsonify, redirect, request, abort
import requests, time
from bs4 import BeautifulSoup as Soup
app = Flask(__name__)
f_gh = """<a href="https://gist.github.com/blha303/e8e7568767cea6d12352"><img style="position: absolute; top: 0; right: 0; border: 0;" src="https://camo.githubusercontent.com/a6677b08c955af8400f44c6298f40e7d19cc5b2d/68747470733a2f2f73332e616d617a6f6e6177732e636f6d2f6769746875622f726962626f6e732f666f726b6d655f72696768745f677261795f3664366436642e706e67" alt="Fork me on GitHub" data-canonical-src="https://s3.amazonaws.com/github/ribbons/forkme_right_gray_6d6d6d.png"></a>"""
f_base = "<html><head><title>{title}HasNudes</title></head><body>" + f_gh + "<h2>HasNudes</h2>{body}</body></html>"
f_result = "<h3>{moviename} has {number} nudes{dotorq}</h3>"
f_homepage = "<form><input type=text name=movie><input type=checkbox name=json id=json><label for=json>JSON output</label><input type=submit>"
rl = {}
@app.route("/")
def asdf():
trusted_proxies = ['127.0.0.1']
route = request.access_route + [request.remote_addr]
remote_addr = next((addr for addr in reversed(route)
if addr not in trusted_proxies), request.remote_addr)
if request.args.get('movie'):
if len(request.args.get('movie')) > 100 or (remote_addr in rl and time.time()-300 < rl[remote_addr]):
rl[remote_addr] = time.time()
err = "Sorry, you've been rate limited by your request being too long. Try again in five minutes. (More requests will reset the counter :P)"
if 'json' in request.args:
return jsonify(error=err)
else:
return err
info = hasnudes(request.args.get('movie'))
if 'json' in request.args:
return jsonify(q=request.args.get('movie'), nudes=info)
if info:
return f_base.format(title="YES - " if info and int(info[1]) > 0 else "NO",
body=f_result.format(moviename=" ".join(info[0].split(", ")[::-1]),
number=info[1] if int(info[1]) > 0 else "no (recorded)",
dotorq="!" if int(info[1]) > 0 else ". :("))
else:
return f_base.format(title="NO - ",
body=f_result.format(moviename=request.args.get('movie'),
number="no (recorded)",
dotorq=". :("))
if 'json' in request.args:
return jsonify(error="No query ('movie' param needed)")
return f_base.format(title="", body=f_homepage)
def hasnudes(title):
s = set(('the', 'a', 'an'))
word = filter(lambda w: not w in s,title.lower().split())[0]
soup = Soup(requests.get("http://www.celebritymoviearchive.com/tour/looks.php/" + word[0].upper()).text)
info = [[td.text.strip() for td in tr.findAll('td')] for tr in soup.findAll('tr') if tr.find('td') and word in tr.find('td').text.lower()]
if info and len(info) > 0:
info = info[0]
if int(info[1]) > 0:
return info
return False
else:
return False
if __name__ == "__main__":
app.run(port=5213)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment