かずきち。の日記

Pythonエンジニア&元東大生のつぶやき

pymixiで送信済みメッセージのidを抜く

さて、pythonmixiの送信済みメッセージの重複なしでidを抜いてDBに入れるプログラムを作りましたよ。
mixiidのDBにはuserid(メールアドレス)、password(パスワード)、mixiid(ID)を格納しています。
あとはse+(mixiid)のテーブルを作って、どんどん送信済みメッセージのidを格納してくれます。
[id:kazukichi_0914:20091101:1257100540]
からsend_messageモジュールを入れてください。

#!/usr/local/bin/python
# -*- coding: utf-8 -*-

import mixi
import sys, time
import re
import MySQLdb

def get_mixi_message():
    try:
        proxy = None
        encoding = "utf8"
        target = []
        maxitem = 30
        with_content = True
        fout = sys.stdout
    except:
        return 1
    if not userid or not password:
        return 1
    
    try:
        m = mixi.MIXI(proxy)
        m.login(userid, password)
    except:
        pass

    check = "SELECT mixiid FROM se"+str(mixiid)
    try:
        cur.execute(check)
        print "Already exist database"
    except:
        create = "create table se"+str(mixiid)+" (mixiid int,nickname CHAR(255))"
        cur.execute(create)
        print "create new database"
   
    l = m.send_message()
    
    for i in l:
        x1 = i["mixiid"]
        x2 = i["nickname"]
        x2 = re.sub("'","",x2)
        x2 = re.sub("\+","",x2)
        x2 = re.sub("\"'","",x2)
        x2 = re.sub(",'","",x2)
        print x1
        print x2


        search = "SELECT mixiid FROM se"+str(mixiid)+" WHERE mixiid = "+str(x1)
        if cur.execute(search)== 0:
            insert = "INSERT INTO se"+str(mixiid)+" VALUES("+str(x1)+",'"+str(x2)+"')"
            #print insert
            cur.execute(insert)
            print "create new data"

if __name__ == "__main__":
    connect = MySQLdb.connect(db="hoge", host="localhost", port=3306, user="hoge", passwd="hoge")
    cur = connect.cursor()

    search = "SELECT * FROM account ORDER BY mixiid DESC"
    cur.execute(search)
    rows = cur.fetchall()
    for row in rows:
        userid = str(row[0])
        password = str(row[1])
        mixiid = str(row[2])
        get_mixi_message()
広告を非表示にする