A script that starts a local server at 127.0.0.1:8000
and serves random images from booru sites indexed by the cure.ninja/booru API. I wrote mostly this because webms seem to be broken on the site even after disabling referrer headers, but also because I'm planning to maybe make this into a fap gauntlet thing later.
Requires Python 3, uses only standard library modules. Images are not saved to the hard drive.
Originally intended to implement this in HTML, but it turns out working around CORS is a pain in the ass.
Usage: run python3 file.py
, then point your browser to 127.0.0.1:8000
. Refresh the page or click on the image.
query = "-s"
# change this query to whatever, ex. "tanline+naughty_face+-s"
# See https://cure.ninja/booru/about/api for more information.
# This weeaboo porn scraper is licensed under the
# DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
# Version 2, December 2004
#
# Copyright (C) 2xxx ure mum m8
#
# Everyone is permitted to copy and distribute verbatim or modified
# copies of this license document, and changing it is allowed as long
# as the name is changed.
#
# DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
# TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
#
# 0. You just DO WHAT THE FUCK YOU WANT TO.
import http.server
from urllib.request import Request, urlopen
import socketserver
import json
import sys
import time
from datetime import datetime, timedelta
PORT = 8000
order = "r"# r=random, see API details for more options
request = {}
results = []
curImg = 0
page = 1
start = time.time()
def getElapsed():
sec = timedelta(seconds=time.time()-start)
d = datetime(1,1,1) + sec
return str(d.hour) + " hours " + str(d.minute) + " minutes " + str(d.second) + " seconds"
def fetch(u):
print("fetching " +u)
r = Request(url=u)
r.add_header("User-Agent", "Mozilla/5.0 ;Windows NT 6.1; WOW64; Trident/7.0; rv:11.0; like Gecko")
response = urlopen(r)
return response
def fetchResults():
global query, order, page, results
requrl = "https://cure.ninja/booru/api/json/"+str(page)+"?o="+order+"&f=a&q="+query
res = fetch(requrl)
jsn = json.loads(res.read().decode("utf-8"))
for r in jsn["results"]:
results.append(r)
page += 1
def getType(url):
ctype = ""
urlend = url[-10:]
if urlend.count(".jpg") or urlend.count(".jpeg"):
ctype = "image/jpg"
elif urlend.count(".gif"):
ctype = "image/gif"
elif urlend.count(".png"):
ctype = "image/png"
elif urlend.count(".webm"):
ctype = "video/webm"
return ctype
class Handler(http.server.SimpleHTTPRequestHandler):
def do_GET(self):
self.send_response(200)
if self.path.count("i="):
iurl = self.path[self.path.index("i=")+2:]
self.serveImage(iurl)
else:
self.servePage()
def serveImage(self, url):
ftype = ""
img = fetch(url)
data = img.read()
ctype = getType(url)
self.send_header("Content-type", ctype)
self.send_header("Content-length", sys.getsizeof(data))
self.end_headers()
self.wfile.write(data)
def servePage(self):
global curImg, page, results
self.send_header("Content-type","text/html")
self.end_headers()
if (curImg) >= len(results)-1:
fetchResults()
self.wfile.write(bytes(
("""
<!DOCTYPE HTML>
<html>
<head>
<title>"""+str(curImg)+": "+results[curImg]["tags"]+"""</title>
</head>
<body>
"""+
("""
<a href='http://127.0.0.1:8000/"""+str(curImg+1)+"""'>
<img style="max-width:100%" src='http:127.0.0.1:8000/i="""+results[curImg]["url"]+"""'>
</a>
"""
if getType(results[curImg]["url"]) != "video/webm"
else """
<video controls autoplay loop=1>
<source type='video/webm' src='"""+results[curImg]["url"]+"""' >
</video>
"""
)
+"""<p><a href='"""+results[curImg]["page"]+"""'>"""+results[curImg]["page"]+"""</a></p>
<p><a href='"""+results[curImg]["url"]+"""'>"""+results[curImg]["url"]+"""</a></p>
<p>"""+results[curImg]["tags"]+"""</p>
<p>query string: """+query+"""</p>
<p>Images loaded: """+str(curImg+1)+""", time elapsed since server start: """+getElapsed()+"""</p>
</body>
</html>
"""), "utf-8")
)
curImg += 1
if __name__ == "__main__":
server = socketserver.TCPServer(('', PORT), Handler)
try:
print("Server starting at 127.0.0.1:"+str(PORT))
server.serve_forever()
except:
server.socket.close()