my code stock.com

cigarmanTH

ggts_activities_insert.py
by cigarmanTH

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

Snippet options

Download: Download snippet as ggts_activities_insertpy.py.
Copy snippet: For this you need a free my code stock.com 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 plus_cli.py 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 [email protected]
from pymongo import Connection [email protected]

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

def build_service(credentials, http, api_key=None):
    if ( credentials != None ):
        http = credentials.authorize(http)
    service = apiclient.discovery.build('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()
            con.ggts.activities.insert(item)
            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:
        activitylist(ggtsid)

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

Create a free my code stock.com account now.

my code stok.com 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.