Created
August 6, 2012 21:37
-
-
Save josephmisiti/3278679 to your computer and use it in GitHub Desktop.
Facebook created_time Conversion
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys, datetime | |
from django.core.management.base import BaseCommand, CommandError | |
from optparse import make_option | |
from django.conf import settings | |
from pymongo import Connection | |
from bson.objectid import ObjectId | |
class Command(BaseCommand): | |
help = """Converts all facebook dates to MongoDate Strings So They are Query-able""" | |
def handle(self, *args, **options): | |
mongoDB = Connection()[settings.DB] | |
datapoints = mongoDB['mycollection'].find() | |
for dp in datapoints: | |
if 'lks' not in dp: continue | |
new_fb_likes = [] | |
for lk in dp['lks']: | |
try: | |
strdate = lk['created_time'].split('T')[0] | |
strtime = lk['created_time'].split('T')[1] | |
year = int(strdate.split("-")[0]) | |
month = int(strdate.split("-")[1]) | |
day = int(strdate.split("-")[2]) | |
hour = int(strtime.split("+")[0].split(":")[0]) | |
minute = int(strtime.split("+")[0].split(":")[1]) | |
second = int(strtime.split("+")[0].split(":")[2]) | |
lk['created_time'] = datetime.datetime(year,month,day,hour,minute,second) | |
new_fb_likes.append( lk ) | |
except: | |
new_fb_likes.append( lk ) | |
print mongoDB['mycollection'].update({"_id" : ObjectId(dp['_id']) }, { "$set" : { "lks" : new_fb_likes }}) | |
print 'Updating objectid=%s' % dp['_id'] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment