my code

by cigarmanTH

Get Activity data of ALL AKB48 members.
Store in MongoDB.

Snippet options

Download: Download snippet as
Copy snippet: For this you need a free my code account.
Embed code : You will find the embed code for this snippet at the end of the page, if you want to embed it into a website or a blog!

#! c:/Python26/python.exe
# coding: utf-8 

# at first time in new environment,run to get credentials before run this script
import sys
import codecs
sys.stdout = codecs.getwriter('utf-8')(sys.stdout)

import apiclient.discovery
import httplib2
import settings
import datetime
import time
import dateutil.parser #@UnresolvedImport
from pymongo import Connection #@UnresolvedImport

def ggtsidlist_get():
#get ggtsidlist from collection 'con.ggts.ggtsid' on MongoDB
#so run first
    con = Connection()
    item = []
    ggtsidlist = []
    for item in con.ggts.ggtsid.find():  
            print 'ggtsid get error: ' + item['ggtsid']
    return ggtsidlist

def build_service(credentials, http, api_key=None):
    if ( credentials != None ):
        http = credentials.authorize(http)
    service ='plus', 'v1', http=http, developerKey=api_key)
    return service

def activitylist(ggtsid):

    httpUnauth = httplib2.Http()
    serviceUnauth = build_service(None, httpUnauth, settings.API_KEY)
    request = serviceUnauth.activities().list(userId=ggtsid, collection='public')

    activities = []

    while ( request != None ):
        activities_doc = request.execute(httpUnauth)
        if 'items' in activities_doc:
            activities += activities_doc['items']
        request = serviceUnauth.activities().list_next(request, activities_doc)

    counter = 0       
    adj_hour = datetime.timedelta(hours=-9)
    if len(activities) > 0:

        for item in activities:
            item['_id'] = item['id']
            published_jst_dt = dateutil.parser.parse(item['published']) - adj_hour
            published_jst_d = str(published_jst_dt)[:10]
            updated_jst_dt = dateutil.parser.parse(item['updated']) - adj_hour
            updated_jst_d = str(updated_jst_dt)[:10]            
            item['published_jst_dt'] = published_jst_dt
            item['published_jst_d'] = published_jst_d
            item['updated_jst_dt'] = updated_jst_dt
            item['updated_jst_d'] = updated_jst_d
            con = Connection()
            counter = counter + 1
        print 'completed: ' + item['actor']['displayName'] + ' at: ' + str(time.localtime()) + ' counted:' + str(counter)
        counter = 0
if __name__=='__main__':
#if needed,remove collection    
#    con = Connection()
#    con.ggts.activities.remove()
    print  '--------------------'
    print 'started at ' + str(time.localtime())

    ggtsidlist = ggtsidlist_get()
    for ggtsid in ggtsidlist:

    print 'finished at ' + str(time.localtime())

Create a free my code account now.

my code is a free service, which allows you to save and manage code snippes of any kind and programming language. We provide many advantages for your daily work with code-snippets, also for your teamwork. Give it a try!

Find out more and register now

You can customize the height of iFrame-Codes as needed! You can find more infos in our API Reference for iframe Embeds.