-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtumblr.py
21 lines (20 loc) · 921 Bytes
/
tumblr.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
import requests
from bs4 import BeautifulSoup
from common import url_to_img
import logging
from common import generate_imgs
import os
class tumblr:
@staticmethod
def get_images_from_profile(url:str, max_n: int = 0):
if not os.environ.get("REAL_TUMBLR", "false").lower() == "true":
logging.warning("Mocking images from Instagram - put `REAL_TUMBLR=true` in .env for real requests")
return generate_imgs(min(max_n,5) if max_n > 0 else 5)
else:
#url = "http://www.%s.tumblr.com" % profile
profile_html: str = requests.get(url).text
soup = BeautifulSoup(profile_html, 'html.parser')
img_elements = soup.select('[src*="64.media.tumblr.com"]')
img_elements = img_elements if max_n == 0 else img_elements[:max_n]
imgs = list(map(lambda x: url_to_img(x["src"]), (img_elements)))
return imgs