Skip to content

Commit

Permalink
Merge pull request #17 from DankCity/add-cli-commands
Browse files Browse the repository at this point in the history
Add CLI flags
  • Loading branch information
levi-rs authored Sep 9, 2017
2 parents 98871a1 + 022ec5f commit 58c5dd9
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 14 deletions.
12 changes: 12 additions & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
@@ -1,7 +1,19 @@
CHANGES
=======

0.1.2
-----

* Add retry logic for ConnectionError exceptions

0.1.1
-----

* Update README with virtualenv instructions

0.1.0
-----

* Update classifiers for beta release
* Fix timestamping
* Fix setup.cfg formatting
Expand Down
40 changes: 26 additions & 14 deletions recho/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,7 @@
from recho.recho import get_reddit_posts_since, post_to_slack

CONFIG_NAME = '.recho.ini'

TS_NAME = '.recho_timestamps'
TS_FILEPATH = os.path.join(os.path.dirname(__file__), TS_NAME)


class RechoError(Exception):
Expand All @@ -26,35 +24,49 @@ def _get_args():
help="The name of the target Redditor"
)

parser.add_argument(
'-V', '--version', action='version',
version='{version}'.format(version=recho_version)
)

default_config_path = os.path.join(os.path.expanduser('~'), CONFIG_NAME)
parser.add_argument(
'--config', type=str, default="{0}".format(default_config_path),
help="Location of the recho config file")

default_ts_path = os.path.join(os.path.dirname(__file__), TS_NAME)
parser.add_argument(
'--timestamp-file', type=str, default="{0}".format(default_ts_path),
help="Location of the recho config file")

return parser.parse_args()


def _load_config():
def _load_config(config_path):
""" Load the configuration file """
config = ConfigParser()
config_path = os.path.join(os.path.expanduser('~'), CONFIG_NAME)

if not os.path.exists(config_path):
raise RechoError("Config file must be present in ~/.recho.ini")
raise RechoError("Could not find config file at: {0}".format(config_path))

config.read(config_path)

return config


def _get_last_seen(redditor):
def _get_last_seen(redditor, ts_path):
""" Return the last time a check was done """
if not os.path.exists(TS_FILEPATH):
with open(TS_FILEPATH, 'w') as w: # pylint: disable=C0103
if not os.path.exists(ts_path):
with open(ts_path, 'w') as w: # pylint: disable=C0103
w.write(json.dumps({}))

with open(TS_FILEPATH, 'r') as rfile:
with open(ts_path, 'r') as rfile:
timestamps = json.load(rfile)

if redditor not in timestamps:
last_seen = dt.utcnow()
timestamps[redditor] = last_seen.timestamp()
with open(TS_FILEPATH, 'w') as w: # pylint: disable=C0103
with open(ts_path, 'w') as w: # pylint: disable=C0103
w.write(json.dumps(timestamps, indent=4, sort_keys=True))
else:
last_seen = dt.fromtimestamp(float(timestamps[redditor]))
Expand All @@ -67,11 +79,11 @@ def main():
args = _get_args()

# Load config
config = _load_config()
config = _load_config(args.config)

try:
# Get last timestamp
last_seen = _get_last_seen(args.redditor)
last_seen = _get_last_seen(args.redditor, args.timestamp_file)

# Get new comments and threads from reddit
new_posts = get_reddit_posts_since(args.redditor, last_seen)
Expand All @@ -81,11 +93,11 @@ def main():
latest_timestamp = post_to_slack(config['slack'], new_posts)

# Write new timestamp
with open(TS_FILEPATH, 'r') as r: # pylint: disable=C0103
with open(args.timestamp_file, 'r') as r: # pylint: disable=C0103
timestamps = json.load(r)

timestamps[args.redditor] = latest_timestamp.timestamp()
with open(TS_FILEPATH, 'w') as w: # pylint: disable=C0103
with open(args.timestamp_file, 'w') as w: # pylint: disable=C0103
w.write(json.dumps(timestamps, indent=4, sort_keys=True))
except:
# Log to sentry, if configured
Expand Down

0 comments on commit 58c5dd9

Please sign in to comment.