Python rss不会立即更新

Python rss不会立即更新,python,rss,alert,Python,Rss,Alert,我有一个小脚本,专门用于监视RSS中的“用python标记的新问题”。它在循环的第一次迭代中将提要存储在一个变量中,然后根据存储在变量中的提要不断检查提要。如果提要发生更改,它会更新变量并将最新条目输出到控制台,并播放声音文件提醒我有新问题。总而言之,它很方便,因为我什么都不用看。然而,在实际发布的新问题和我的脚本检测提要更新之间存在时间差异。这些差异似乎在时间长度上有所不同,但一般来说,它不是即时的,而且往往不会在对一个问题采取足够的行动之前提醒我,因此它几乎已经得到了处理。不总是这样,但一般

我有一个小脚本,专门用于监视RSS中的“用python标记的新问题”。它在循环的第一次迭代中将提要存储在一个变量中,然后根据存储在变量中的提要不断检查提要。如果提要发生更改,它会更新变量并将最新条目输出到控制台,并播放声音文件提醒我有新问题。总而言之,它很方便,因为我什么都不用看。然而,在实际发布的新问题和我的脚本检测提要更新之间存在时间差异。这些差异似乎在时间长度上有所不同,但一般来说,它不是即时的,而且往往不会在对一个问题采取足够的行动之前提醒我,因此它几乎已经得到了处理。不总是这样,但一般来说。我是否有办法确保更新/警报更快?或者这是最好的吗?我突然想到,只有在对某个问题采取实际行动时,才会更新此特定提要。。有人知道是不是这样吗

我是否误解了rss的实际工作方式

import urllib2
import mp3play
import time
from xml.dom import minidom



def SO_notify():
    """ play alarm when rss is updated """

rss = ''
filename = "path_to_soundfile"
mp3 = mp3play.load(filename)
mp3.volume(25)

while True:  
    html = urllib2.urlopen("http://stackoverflow.com/feeds/tag?tagnames=python&sort=newest")
    new_rss = html.read()
    if new_rss == rss:
        continue
    rss = new_rss
    feed = minidom.parseString(rss)
    new_entry = feed.getElementsByTagName('entry')[0]
    title = new_entry.getElementsByTagName('title')[0].childNodes[0].nodeValue
    print title
    mp3.play()
    time.sleep(30) #Edit - thanks to all who suggested this

SO_notify()
比如:

import requests
import mp3play
import time

curr_ids = []
filename = "path_to_soundfile"
mp3 = mp3play.load(filename)
mp3.volume(25)

while True:
    api_json = requests.get("http://api.stackoverflow.com/1.1/questions/unanswered?order=desc&tagged=python").json()
    new_questions = []
    all_questions = []
    for q in api_json["questions"]:
        all_questions.append(q["question_id"])
        if q["question_id"] not in curr_ids:
            new_questions.append(q["question_id"])
    if new_questions:
        print(new_questions)
        mp3.play()
    curr_ids = all_questions
    time.sleep(30)
    # make curr_ids a dictionary for easier lookup
    curr_ids = []

    filename = "path_to_soundfile"
    mp3 = mp3play.load(filename)
    mp3.volume(25)

    # Loop
    while True:
        # Get the list of entries in objects
        entries = get_list_of_entries()

        new_ids = []

        for entry in entries:
            # Check if we reached the most recent entry
            if entry.id in curr_ids:
                # Force loop end if we did
                break

            new_ids.append(entry.id)

            # Do whatever operations
            print entry.title

        if len(new_ids) > 0:
           mp3.play()
           curr_ids = new_ids
        else:
           # No updates in the meantime
           pass

        sleep(30)

使用此处的软件包是因为urllib给我带来了一些编码问题。

IMHO,根据您想要的方法,您可以有两种解决方案:

使用JSON-这将为您提供一个包含所有条目的良好dict。 使用rssxml。在这种情况下,您需要处理XML之类的内容。 无论哪种方式,代码都应该类似于:

import requests
import mp3play
import time

curr_ids = []
filename = "path_to_soundfile"
mp3 = mp3play.load(filename)
mp3.volume(25)

while True:
    api_json = requests.get("http://api.stackoverflow.com/1.1/questions/unanswered?order=desc&tagged=python").json()
    new_questions = []
    all_questions = []
    for q in api_json["questions"]:
        all_questions.append(q["question_id"])
        if q["question_id"] not in curr_ids:
            new_questions.append(q["question_id"])
    if new_questions:
        print(new_questions)
        mp3.play()
    curr_ids = all_questions
    time.sleep(30)
    # make curr_ids a dictionary for easier lookup
    curr_ids = []

    filename = "path_to_soundfile"
    mp3 = mp3play.load(filename)
    mp3.volume(25)

    # Loop
    while True:
        # Get the list of entries in objects
        entries = get_list_of_entries()

        new_ids = []

        for entry in entries:
            # Check if we reached the most recent entry
            if entry.id in curr_ids:
                # Force loop end if we did
                break

            new_ids.append(entry.id)

            # Do whatever operations
            print entry.title

        if len(new_ids) > 0:
           mp3.play()
           curr_ids = new_ids
        else:
           # No updates in the meantime
           pass

        sleep(30)
几点注意:

我会按最早的条目排序,这样打印的条目看起来就像一个流,最近的条目是最后打印出来的。 新的任务是将ID列表保持在最小值。否则,查找将随着时间的推移而变慢 get_list_of_entries是一个容器,用于从XML的源对象或JSON的dict中获取条目。根据您想要的方法,引用它们是不同的,但原则是相同的
请注意,从外观上看,您没有睡眠功能。我想说,你是在锤炼,我不完全相信它是适当的,考虑到那里有一个Lee30。还有,API:@ ToRoad,我不太清楚如何使用API。这些东西是我标记在url末尾的吗?我必须使用json吗?是的,你需要使用json,因为。。现在几乎所有API都是这样工作的。这很简单。import json和json.loadsstring:新的API要求您注册一个应用程序,该应用程序为您提供了一个密钥和密码。接下来,您只需调用一个带有一组参数和标题的URL,这里更是如此:还有用于此IMHO的Python库,您应该在每次读取后进行睡眠,这样您就不会重击服务器并更改逻辑,而不是读取第一个条目,要读取所有条目,直到到达数据库中最新的条目,请执行一种差异。这将返回一个问题id列表,如:[21442125]是,如果您将新的_问题替换为新的_问题。appendq[question_id]。appendq[title]您拥有标题。在浏览器中打开SO api url以查看此处提供的内容。