Example #1
0
def main(argvs, argc):
    """
    このスクリプトでツイッターの情報を記録するデータベースを作成します
    """
    if (argc != 2):
        print "Usage #python %s dbname" % argvs[0]
        return -1

    dbname = argvs[1]
    if os.path.exists(dbname):
        print dbname, " already exists."
        return -1

    db = TwitterDb(dbname)
    db.create_db()
    print db.dbpath, " is created."

    return 0
Example #2
0
def main(argvs, argc):
    if argc != 4:
        print "Usage: python search_to_db.py dbpath term since_id"
        return -1

    dbpath = argvs[1]
    term = argvs[2]
    since_id = int(argvs[3])

    conf = ConfigParser.SafeConfigParser()
    conf.read("twitter.ini")

    print "==================================="
    print "DB path:", dbpath
    print "term :", term
    print "since_id :", since_id
    print "==================================="
    # もし、UTF-8のターミナルなら以下の行はコメントアウトする
    term = unicode(term, 'cp932').encode('utf-8')
    tw = TweeterSearcher(consumer_key=conf.get('Twitter', 'consumer_key'),
                         consumer_secret=conf.get('Twitter',
                                                  'consumer_secret'),
                         access_token_key=conf.get('Twitter',
                                                   'access_token_key'),
                         access_token_secret=conf.get('Twitter',
                                                      'access_token_secret'))
    db = TwitterDb(dbpath)
    cond = db.GetCondition(term)
    tw_max_id = None
    tw_since_id = None
    cond_id = None
    if cond is None:
        tw_since_id = since_id
        db.SetCondition(None, term, 0, 0, 0, since_id)
        cond = db.GetCondition(term)
        cond_id = cond['id']
    else:
        cond_id = cond['id']
        if cond['found_since']:
            tw_since_id = cond['max_id'] + 1
        else:
            tw_since_id = since_id
            if cond['min_id'] > 0:
                tw_max_id = cond['min_id'] - 1
    print "tw_since_id :", tw_since_id
    print "tw_max_id :", tw_max_id

    ret = tw.StartSearch(term, tw_since_id, tw_max_id)
    found = 0
    for t in tw.GetTweets():
        utc = dateutil.parser.parse(t['createdtime']).utctimetuple()
        tmstamp = time.mktime(utc)
        db.AppendTweet(cond_id, t['id'], t['text'], t['user_id'], tmstamp)
    if ret:
        # 全レコードを読み込んだ
        print "All tweets is imported."
        found = 1

    if len(tw.GetTweets()) > 0:
        db.SetCondition(cond_id, term, found, tw.max_id, tw.min_id, since_id)
        print "Import %d" % (len(tw.GetTweets()))

    db.Commit()

    tm = tw.GetSearchReset()
    print "REMAING:%d RESET-TIME: %d:%d" % (tw.GetSearchRemaining(),
                                            tm.tm_hour, tm.tm_min)

    return 0
Example #3
0
 def setUp(self):
     self._db = TwitterDb(self._dbpath)
     self._db.create_db()