my code stock.com

Get Comment data of AKB48 members only.
Store in MongoDB

Snippet options

Download: Download snippet as ggts_comments_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]
import rfc3339 [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: ' 
    return ggtsidlist

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

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 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)
        try:     
            if 'items' in comments_doc:
                comments += comments_doc['items']
                request = serviceUnauth.activities().list_next(request, comments_doc)
        except:
            print 'serviceUnauth error'
            print sys.exc_info()[:2]
            continue

    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()
                con.ggts.comments.insert(item)
                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:
        commentlist(activityid)

    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.