Created
October 4, 2014 17:41
-
-
Save nubela/c22cf219cd2e8cd2cfed to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import datetime | |
import random | |
from BeautifulSoup import BeautifulSoup | |
from base.util import generate_uuid | |
from homepage.app import homepage_db | |
from homepage.model import UnsplashImage | |
from homepage_cfg import EXPIRE_UNSPLASH_DAYS, UNSPLASH_IMAGE_DIR | |
import os | |
import requests | |
from sqlalchemy import asc, desc, func | |
def update(): | |
n = 1 | |
total_images = 0 | |
while total_images <= 20 and n < 10: | |
url = "https://unsplash.com/?page=%d" % (n) | |
r = requests.get(url) | |
if r.status_code == 200: | |
soup = BeautifulSoup(r.text) | |
all_photos = soup.findAll("div", {"class": "photo"}) | |
for p in all_photos: | |
image_url = p.a.img["src"] | |
if get(image_url) is not None: | |
total_images += 1 | |
continue | |
print "Downloading %d: %s" % (total_images + 1, image_url) | |
file_name = download_url(image_url) | |
unsplash_img = UnsplashImage() | |
unsplash_img.id = image_url | |
unsplash_img.utc_date_created = datetime.datetime.utcnow() | |
unsplash_img.filename = file_name | |
save(unsplash_img) | |
total_images += 1 | |
n += 1 | |
def save(o): | |
homepage_db.add(o) | |
homepage_db.commit() | |
def delete(o): | |
homepage_db.delete(o) | |
homepage_db.commit() | |
def _does_file_exists(file_path): | |
return os.path.exists(file_path); | |
def random_image(): | |
unsplash_img_lis = (homepage_db. | |
query(UnsplashImage) | |
).all() | |
return random.choice(unsplash_img_lis) | |
def fetch(): | |
#get unexpired image | |
unsplash_img = (homepage_db. | |
query(UnsplashImage). | |
filter(UnsplashImage.utc_expiry != None). | |
filter(UnsplashImage.utc_expiry > datetime.datetime.utcnow()) | |
).first() | |
if unsplash_img is not None: | |
return unsplash_img | |
#get unused image | |
unused_img = (homepage_db. | |
query(UnsplashImage). | |
filter(UnsplashImage.utc_expiry == None). | |
order_by(asc(UnsplashImage.utc_date_created)) | |
).first() | |
if unused_img is not None: | |
unused_img.utc_expiry = datetime.datetime.utcnow() + datetime.timedelta(days=EXPIRE_UNSPLASH_DAYS) | |
save(unused_img) | |
return unused_img | |
#get latest expired image | |
latest_img = (homepage_db. | |
query(UnsplashImage). | |
order_by(desc(UnsplashImage.utc_expiry)) | |
).first() | |
return latest_img | |
def download_url(url): | |
file_name = "%s.jpg" % (generate_uuid()) | |
file_path = os.path.join(UNSPLASH_IMAGE_DIR, file_name) | |
r = requests.get(url, allow_redirects=True) | |
with open(file_path, 'wb') as f: | |
f.write(r.content) | |
return file_name | |
def get(id): | |
return (homepage_db. | |
query(UnsplashImage). | |
filter(UnsplashImage.id == unicode(id)) | |
).first() if id is not None else None |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment