my code

Get Comment data of AKB48 members only.
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
import rfc3339 #@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: ' 
    return ggtsidlist

def activityid_get():
#get ggtsidlist from collection 'con.ggts.ggtsid' on MongoDB
#so run first
    con = Connection()
    item = []
    activityidlist = []
    diff_day = datetime.timedelta(days=365)
    d = rfc3339.rfc3339(,utc=True)
    for item in con.ggts.activities.find({"published": {"$gt" : d }}):  
            print 'activityidlist get error: ' + item['id']
    return activityidlist

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 commentlist(activityid):

    httpUnauth = httplib2.Http()
    serviceUnauth = build_service(None, httpUnauth, settings.API_KEY)
    request = serviceUnauth.comments().list(activityId=activityid)

    comments = []
    adj_hour = datetime.timedelta(hours=-9)

    while ( request != None ):
        comments_doc = request.execute(httpUnauth)
            if 'items' in comments_doc:
                comments += comments_doc['items']
                request = serviceUnauth.activities().list_next(request, comments_doc)
            print 'serviceUnauth error'
            print sys.exc_info()[:2]

    counter = 0       
    if len(comments) > 0:
        ggtsidlist = ggtsidlist_get()
        for item in comments:
            if item['actor']['id'] in ggtsidlist:
                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: ' + activityid + ', ' + item['published'] + ',' +' at: ' + str(time.localtime()) + ' counted:' + str(counter)
        counter = 0
if __name__=='__main__':
#if needed,remove collection    
#    con = Connection()
#    con.ggts.comments.remove()
    print  '--------------------'
    print 'started at ' + str(time.localtime())

    activity = activityid_get()
    for activityid in activity:

    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.