From 5c7503cb75b8f629295220d586eb6a0896de621f Mon Sep 17 00:00:00 2001 From: Martin Herndl Date: Sun, 22 Aug 2021 16:42:31 +0200 Subject: [PATCH] Update feeds synchronously This seems to be fixing #10 --- nina_xmpp/__init__.py | 49 +++++++++++++++++++++---------------------- 1 file changed, 24 insertions(+), 25 deletions(-) diff --git a/nina_xmpp/__init__.py b/nina_xmpp/__init__.py index 1196025..043b19d 100644 --- a/nina_xmpp/__init__.py +++ b/nina_xmpp/__init__.py @@ -96,37 +96,36 @@ def message_received(self, msg): async def update_feeds_task(self): self.logger.debug('Started update feeds task') while True: - await self.update_feeds() + self.update_feeds() self.logger.debug( 'Finished updating feeds, sleeping for ' f'{self.config["check_interval"]}s' ) await asyncio.sleep(self.config['check_interval']) - async def update_feeds(self): - async with httpx.AsyncClient(trust_env=False) as http_client: - for url in self.config['feeds']: - headers = {} - try: - feed = self.db.query(Feed).filter_by(url=url).one() - except NoResultFound: - self.logger.info(f'Updating feed {url} for the first time') - feed = Feed(url=url) - else: - self.logger.info( - f'Updating feed {url} (last modified: {feed.last_modified})' - ) - if feed.last_modified: - headers['If-Modified-Since'] = feed.last_modified - if feed.etag: - headers['If-None-Match'] = feed.etag - response = await http_client.get(url, headers=headers) - - if response.status_code == httpx.codes.OK: - feed.last_modified = response.headers.get('Last-Modified') - feed.etag = response.headers.get('ETag') - self.db.add(feed) - self.send_updates_for_feed(response.json()) + def update_feeds(self): + for url in self.config['feeds']: + headers = {} + try: + feed = self.db.query(Feed).filter_by(url=url).one() + except NoResultFound: + self.logger.info(f'Updating feed {url} for the first time') + feed = Feed(url=url) + else: + self.logger.info( + f'Updating feed {url} (last modified: {feed.last_modified})' + ) + if feed.last_modified: + headers['If-Modified-Since'] = feed.last_modified + if feed.etag: + headers['If-None-Match'] = feed.etag + response = httpx.get(url, headers=headers) + + if response.status_code == httpx.codes.OK: + feed.last_modified = response.headers.get('Last-Modified') + feed.etag = response.headers.get('ETag') + self.db.add(feed) + self.send_updates_for_feed(response.json()) self.db.commit()