#!/usr/bin/env python3   """ A (semi-nice) downloader for e621.net, which aims "to archive the best/strangest/most excellent animal/anthro-related artwork, regardless of content, for all those who wish to view it."  May Sköll be merciful on my soul. """   import os.path import random import tempfile import time import urllib.request     # gets the image data for an image with the specified ID def get_image_by_id(imgid) :     url = "https://e621.net/post/show/" + imgid + "/"     pagereq = urllib.request.urlopen(url)     line = None     for tline in pagereq:         tline = tline.decode('utf-8')  # Decoding the binary data to text.         if "Download" in tline :             line = tline             break     pagereq.close()     if not line :         return None     download = line.strip().lstrip('| Download')     imageurl = "http://e621.net"+download     response = urllib.request.urlopen(imageurl)     data = response.read()     response.close()     return data   # saves an image with the specified ID def save_image_by_id(imgid, filedir=tempfile.gettempdir()) :     data = get_image_by_id(imgid)     magic = data[0:4]     imagetype = ".img"     if (data[0:4] == b'GIF8') :         imagetype = ".gif"     elif (data[0:8] == b'\x89PNG\r\n\x1a\n') :         imagetype = ".png"     elif (data[0:2]==b'\xff\xd8' or data[0:4]=='JFIF' or data[0:4]==b'Exif') :         imagetype = ".jpg"     fullpath = os.path.join(filedir,imgid) + imagetype     outfile = open(fullpath,'wb')     outfile.write(data)     outfile.close()     return fullpath     def save_images_from_page(url, filedir=tempfile.gettempdir()) :     pagereq = urllib.request.urlopen(url)     imgids = []     for tline in pagereq:         tline = tline.decode('utf-8')  # Decoding the binary data to text.         if 'thumb blacklist' in tline :             imgids.append(tline.strip().lstrip(''))     pagereq.close()     for imgid in imgids :         save_image_by_id(imgid, filedir)         time.sleep(3 + random.random())     return imgids       def save_images_from_search(tags=[]) :     url = "https://e621.net/post/index?tags="     url += '+'.join([tag.replace(' ','_') for tag in tags])     page = 1     while True :         liturl = url + "&page=" + str(page)         results = save_images_from_page(url)         if len(results) == 0 :             break     page += 1     def save_images_from_pool(poolid) :     url = "https://e621.net/pool/show/" + str(poolid)     page = 1     while True :         liturl = url + "&page=" + str(page)         results = save_images_from_page(url, "/tmp/"+str(poolid)+"/")         if len(results) == 0 :             break         page += 1     #save_images_from_search(['wrincewind']) #save_images_from_pool(1874)