Skip to content

Instantly share code, notes, and snippets.

@jonathanbarton
Last active January 5, 2017 01:00
Show Gist options
  • Save jonathanbarton/5f6a4f118c927b00ccbeaae003df9b32 to your computer and use it in GitHub Desktop.
Save jonathanbarton/5f6a4f118c927b00ccbeaae003df9b32 to your computer and use it in GitHub Desktop.
giphy.py
import giphypop
import urllib
import os
FILE_TYPE = '.gif'
BASE_PATH = os.environ.get('BASE_PATH') or '/usr/local/mhe-dashboard'
GIF_COUNT = os.environ.get('GIF_COUNT') or 20
DAYS_TO_KEEP = 5
def get_filename(media_url):
url_parts = media_url.split('/')
return url_parts[len(url_parts)-2]
def save_giphy(gif):
filename = get_filename(gif.media_url)
f = open(os.path.join(BASE_PATH, filename + FILE_TYPE), 'wb')
f.write(urllib.urlopen(gif.media_url).read())
f.close()
def prune_old_files():
now = time.time()
path = BASE_PATH
for f in os.listdir(path):
if os.stat(os.path.join(path,f)).st_mtime < now - DAYS_TO_KEEP * 86400:
os.remove(os.path.join(path, f))
if __name__ == "__main__":
giphy = giphypop.Giphy()
daily_gifs = [giphy.screensaver() for _ in range(GIF_COUNT)]
for gif in daily_gifs:
save_giphy(gif)
prune_old_files()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment