5

プログラムが最後に実行されてからのrss更新を取得して表示するPythonプログラムを作成しようとしています。私はfeedparserを使用していて、etagsを使用しようとしていますが、 SOで説明されているように最後に変更されましたが、テストスクリプトが機能していないようです。

import feedparser
rsslist=["http://skottieyoung.tumblr.com/rss","http://mrjakeparker.com/feed/"]
for feed in rsslist:
print('--------'+feed+'-------')
d=feedparser.parse(feed)
print(len(d.entries))
if (len(d.entries) > 0):
    etag=d.feed.get('etag','')
    modified=d.get('modified',d.get('updated',d.entries[0].get('published','no modified,update or published fields present in rss')))

    d2=feedparser.parse(feed,modified)
    if (len(d2.entries) > 0):
        etag2=d2.feed.get('etag','')
        modified2=d2.get('updated',d.entries[0].get('published',''))

    if (d2==d): #ideally we would never see this bc etags/last modified would prevent unnecessarily downloading what we all ready have.
        print("Arrg these are the same")

rss / xmlテクノロジが、オンラインで使用していた参照から変更されたかどうか、またはコードに問題があるかどうかは正直わかりません。

とにかく、私はrssフィードを効率的に使用するための最良の解決策を探しています。現状では、最終変更フィールドやetagsフィールドを使用することで意図されているような帯域幅の浪費を最小限に抑えることを目指しています。

前もって感謝します。

4

2 に答える 2

7

あなたの問題は、の代わりに最終更新日を渡していることですetag。はメソッドetagの 2 番目の引数で、3 番目の引数です。parse()modified

それ以外の:

d2=feedparser.parse(feed,modified)

行う:

d2=feedparser.parse(feed,modified=modified)

ソース コードを確認した後、何も変更されていない場合にサーバーが空の応答を返すことができるように、適切なヘッダーをサーバーに送信することだけが関数に渡されるように見えetagます。サーバーがこれをサポートしていない場合、サーバーは完全な RSS フィードを返します。各エントリの日付を確認し、前のリクエストの最大日付よりも小さい日付のエントリを無視するようにコードを変更します。modifiedparse()

import feedparser
rsslist=["http://skottieyoung.tumblr.com/rss", "http://mrjakeparker.com/feed/"]

def feed_modified_date(feed):
    # this is the last-modified value in the response header
    # do not confuse this with the time that is in each feed as the server
    # may be using a different timezone for last-resposne headers than it 
    # uses for the publish date

    modified = feed.get('modified')
    if modified is not None:
        return modified

    return None

def max_entry_date(feed):
    entry_pub_dates = (e.get('published_parsed') for e in feed.entries)
    entry_pub_dates = tuple(e for e in entry_pub_dates if e is not None)

    if len(entry_pub_dates) > 0:
        return max(entry_pub_dates)    

    return None

def entries_with_dates_after(feed, date):
    response = []

    for entry in feed.entries:
        if entry.get('published_parsed') > date:
            response.append(entry)

    return response            

for feed_url in rsslist:
    print('--------%s-------' % feed_url)
    d = feedparser.parse(feed_url)
    print('feed length %i' % len(d.entries))

    if len(d.entries) > 0:
        etag = d.feed.get('etag', None)
        modified = feed_modified_date(d)
        print('modified at %s' % modified)

        d2 = feedparser.parse(feed_url, etag=etag, modified=modified)
        print('second feed length %i' % len(d2.entries))
        if len(d2.entries) > 0:
            print("server does not support etags or there are new entries")
            # perhaps the server does not support etags or last-modified
            # filter entries ourself

            prev_max_date = max_entry_date(d)

            entries = entries_with_dates_after(d2, prev_max_date)

            print('%i new entries' % len(entries))
        else:
            print('there are no entries')

これにより、次が生成されます。

--------http://skottieyoung.tumblr.com/rss-------
feed length 20
modified at None
second feed length 20
server does not support etags or there are new entries
0 new entries
--------http://mrjakeparker.com/feed/-------
feed length 10
modified at Wed, 07 Nov 2012 19:27:48 GMT
second feed length 0
there are no entries
于 2012-11-08T21:28:56.497 に答える