import requests
import re
from bs4 import BeautifulSoup
from PIL import Image
import io
import base64
import time
import json
def getimg(url):
for i in range(5):
try:
rs = requests.get(url, timeout=(5,5))
break
except:
time.sleep(1)
try:
b64 = "data:image/png;base64,"+base64.b64encode(rs.content).decode()
buf = io.BytesIO(rs.content)
img = Image.open(buf)
except:
img = Image.new('RGB', (10,10))
b64 = "dummy"
return img,b64
rs = requests.get("https://www.iconfinder.com/iconsets/fatcow")
html = BeautifulSoup(rs.text, 'html.parser')
grid = html.find('div', class_='icon-grid')
db = []
for pageid in range(10000):
for i in range(5):
try:
rs = requests.get(f"https://www.iconfinder.com/iconsets/fatcow/{pageid}?iconset=fatcow")
break
except:
time.sleep(1)
html = BeautifulSoup(rs.text, 'html.parser')
icons = html.find_all('div', class_='icon-preview-img')
if len(icons) == 0: break
for div in icons:
img = div.find('img')
alt = img.get('alt')
url = img.get('src')
#img, b64 = getimg(url)
filename = url.split("/")[-1]
#print(alt)
#display(img)
db.append({
'filename': filename,
'url': url,
#'base64': b64,
'alt': alt,
})
len(db)
with open("fatcow-listplain.json", "w") as f:
json.dump(db, f)
for idx,d in enumerate(db):
if d.get('base64', '') != '': continue
url = d['url']
img, b64 = getimg(url)
print(idx, end=' ')
d['base64'] = b64
for idx,d in enumerate(db):
if d.get('base64', '') != '' and \
d.get('base64', '') != 'dummy': continue
url = d['url']
img, b64 = getimg(url)
print(idx, end=' ')
d['base64'] = b64
for d in db:
if d['base64'] == 'dummy':
print(d)
with open("fatcow-icons.json", "w") as f:
json.dump(db, f)
!du -sh fatcow-icons.json
from IPython.display import IFrame
from IPython.core.display import HTML
from PIL import Image
def add_margin(pil_img, top, right, bottom, left, color):
width, height = pil_img.size
new_width = width + right + left
new_height = height + top + bottom
result = Image.new(pil_img.mode, (new_width, new_height), color)
result.paste(pil_img, (left, top))
return result
#https://stackoverflow.com/questions/48229318/how-to-convert-image-pil-into-base64-without-saving/48229407
#by @Taha Mahjoubi
def img_to_base64_str(img):
buffered = io.BytesIO()
img.save(buffered, format="PNG")
buffered.seek(0)
img_byte = buffered.getvalue()
img_str = "data:image/png;base64," + base64.b64encode(img_byte).decode()
return img_str
def img_from_base64_str(msg):
msg = msg.replace("data:image/png;base64,", "")
msg = base64.b64decode(msg)
buf = io.BytesIO(msg)
img = Image.open(buf)
return img
html = """
<html>
<head>
<style>
ul{
list-style:none;
}
li {
font-size:11px;
width:60px;
height:60px;
text-align:center;
float:left;
}
</style>
</head>
<body>
BODY
</body>
</html>
"""
body = []
body.append("<ul>")
for d in db:
if d['base64'] == 'dummy': continue
try:
img = img_from_base64_str(d['base64'])
alt = d['alt']
img_new = add_margin(img, 4, 0, 0, 0, (255,255,255,0))
b64 = img_to_base64_str(img_new)
body.append(f"""
<li>
<img src="{b64}"/><br>
{alt}
</li>
""")
except:
print("failed")
display(img)
body.append("</ul>")
body = "\n".join(body)
html = html.replace('BODY', body)
htmlfile = "fatcow.html"
with open(htmlfile, "w") as f:
f.write(html)
display(IFrame(htmlfile, '100%', '800px'))
db[0]
with open("fatcow-icons.json", "w") as f:
json.dump(db, f)
!du -sh fatcow-icons.json
for line in rs.text.split("\n"):
if "key_j.png" in line:
print(line)