forked from techytushar/random-memer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
66 lines (56 loc) · 1.73 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
@Author : Tushar Mittal (chiragmittal.mittal@gmail.com)
Flask API to return random meme images
"""
import random
import requests
from bs4 import BeautifulSoup
from flask import Flask, send_file
from PIL import Image
from io import BytesIO
app = Flask(__name__)
def get_new_memes():
"""Scrapers the website and extracts image URLs
Returns:
imgs [list]: List of image URLs
"""
url = 'https://www.memedroid.com/memes/tag/programming'
response = requests.get(url)
soup = BeautifulSoup(response.content, 'lxml')
divs = soup.find_all('div', class_='item-aux-container')
imgs = []
for div in divs:
img = div.find('img')['src']
if img.startswith('http') and img.endswith('jpeg'):
imgs.append(img)
return imgs
def serve_pil_image(pil_img):
"""Stores the downloaded image file in-memory
and sends it as response
Args:
pil_img: Pillow Image object
Returns:
[response]: Sends image file as response
"""
img_io = BytesIO()
pil_img.save(img_io, 'JPEG', quality=70)
img_io.seek(0)
return send_file(img_io, mimetype='image/jpeg')
@app.after_request
def set_response_headers(response):
"""Sets Cache-Control header to no-cache so GitHub
fetches new image everytime
"""
response.headers['Cache-Control'] = 'no-cache, no-store, must-revalidate'
response.headers['Pragma'] = 'no-cache'
response.headers['Expires'] = '0'
return response
@app.route("/", methods=['GET'])
def return_meme():
img_url = random.choice(get_new_memes())
res = requests.get(img_url, stream=True)
res.raw.decode_content = True
img = Image.open(res.raw)
return serve_pil_image(img)