0

それで、私はPythonにかなり慣れていないので、今日、私の担当者のためにstackoverflowをポーリングするスクリプトを作成するという考えがありました.

電子メールの部分は機能しますが、何らかの理由でポーリングを正しく行うことができないため、皆さんがそれを試してみたいかどうかを確認することにしました.

これが私のコードです:

import sys
from stackauth import StackAuth
from stackexchange import Site, StackOverflow
import smtplib

from email.MIMEMultipart import MIMEMultipart
from email.MIMEBase import MIMEBase
from email.MIMEText import MIMEText
from email import Encoders
import os

import time

gmail_user = "email@gmail.com"
gmail_pwd = "password"

def mail(to, subject, text):
   msg = MIMEMultipart()

   msg['From'] = gmail_user
   msg['To'] = to
   msg['Subject'] = subject

   msg.attach(MIMEText(text))

   mailServer = smtplib.SMTP("smtp.gmail.com", 587)
   mailServer.ehlo()
   mailServer.starttls()
   mailServer.ehlo()
   mailServer.login(gmail_user, gmail_pwd)
   mailServer.sendmail(gmail_user, to, msg.as_string())
   # Should be mailServer.quit(), but that crashes...
   mailServer.close()

old_rep = None

while True:

    user_id = 731221 if len(sys.argv) < 2 else int(sys.argv[1])
    print 'StackOverflow user %d\'s accounts:' % user_id

    stack_auth = StackAuth()
    so = Site(StackOverflow)
        accounts = stack_auth.associated(so, user_id)
    REP =  accounts[3].reputation
    print REP
        if REP != old_rep:
        old_rep = REP
                mail("email@email.com","REP",str(REP))
    time.sleep(10)

現在、REP を印刷すると最初は正しいのですが、担当者が変更されても更新されません。理想的にはそうなります。どんな助けでも大歓迎です。前もって感謝します。

4

1 に答える 1

1

これは、適切にループする単純化された例です。

import time
from stackauth import StackAuth
from stackexchange import Site, StackOverflow

rep = None
while True:
    stack_auth = StackAuth()
    so = Site(StackOverflow)
    accounts = stack_auth.associated(so, 641766) # using my id
    so_acct = filter(lambda x: x.on_site.api_endpoint.endswith('api.stackoverflow.com'), accounts)[0] # filtering my accounts so I only check rep on stackoverflow
    if rep != so_acct.reputation:
        rep = so_acct.reputation
        print rep
        # send e-mail
    time.sleep(30)

アカウントをフィルタリングする行を追加して、適切なサイトの担当者のみをチェックするようにしました. あなたはインデックスを使用していましたが、それが安定しているかどうかはわかりません。(元の例のように) 10 秒ごとにポーリングするのは少しやり過ぎかもしれません。担当者の最新の更新が本当に必要ですか? これを cron ジョブとして記述し、5、10、15 分ごとに実行することを検討してください。

于 2011-05-07T02:13:14.507 に答える