def sendTmt(userName,toMail): # 引数 ユーザ名 メールアドレス getUser = userName print sendTmt, userName, toMail conf = auth_api.loadJSON(homePath+"/config.json") tw = auth_api.connect(conf["consumer_token"], conf["consumer_secret"], homePath) a = datetime.datetime.today() timeMax = a - datetime.timedelta(hours = a.hour - 6, minutes = a.minute, seconds = a.second) timeMin = timeMax - datetime.timedelta(days=1) #latestTime = datetime.datetime.today()-datetime.timedelta(days=1) pageNum = 50 header = u" :: Today's my tweets ::\n"+unicode(getUser,"utf-8")+u"さんの一日の発言です。\n\n" header = header.encode("iso-2022-jp") outSentence = "" try: flag = True for i in range(pageNum): if flag != True : break print getUser, pageNum for t in tw.user_timeline(getUser,page = i): td = toDate3.toDate3(t.created_at) if td - timeMin < datetime.timedelta(days =0): flag = False break if td - timeMax > datetime.timedelta(days = 0): continue #if t[0].startswith("@") or t[0].startswith(".@"): # continue s = unicode(t.text).encode("iso-2022-jp","ignore") outSentence = s + " " + td.strftime('%Y/%m/%d %H:%M:%S') + "\n\n" + outSentence except: traceback.print_exc() outSentence = header + outSentence outSentence += "\n Today's my tweets by showyou(twitter.com/showyou)\n" #print unicode(outSentence,"iso-2022-jp").encode("cp932") userdata2 = jsonfile.read(homePath + "/user/twdata_tmt") user = userdata2["user"] passWord = userdata2["pass"] from_addr = user to_addr = toMail title = "today's my tweets:"+timeMin.strftime('%Y/%m/%d') title = title.encode("iso-2022-jp","ignore") msg = mail.create_message2(from_addr, to_addr, title, outSentence, 'ISO-2022-JP') mail.send_via_gmail(user,passWord,from_addr, to_addr, msg)
def analyzeDM(userData,tw,latest): # DM 取得解析 a = tw.direct_messages() a.reverse() for x in a: user_name = x.sender_screen_name text = x.text created_at = x.created_at print toDate3.toDate3(created_at) - latest # 最終更新時刻以前のログはカット if toDate3.toDate3(created_at) - latest < datetime.timedelta(days =0) : print "pass" continue; xs = text#.encode("utf-8") cmd = xs.split()#xsをスペースで分割 #xs の解析で命令処理 flag = False id = -1 for u in range(len(userData)): if userData[u]["user"] == user_name: flag = True id = u break if cmd[0] == "start": print cmd print len(cmd) if len(cmd) < 2: print "error:start needs 1 arg" continue if flag : # keyが存在:更新 userData[id]["mail"] = cmd[1] else: d = {} d["user"] = user_name d["mail"] = cmd[1] d["now"] = False d["time"] = latest userData.append(d) elif cmd[0] == "get": if len(cmd) < 2: if flag: toMail = userData[i]["mail"] else: print "error: not registered user and none mail address" continue else: toMail = cmd[1] if flag : # keyが存在:更新 userData[id]["mail"] = toMail userData[id]["now"] = True else: d = {} d["user"] = user_name d["mail"] = toMail d["now"] = True userData.append(d) elif cmd[0] == "stop": if flag == False: print "error:stop - no key" continue print "delete user", print userData[id]["user"] del userData[id] else: print "error:no cmd"+xs