Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
corradio committed Nov 10, 2016
1 parent 831e30e commit a738d3a
Showing 1 changed file with 13 additions and 4 deletions.
17 changes: 13 additions & 4 deletions feeder/feeder.py
Original file line number Diff line number Diff line change
Expand Up @@ -177,10 +177,19 @@ def execute_parser(parser):
print 'future:', obj['datetime'], 'now', arrow.now()
raise Exception("Data from %s can't be in the future" % obj['countryCode'])
try:
col.insert_one(obj)
logger.info('Inserted %s @ %s into the db' % (obj.get('countryCode'), obj.get('datetime')))
logger.debug(obj)
if cache: cache.delete('production')
result = col.update_one(
{ 'countryCode': obj['countryCode'], 'datetime': obj['datetime'] },
{ '$set': obj },
upsert=True)
if result.modified_count:
logger.info('Updated %s @ %s' % (obj.get('countryCode'), obj.get('datetime')))
elif result.matched_count:
logger.debug('Already up to date: %s @ %s' % (obj.get('countryCode'), obj.get('datetime')))
elif result.upserted_id:
logger.info('Inserted %s @ %s' % (obj.get('countryCode'), obj.get('datetime')))
else:
raise Exception('Unknown database command result.')
if (result.modified_count or result.upserted_id) and cache: cache.delete('production')
except pymongo.errors.DuplicateKeyError:
# (datetime, countryCode) does already exist. Don't raise.
# Note: with this design, the oldest record stays.
Expand Down

0 comments on commit a738d3a

Please sign in to comment.