rss-to-activitypub icon indicating copy to clipboard operation
rss-to-activitypub copied to clipboard

Refresh header support

Open SoniEx2 opened this issue 4 years ago • 0 comments

:joy:

I feel like the Refresh header would fit in great with RSS feeds, and other systems where things should be refreshed every now and then.

I added this to my software recently:

class RemoteDataSource(ObjectDataSource):
    def __init__(self, uri):
        super().__init__({})
        self.uri = uri
        self.remote_exists = False
        self.next_update = 0

    def update(self):
        if self.next_update > time.time():
            return
        # I long for the day when toml has a registered media type
        response = requests.get(self.uri, headers={'user-agent': 'ganarchy/0.0.0', 'accept': '*/*'})
        self.remote_exists = response.status_code == 200
        seconds = 3600
        if (refresh := response.headers.get('Refresh', None)) is not None:
            try:
                seconds = int(refresh)
            except ValueError:
                refresh = refresh.split(';', 1)
                try:
                    seconds = int(refresh[0])
                except ValueError:
                    pass
        self.next_update = time.time() + seconds
        if self.remote_exists:
            response.encoding = 'utf-8'
            try:
                self._obj = qtoml.loads(response.text)
            except (UnicodeDecodeError, qtoml.decoder.TOMLDecodeError) as e:
                self._obj = {}
                return e
        else:
            return response

    def exists(self):
        return self.remote_exists

    def __repr__(self):
        return "RemoteDataSource({!r})".format(self.uri)

maybe you too can make use of the Refresh header?

SoniEx2 avatar May 01 '20 02:05 SoniEx2