Skip to content

Instantly share code, notes, and snippets.

@keymon
Created January 15, 2012 04:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save keymon/1614319 to your computer and use it in GitHub Desktop.
Save keymon/1614319 to your computer and use it in GitHub Desktop.
#!/usr/bin/python
# -*- coding: utf-8 -*-
#
# Script: example_query_accesslog.py
import accesslog_parse_mongo
import sys
from datetime import datetime, timedelta
def main():
(mongo_connection, mongo_db, mongo_collection) = \
accesslog_parse_mongo.do_mongo_init()
# Get time > now()-1day and url=sys.argv[1]
if len(sys.argv) > 1:
query = {'timestamp': {'$gt': datetime.now() - timedelta(days=1)},
'url': sys.argv[1]}
else:
query = {'timestamp': {'$gt': datetime.now() - timedelta(days=1)}}
entries = mongo_collection.find(query, )
print "There are %i visits from:" % entries.count()
for entry in entries:
print " - %s at %s from %s" % \
(entry['client_ip'], entry['time'], entry['referer'])
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment