Skip to content

Commit

Permalink
add file cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniel D. Beck committed Feb 17, 2017
1 parent dac4d5d commit 096f198
Showing 1 changed file with 29 additions and 1 deletion.
30 changes: 29 additions & 1 deletion starnearyou.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from time import sleep
import shutil
import subprocess
import time
import urllib.parse

import click
Expand All @@ -29,6 +30,8 @@
"{year:04d}/{month:02d}/{day:02d}/")
DEST_FILENAME_TEMPLATE = "{year:04d}_{month:02d}_{day:02d}_{hour:02d}.gif"

DELETION_LIMIT = 24 * 60 * 60


# ======================
# CLI callback functions
Expand Down Expand Up @@ -143,6 +146,8 @@ def cli(work_dir, tweet, auth_info, logfile, loglevel):

logger.info("Tweeted http://twitter.com/starnearyou/status/%s",
tweet_response[u'id_str'])

clean_up(work_dir)
return
except twython.exceptions.TwythonError as err:
logger.exception("Tweeting failed: %r", err)
Expand Down Expand Up @@ -190,7 +195,6 @@ def configure_logging(filename=None, level=logging.INFO):

def make_sun_gif(work_dir):
"""Fetch and make the latest Sun GIF in `work_dir`."""
logger.info("start_timeing to generate a GIF")
download_dir = os.path.join(work_dir, 'originals')
gifs_dir = os.path.join(work_dir, 'gifs')

Expand Down Expand Up @@ -349,6 +353,22 @@ def optimize_gif(source, dest):
logger.debug("Optimized GIF saved: %s", dest)


def clean_up(work_dir):
logger.debug("Cleaning up downloaded files")

download_dir = os.path.join(work_dir, 'originals')
deletion_count = 0

for dirpath, dirnames, filenames in os.walk(download_dir):
for filename in filenames:
fpath = os.path.join(dirpath, filename)
if is_file_too_old(fpath):
logger.debug('Deleting %s', fpath)
deletion_count += 1
os.remove(fpath)

logger.info('Deleted %s files', deletion_count)

# =========
# Utilities
# =========
Expand All @@ -358,5 +378,13 @@ def split_url(url):
return os.path.basename(urllib.parse.urlparse(url).path)


def is_file_too_old(fpath):
"""Return whether the file is older than the `DELETION_LIMIT` threshhold in
seconds.
"""
mtime = os.path.getmtime(fpath)
return (time.time() - mtime) > DELETION_LIMIT


if __name__ == '__main__':
cli()

0 comments on commit 096f198

Please sign in to comment.