pax_global_header00006660000000000000000000000064145563777720014541gustar00rootroot0000000000000052 comment=a61a96ffd4d4646ed328f509b1a4e5f307a0bb8b exxamalte-python-georss-client-a61a96f/000077500000000000000000000000001455637777200202435ustar00rootroot00000000000000exxamalte-python-georss-client-a61a96f/.coveragerc000066400000000000000000000000351455637777200223620ustar00rootroot00000000000000[run] source = georss_client exxamalte-python-georss-client-a61a96f/.github/000077500000000000000000000000001455637777200216035ustar00rootroot00000000000000exxamalte-python-georss-client-a61a96f/.github/workflows/000077500000000000000000000000001455637777200236405ustar00rootroot00000000000000exxamalte-python-georss-client-a61a96f/.github/workflows/ci.yaml000066400000000000000000000026051455637777200251220ustar00rootroot00000000000000name: CI on: push: branches: - master pull_request: ~ workflow_dispatch: jobs: test: name: Python ${{ matrix.python-version }} runs-on: ubuntu-latest env: USING_COVERAGE: '3.10' strategy: fail-fast: true matrix: python-version: [ "3.8", "3.9", "3.10", "3.11", "3.12" ] steps: - uses: "actions/checkout@v2" - uses: "actions/setup-python@v2" with: python-version: "${{ matrix.python-version }}" - name: "Install dependencies" run: | set -xe python -VV python -m site python -m pip install --upgrade pip setuptools wheel python -m pip install -r requirements.txt python -m pip install -r requirements-test.txt - name: "Run tests for ${{ matrix.python-version }}" run: | pip install pytest-cov pip install coverage pytest \ -qq \ --timeout=9 \ --durations=10 \ -n auto \ --cov georss_client \ --cov-report xml \ -o console_output_style=count \ -p no:sugar \ tests python -m coverage xml - name: "Upload coverage to Codecov" if: "contains(env.USING_COVERAGE, matrix.python-version)" uses: "codecov/codecov-action@v1" with: fail_ci_if_error: true exxamalte-python-georss-client-a61a96f/.gitignore000066400000000000000000000023011455637777200222270ustar00rootroot00000000000000# Byte-compiled / optimized / DLL files __pycache__/ *.py[cod] *$py.class # C extensions *.so # Distribution / packaging .Python build/ develop-eggs/ dist/ downloads/ eggs/ .eggs/ lib/ lib64/ parts/ sdist/ var/ wheels/ *.egg-info/ .installed.cfg *.egg MANIFEST # PyInstaller # Usually these files are written by a python script from a template # before PyInstaller builds the exe, so as to inject date/other infos into it. *.manifest *.spec # Installer logs pip-log.txt pip-delete-this-directory.txt # Unit test / coverage reports htmlcov/ .tox/ .coverage .coverage.* .cache nosetests.xml coverage.xml *.cover .hypothesis/ .pytest_cache/ # Translations *.mo *.pot # Django stuff: *.log local_settings.py db.sqlite3 # Flask stuff: instance/ .webassets-cache # Scrapy stuff: .scrapy # Sphinx documentation docs/_build/ # PyBuilder target/ # Jupyter Notebook .ipynb_checkpoints # pyenv .python-version # celery beat schedule file celerybeat-schedule # SageMath parsed files *.sage.py # Environments .env .venv env/ venv/ ENV/ env.bak/ venv.bak/ # Spyder project settings .spyderproject .spyproject # Rope project settings .ropeproject # mkdocs documentation /site # mypy .mypy_cache/ # IDE .idea/ exxamalte-python-georss-client-a61a96f/.pre-commit-config.yaml000066400000000000000000000006571455637777200245340ustar00rootroot00000000000000repos: - repo: https://github.com/astral-sh/ruff-pre-commit rev: v0.1.11 hooks: - id: ruff args: - --fix - repo: https://github.com/psf/black rev: 23.12.1 hooks: - id: black language_version: python3 - repo: https://github.com/pycqa/flake8 rev: 7.0.0 hooks: - id: flake8 - repo: https://github.com/PyCQA/isort rev: 5.13.2 hooks: - id: isort exxamalte-python-georss-client-a61a96f/CHANGELOG.md000066400000000000000000000061201455637777200220530ustar00rootroot00000000000000# Changes ## 0.16 (31/01/2024) * Removed Python 3.7 support. * Added Python 3.11 support. * Added Python 3.12 support. * Bumped requests to 2.31.0. * Bumped dateparser to 1.2.0. * Bumped haversine to 2.8.1. * Bumped xmltodict to 0.13.0. * Bumped library versions: black, flake8, isort. * Migrated to pytest. * Code quality improvements. ## 0.15 (16/02/2022) * No functional changes. * Migrated to github actions. * Added Python 3.10 support. * Removed Python 3.6 support. * Bumped library versions: black, flake8, isort. ## 0.14 (08/06/2021) * Add license tag (thanks @fabaff). * General code improvements. ## 0.13 (20/04/2021) * Python 3.9 support. ## 0.12 (30/12/2020) * Add non-standard namespace used by [EMSC feed](https://www.emsc-csem.org/service/rss/rss.php). ## 0.11 (18/10/2020) * Excluded tests from package (thanks @scop). * Python 3.8 support. ## 0.10 (05/12/2019) * Fix handling feeds starting with byte order mark. ## 0.9 (01/04/2019) * Migrated Instituto Geográfico Nacional Sismología feed integration to [python-georss-ign-sismologia-client](https://github.com/exxamalte/python-georss-ign-sismologia-client) * Migrated generic GeoRSS feed integration to [python-georss-generic-client](https://github.com/exxamalte/python-georss-generic-client) * Migrated Western Australia Department of Fire and Emergency Services feed integration to [python-georss-wa-dfes-client](https://github.com/exxamalte/python-georss-wa-dfes-client) * Migrated Queensland Fire and Emergency Services (QFES) Bushfire Alert feed integration to [python-georss-qfes-bushfire-alert-client](https://github.com/exxamalte/python-georss-qfes-bushfire-alert-client) * Migrated Tasmania Fire Service Incidents feed to [python-georss-tfs-incidents-client](https://github.com/exxamalte/python-georss-tfs-incidents-client). * Migrated INGV Centro Nazionale Terremoti (Earthquakes) feed to [python-georss-ingv-centro-nazionale-terremoti-client](https://github.com/exxamalte/python-georss-ingv-centro-nazionale-terremoti-client) * Migrated Natural Resources Canada Earthquakes feed [python-georss-nrcan-earthquakes-client](https://github.com/exxamalte/python-georss-nrcan-earthquakes-client) * Dropped Python 3.5 support. ## 0.8 (24/03/2019) * Fixed issue where the feed entries do not have any suitable timestamps. ## 0.7 (24/03/2019) * Simple Feed Manager for all feeds added. ## 0.6 (20/03/2019) * Support for Instituto Geográfico Nacional Sismología (Earthquakes) feed. ## 0.5 (14/12/2018) * Built-in XML parser. * Python 3.7 support. ## 0.4 (01/11/2018) * Third-party library updates. ## 0.3 (08/10/2018) * Filter out entries without any geo location data. * Support for Natural Resources Canada Earthquakes feed. * Support for INGV Centro Nazionale Terremoti (Earthquakes) feed. ## 0.2 (05/10/2018) * Support for Tasmania Fire Service Incidents feed. * Support for Western Australia Department of Fire and Emergency Services feed. ## 0.1 (27/09/2018) * Initial release with support for generic GeoRSS feeds and the QFES Bushfire Alert feed. * Calculating distance to home coordinates. * Support for filtering by distance and category for all feeds. exxamalte-python-georss-client-a61a96f/LICENSE000066400000000000000000000261351455637777200212570ustar00rootroot00000000000000 Apache License Version 2.0, January 2004 http://www.apache.org/licenses/ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 1. Definitions. "License" shall mean the terms and conditions for use, reproduction, and distribution as defined by Sections 1 through 9 of this document. "Licensor" shall mean the copyright owner or entity authorized by the copyright owner that is granting the License. "Legal Entity" shall mean the union of the acting entity and all other entities that control, are controlled by, or are under common control with that entity. For the purposes of this definition, "control" means (i) the power, direct or indirect, to cause the direction or management of such entity, whether by contract or otherwise, or (ii) ownership of fifty percent (50%) or more of the outstanding shares, or (iii) beneficial ownership of such entity. "You" (or "Your") shall mean an individual or Legal Entity exercising permissions granted by this License. "Source" form shall mean the preferred form for making modifications, including but not limited to software source code, documentation source, and configuration files. "Object" form shall mean any form resulting from mechanical transformation or translation of a Source form, including but not limited to compiled object code, generated documentation, and conversions to other media types. "Work" shall mean the work of authorship, whether in Source or Object form, made available under the License, as indicated by a copyright notice that is included in or attached to the work (an example is provided in the Appendix below). "Derivative Works" shall mean any work, whether in Source or Object form, that is based on (or derived from) the Work and for which the editorial revisions, annotations, elaborations, or other modifications represent, as a whole, an original work of authorship. For the purposes of this License, Derivative Works shall not include works that remain separable from, or merely link (or bind by name) to the interfaces of, the Work and Derivative Works thereof. "Contribution" shall mean any work of authorship, including the original version of the Work and any modifications or additions to that Work or Derivative Works thereof, that is intentionally submitted to Licensor for inclusion in the Work by the copyright owner or by an individual or Legal Entity authorized to submit on behalf of the copyright owner. For the purposes of this definition, "submitted" means any form of electronic, verbal, or written communication sent to the Licensor or its representatives, including but not limited to communication on electronic mailing lists, source code control systems, and issue tracking systems that are managed by, or on behalf of, the Licensor for the purpose of discussing and improving the Work, but excluding communication that is conspicuously marked or otherwise designated in writing by the copyright owner as "Not a Contribution." "Contributor" shall mean Licensor and any individual or Legal Entity on behalf of whom a Contribution has been received by Licensor and subsequently incorporated within the Work. 2. Grant of Copyright License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable copyright license to reproduce, prepare Derivative Works of, publicly display, publicly perform, sublicense, and distribute the Work and such Derivative Works in Source or Object form. 3. Grant of Patent License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable (except as stated in this section) patent license to make, have made, use, offer to sell, sell, import, and otherwise transfer the Work, where such license applies only to those patent claims licensable by such Contributor that are necessarily infringed by their Contribution(s) alone or by combination of their Contribution(s) with the Work to which such Contribution(s) was submitted. If You institute patent litigation against any entity (including a cross-claim or counterclaim in a lawsuit) alleging that the Work or a Contribution incorporated within the Work constitutes direct or contributory patent infringement, then any patent licenses granted to You under this License for that Work shall terminate as of the date such litigation is filed. 4. Redistribution. You may reproduce and distribute copies of the Work or Derivative Works thereof in any medium, with or without modifications, and in Source or Object form, provided that You meet the following conditions: (a) You must give any other recipients of the Work or Derivative Works a copy of this License; and (b) You must cause any modified files to carry prominent notices stating that You changed the files; and (c) You must retain, in the Source form of any Derivative Works that You distribute, all copyright, patent, trademark, and attribution notices from the Source form of the Work, excluding those notices that do not pertain to any part of the Derivative Works; and (d) If the Work includes a "NOTICE" text file as part of its distribution, then any Derivative Works that You distribute must include a readable copy of the attribution notices contained within such NOTICE file, excluding those notices that do not pertain to any part of the Derivative Works, in at least one of the following places: within a NOTICE text file distributed as part of the Derivative Works; within the Source form or documentation, if provided along with the Derivative Works; or, within a display generated by the Derivative Works, if and wherever such third-party notices normally appear. The contents of the NOTICE file are for informational purposes only and do not modify the License. You may add Your own attribution notices within Derivative Works that You distribute, alongside or as an addendum to the NOTICE text from the Work, provided that such additional attribution notices cannot be construed as modifying the License. You may add Your own copyright statement to Your modifications and may provide additional or different license terms and conditions for use, reproduction, or distribution of Your modifications, or for any such Derivative Works as a whole, provided Your use, reproduction, and distribution of the Work otherwise complies with the conditions stated in this License. 5. Submission of Contributions. Unless You explicitly state otherwise, any Contribution intentionally submitted for inclusion in the Work by You to the Licensor shall be under the terms and conditions of this License, without any additional terms or conditions. Notwithstanding the above, nothing herein shall supersede or modify the terms of any separate license agreement you may have executed with Licensor regarding such Contributions. 6. Trademarks. This License does not grant permission to use the trade names, trademarks, service marks, or product names of the Licensor, except as required for reasonable and customary use in describing the origin of the Work and reproducing the content of the NOTICE file. 7. Disclaimer of Warranty. Unless required by applicable law or agreed to in writing, Licensor provides the Work (and each Contributor provides its Contributions) on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, including, without limitation, any warranties or conditions of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE. You are solely responsible for determining the appropriateness of using or redistributing the Work and assume any risks associated with Your exercise of permissions under this License. 8. Limitation of Liability. In no event and under no legal theory, whether in tort (including negligence), contract, or otherwise, unless required by applicable law (such as deliberate and grossly negligent acts) or agreed to in writing, shall any Contributor be liable to You for damages, including any direct, indirect, special, incidental, or consequential damages of any character arising as a result of this License or out of the use or inability to use the Work (including but not limited to damages for loss of goodwill, work stoppage, computer failure or malfunction, or any and all other commercial damages or losses), even if such Contributor has been advised of the possibility of such damages. 9. Accepting Warranty or Additional Liability. While redistributing the Work or Derivative Works thereof, You may choose to offer, and charge a fee for, acceptance of support, warranty, indemnity, or other liability obligations and/or rights consistent with this License. However, in accepting such obligations, You may act only on Your own behalf and on Your sole responsibility, not on behalf of any other Contributor, and only if You agree to indemnify, defend, and hold each Contributor harmless for any liability incurred by, or claims asserted against, such Contributor by reason of your accepting any such warranty or additional liability. END OF TERMS AND CONDITIONS APPENDIX: How to apply the Apache License to your work. To apply the Apache License to your work, attach the following boilerplate notice, with the fields enclosed by brackets "[]" replaced with your own identifying information. (Don't include the brackets!) The text should be enclosed in the appropriate comment syntax for the file format. We also recommend that a file or class name and description of purpose be included on the same "printed page" as the copyright notice for easier identification within third-party archives. Copyright [yyyy] [name of copyright owner] Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. exxamalte-python-georss-client-a61a96f/README.md000066400000000000000000000107661455637777200215340ustar00rootroot00000000000000# python-georss-client [![Build Status](https://github.com/exxamalte/python-georss-client/actions/workflows/ci.yaml/badge.svg?branch=master)](https://github.com/exxamalte/python-georss-client/actions?workflow=ci) [![codecov](https://codecov.io/gh/exxamalte/python-georss-client/branch/master/graph/badge.svg?token=8L93XZYRJK)](https://codecov.io/gh/exxamalte/python-georss-client) [![PyPi](https://img.shields.io/pypi/v/georss-client.svg)](https://pypi.python.org/pypi/georss-client) [![Version](https://img.shields.io/pypi/pyversions/georss-client.svg)](https://pypi.python.org/pypi/georss-client) [![Maintainability](https://api.codeclimate.com/v1/badges/ed2a70f3af0c2324dcce/maintainability)](https://codeclimate.com/github/exxamalte/python-georss-client/maintainability) This library is a framework to build concrete libraries for convenient access to [GeoRSS](http://www.georss.org/) Feeds. ## Installation `pip install georss-client` ## Known Implementations | Library | Source | Topic | |---------|--------|-------| | [python-georss-generic-client](https://github.com/exxamalte/python-georss-generic-client) | Generic GeoRSS Feeds | misc | | [python-georss-ign-sismologia-client](https://github.com/exxamalte/python-georss-ign-sismologia-client) | Instituto Geográfico Nacional Sismología | Earthquakes | | [python-georss-ingv-centro-nazionale-terremoti-client](https://github.com/exxamalte/python-georss-ingv-centro-nazionale-terremoti-client) | INGV Centro Nazionale Terremoti | Earthquakes | | [python-georss-nrcan-earthquakes-client](https://github.com/exxamalte/python-georss-nrcan-earthquakes-client) | Natural Resources Canada | Earthquakes | | [python-georss-qld-bushfire-alert-client](https://github.com/exxamalte/python-georss-qld-bushfire-alert-client) | Queensland Bushfire Alert | Fires | | [python-georss-tfs-incidents-client](https://github.com/exxamalte/python-georss-tfs-incidents-client) | Tasmania Fire Service Incidents | Fires | | [python-georss-wa-dfes-client](https://github.com/exxamalte/python-georss-wa-dfes-client) | Western Australia Department of Fire and Emergency Services | Fires | ## Usage Each implementation extracts relevant information from the GeoRSS feed. Not all feeds contain the same level of information, or present their information in different ways. After instantiating a particular class and supply the required parameters, you can call `update` to retrieve the feed data. The return value will be a tuple of a status code and the actual data in the form of a list of feed entries specific to the selected feed. Status Codes * _UPDATE_OK_: Update went fine and data was retrieved. The library may still return empty data, for example because no entries fulfilled the filter criteria. * _UPDATE_OK_NO_DATA_: Update went fine but no data was retrieved, for example because the server indicated that there was not update since the last request. * _UPDATE_ERROR_: Something went wrong during the update ## Feed Managers The Feed Managers help managing feed updates over time, by notifying the consumer of the feed about new feed entries, updates and removed entries compared to the last feed update. * If the current feed update is the first one, then all feed entries will be reported as new. The feed manager will keep track of all feed entries' external IDs that it has successfully processed. * If the current feed update is not the first one, then the feed manager will produce three sets: * Feed entries that were not in the previous feed update but are in the current feed update will be reported as new. * Feed entries that were in the previous feed update and are still in the current feed update will be reported as to be updated. * Feed entries that were in the previous feed update but are not in the current feed update will be reported to be removed. * If the current update fails, then all feed entries processed in the previous feed update will be reported to be removed. After a successful update from the feed, the feed manager will provide two different dates: * `last_update` will be the timestamp of the last successful update from the feed. This date may be useful if the consumer of this library wants to treat intermittent errors from feed updates differently. * `last_timestamp` will be the latest timestamp extracted from the feed data. This requires that the underlying feed data actually contains a suitable date. This date may be useful if the consumer of this library wants to process feed entries differently if they haven't actually been updated. exxamalte-python-georss-client-a61a96f/codecov.yml000066400000000000000000000000171455637777200224060ustar00rootroot00000000000000comment: false exxamalte-python-georss-client-a61a96f/georss_client/000077500000000000000000000000001455637777200231035ustar00rootroot00000000000000exxamalte-python-georss-client-a61a96f/georss_client/__init__.py000066400000000000000000000003021455637777200252070ustar00rootroot00000000000000"""Base class for GeoRSS services.""" from georss_client.consts import ATTR_ATTRIBUTION # noqa: F401 from .feed import GeoRssFeed # noqa: F401 from .feed_entry import FeedEntry # noqa: F401 exxamalte-python-georss-client-a61a96f/georss_client/__version__.py000066400000000000000000000000661455637777200257400ustar00rootroot00000000000000"""Define a version constant.""" __version__ = "0.16" exxamalte-python-georss-client-a61a96f/georss_client/consts.py000066400000000000000000000031641455637777200247720ustar00rootroot00000000000000"""Constants. Constants for feeds and feed entries. """ from __future__ import annotations ATTR_ATTRIBUTION = "attribution" CUSTOM_ATTRIBUTE = "custom_attribute" XML_ATTR_HREF = "@href" XML_ATTR_TERM = "@term" XML_CDATA = "#text" XML_TAG_AUTHOR = "author" XML_TAG_CATEGORY = "category" XML_TAG_CHANNEL = "channel" XML_TAG_CONTENT = "content" XML_TAG_CONTRIBUTOR = "contributor" XML_TAG_COPYRIGHT = "copyright" XML_TAG_DC_DATE = "dc:date" XML_TAG_DESCRIPTION = "description" XML_TAG_DOCS = "docs" XML_TAG_ENTRY = "entry" XML_TAG_FEED = "feed" XML_TAG_GENERATOR = "generator" XML_TAG_GEO_LAT = "geo:lat" XML_TAG_GEO_LONG = "geo:long" XML_TAG_GEO_POINT = "geo:Point" XML_TAG_GEORSS_POINT = "georss:point" XML_TAG_GEORSS_POLYGON = "georss:polygon" XML_TAG_GEORSS_WHERE = "georss:where" XML_TAG_GML_EXTERIOR = "gml:exterior" XML_TAG_GML_LINEAR_RING = "gml:LinearRing" XML_TAG_GML_POINT = "gml:Point" XML_TAG_GML_POLYGON = "gml:Polygon" XML_TAG_GML_POS = "gml:pos" XML_TAG_GML_POS_LIST = "gml:posList" XML_TAG_GUID = "guid" XML_TAG_HEIGHT = "height" XML_TAG_ID = "id" XML_TAG_IMAGE = "image" XML_TAG_ITEM = "item" XML_TAG_LANGUAGE = "language" XML_TAG_LAST_BUILD_DATE = "lastBuildDate" XML_TAG_LINK = "link" XML_TAG_MANAGING_EDITOR = "managingEditor" XML_TAG_NAME = "name" XML_TAG_PUB_DATE = "pubDate" XML_TAG_PUBLISHED = "published" XML_TAG_RIGHTS = "rights" XML_TAG_RSS = "rss" XML_TAG_SOURCE = "source" XML_TAG_SUBTITLE = "subtitle" XML_TAG_SUMMARY = "summary" XML_TAG_TITLE = "title" XML_TAG_TTL = "ttl" XML_TAG_UPDATED = "updated" XML_TAG_URL = "url" XML_TAG_WIDTH = "width" UPDATE_OK = "OK" UPDATE_OK_NO_DATA = "OK_NO_DATA" UPDATE_ERROR = "ERROR" exxamalte-python-georss-client-a61a96f/georss_client/exceptions.py000066400000000000000000000001431455637777200256340ustar00rootroot00000000000000"""Exceptions for this library.""" class GeoRssException(Exception): """GeoRSS Exception.""" exxamalte-python-georss-client-a61a96f/georss_client/feed.py000066400000000000000000000133241455637777200243630ustar00rootroot00000000000000"""GeoRSS Feed.""" from __future__ import annotations import codecs import logging from datetime import datetime import requests from .consts import ATTR_ATTRIBUTION, UPDATE_ERROR, UPDATE_OK, UPDATE_OK_NO_DATA from .xml_parser import XmlParser _LOGGER = logging.getLogger(__name__) class GeoRssFeed: """GeoRSS feed base class.""" def __init__( self, home_coordinates, url, filter_radius=None, filter_categories=None ): """Initialise this service.""" self._home_coordinates = home_coordinates self._filter_radius = filter_radius self._filter_categories = filter_categories self._url = url self._request = requests.Request(method="GET", url=url).prepare() self._last_timestamp = None def __repr__(self): """Return string representation of this feed.""" return "<{}(home={}, url={}, radius={}, categories={})>".format( self.__class__.__name__, self._home_coordinates, self._url, self._filter_radius, self._filter_categories, ) def _new_entry(self, home_coordinates, rss_entry, global_data): """Generate a new entry.""" pass def _additional_namespaces(self): """Provide additional namespaces, relevant for this feed.""" pass def update(self): """Update from external source and return filtered entries.""" status, data = self._fetch() if status == UPDATE_OK: if data: entries = [] global_data = self._extract_from_feed(data) # Extract data from feed entries. for rss_entry in data.entries: entries.append( self._new_entry(self._home_coordinates, rss_entry, global_data) ) filtered_entries = self._filter_entries(entries) self._last_timestamp = self._extract_last_timestamp(filtered_entries) return UPDATE_OK, filtered_entries else: # Should not happen. return UPDATE_OK, None elif status == UPDATE_OK_NO_DATA: # Happens for example if the server returns 304 return UPDATE_OK_NO_DATA, None else: # Error happened while fetching the feed. return UPDATE_ERROR, None def _fetch(self): """Fetch GeoRSS data from external source.""" try: with requests.Session() as session: response = session.send(self._request, timeout=10) if response.ok: self._pre_process_response(response) parser = XmlParser(self._additional_namespaces()) feed_data = parser.parse(response.text) self.parser = parser self.feed_data = feed_data return UPDATE_OK, feed_data else: _LOGGER.warning( "Fetching data from %s failed with status %s", self._request.url, response.status_code, ) return UPDATE_ERROR, None except requests.exceptions.RequestException as request_ex: _LOGGER.warning( "Fetching data from %s failed with %s", self._request.url, request_ex ) return UPDATE_ERROR, None def _pre_process_response(self, response): """Pre-process the response.""" if response: _LOGGER.debug("Response encoding %s", response.encoding) if response.content.startswith(codecs.BOM_UTF8): _LOGGER.debug( "UTF8 byte order mark detected, " "setting encoding to 'utf-8-sig'" ) response.encoding = "utf-8-sig" def _filter_entries(self, entries): """Filter the provided entries.""" filtered_entries = entries _LOGGER.debug("Entries before filtering %s", filtered_entries) # Always remove entries without geometry filtered_entries = list( filter(lambda entry: entry.geometry is not None, filtered_entries) ) # Filter by distance. if self._filter_radius: filtered_entries = list( filter( lambda entry: entry.distance_to_home <= self._filter_radius, filtered_entries, ) ) # Filter by category. if self._filter_categories: filtered_entries = list( filter( lambda entry: len( {entry.category}.intersection(self._filter_categories) ) > 0, filtered_entries, ) ) _LOGGER.debug("Entries after filtering %s", filtered_entries) return filtered_entries def _extract_from_feed(self, feed): """Extract global metadata from feed.""" global_data = {} author = feed.author if author: global_data[ATTR_ATTRIBUTION] = author return global_data def _extract_last_timestamp(self, feed_entries): """Determine latest (newest) entry from the filtered feed.""" if feed_entries: dates = sorted( [entry.published for entry in feed_entries if entry.published], reverse=True, ) if dates: last_timestamp = dates[0] _LOGGER.debug("Last timestamp: %s", last_timestamp) return last_timestamp return None @property def last_timestamp(self) -> datetime | None: """Return the last timestamp extracted from this feed.""" return self._last_timestamp exxamalte-python-georss-client-a61a96f/georss_client/feed_entry.py000066400000000000000000000074551455637777200256140ustar00rootroot00000000000000"""Feed Entry.""" from __future__ import annotations import re from datetime import datetime from .consts import CUSTOM_ATTRIBUTE from .geo_rss_distance_helper import GeoRssDistanceHelper class FeedEntry: """Feed entry base class.""" def __init__(self, home_coordinates, rss_entry): """Initialise this feed entry.""" self._home_coordinates = home_coordinates self._rss_entry = rss_entry def __repr__(self): """Return string representation of this entry.""" return f"<{self.__class__.__name__}(id={self.external_id})>" @property def geometry(self): """Return all geometry details of this entry.""" if self._rss_entry: return self._rss_entry.geometry return None @property def coordinates(self): """Return the best coordinates (latitude, longitude) of this entry.""" if self.geometry: return GeoRssDistanceHelper.extract_coordinates(self.geometry) return None @property def external_id(self) -> str | None: """Return the external id of this entry.""" if self._rss_entry: external_id = self._rss_entry.guid if not external_id: external_id = self.title if not external_id: # Use geometry as ID as a fallback. external_id = hash(self.coordinates) return external_id return None def _search_in_external_id(self, regexp): """Find a sub-string in the entry's external id.""" if self.external_id: match = re.search(regexp, self.external_id) if match: return match.group(CUSTOM_ATTRIBUTE) return None @property def title(self) -> str | None: """Return the title of this entry.""" if self._rss_entry: return self._rss_entry.title return None def _search_in_title(self, regexp): """Find a sub-string in the entry's title.""" if self.title: match = re.search(regexp, self.title) if match: return match.group(CUSTOM_ATTRIBUTE) return None @property def category(self) -> str | None: """Return the category of this entry.""" if ( self._rss_entry and self._rss_entry.category and isinstance(self._rss_entry.category, list) ): # To keep this simple, just return the first category. return self._rss_entry.category[0] return None @property def attribution(self) -> str | None: """Return the attribution of this entry.""" return None @property def distance_to_home(self): """Return the distance in km of this entry to the home coordinates.""" return GeoRssDistanceHelper.distance_to_geometry( self._home_coordinates, self.geometry ) @property def description(self) -> str | None: """Return the description of this entry.""" if self._rss_entry and self._rss_entry.description: return self._rss_entry.description return None @property def published(self) -> datetime | None: """Return the published date of this entry.""" if self._rss_entry: return self._rss_entry.published_date return None @property def updated(self) -> datetime | None: """Return the updated date of this entry.""" if self._rss_entry: return self._rss_entry.updated_date return None def _search_in_description(self, regexp): """Find a sub-string in the entry's description.""" if self.description: match = re.search(regexp, self.description) if match: return match.group(CUSTOM_ATTRIBUTE) return None exxamalte-python-georss-client-a61a96f/georss_client/feed_manager.py000066400000000000000000000072261455637777200260610ustar00rootroot00000000000000"""Base class for the feed manager. This allows managing feeds and their entries throughout their life-cycle. """ from __future__ import annotations import logging from datetime import datetime from .consts import UPDATE_OK, UPDATE_OK_NO_DATA _LOGGER = logging.getLogger(__name__) class FeedManagerBase: """Generic Feed manager.""" def __init__(self, feed, generate_callback, update_callback, remove_callback): """Initialise feed manager.""" self._feed = feed self.feed_entries = {} self._managed_external_ids = set() self._last_update = None self._generate_callback = generate_callback self._update_callback = update_callback self._remove_callback = remove_callback def __repr__(self): """Return string representation of this feed.""" return f"<{self.__class__.__name__}(feed={self._feed})>" def update(self): """Update the feed and then update connected entities.""" status, feed_entries = self._feed.update() if status == UPDATE_OK: _LOGGER.debug("Data retrieved %s", feed_entries) # Keep a copy of all feed entries for future lookups by entities. self.feed_entries = {entry.external_id: entry for entry in feed_entries} # Record current time of update. self._last_update = datetime.now() # For entity management the external ids from the feed are used. feed_external_ids = set(self.feed_entries) remove_external_ids = self._managed_external_ids.difference( feed_external_ids ) self._remove_entities(remove_external_ids) update_external_ids = self._managed_external_ids.intersection( feed_external_ids ) self._update_entities(update_external_ids) create_external_ids = feed_external_ids.difference( self._managed_external_ids ) self._generate_new_entities(create_external_ids) elif status == UPDATE_OK_NO_DATA: _LOGGER.debug("Update successful, but no data received from %s", self._feed) else: _LOGGER.warning( "Update not successful, no data received from %s", self._feed ) # Remove all entities. self._remove_entities(self._managed_external_ids.copy()) # Remove all feed entries and managed external ids. self.feed_entries.clear() self._managed_external_ids.clear() def _generate_new_entities(self, external_ids): """Generate new entities for events.""" for external_id in external_ids: self._generate_callback(external_id) _LOGGER.debug("New entity added %s", external_id) self._managed_external_ids.add(external_id) def _update_entities(self, external_ids): """Update entities.""" for external_id in external_ids: _LOGGER.debug("Existing entity found %s", external_id) self._update_callback(external_id) def _remove_entities(self, external_ids): """Remove entities.""" for external_id in external_ids: _LOGGER.debug("Entity not current anymore %s", external_id) self._managed_external_ids.remove(external_id) self._remove_callback(external_id) @property def last_timestamp(self) -> datetime | None: """Return the last timestamp extracted from this feed.""" return self._feed.last_timestamp @property def last_update(self) -> datetime | None: """Return the last successful update of this feed.""" return self._last_update exxamalte-python-georss-client-a61a96f/georss_client/geo_rss_distance_helper.py000066400000000000000000000054631455637777200303370ustar00rootroot00000000000000"""GeoRSS Distance Helper.""" import logging from haversine import haversine from georss_client.xml_parser.geometry import Point, Polygon _LOGGER = logging.getLogger(__name__) class GeoRssDistanceHelper: """Helper to calculate distances between GeoRSS geometries.""" @staticmethod def extract_coordinates(geometry): """Extract the best coordinates from the feature for display.""" latitude = longitude = None if isinstance(geometry, Point): # Just extract latitude and longitude directly. latitude, longitude = geometry.latitude, geometry.longitude elif isinstance(geometry, Polygon): centroid = geometry.centroid latitude, longitude = centroid.latitude, centroid.longitude _LOGGER.debug("Centroid of %s is %s", geometry, (latitude, longitude)) else: _LOGGER.debug("Not implemented: %s", type(geometry)) return latitude, longitude @staticmethod def distance_to_geometry(home_coordinates, geometry): """Calculate the distance between home coordinates and geometry.""" distance = float("inf") if isinstance(geometry, Point): distance = GeoRssDistanceHelper._distance_to_point( home_coordinates, geometry ) elif isinstance(geometry, Polygon): distance = GeoRssDistanceHelper._distance_to_polygon( home_coordinates, geometry ) else: _LOGGER.debug("Not implemented: %s", type(geometry)) return distance @staticmethod def _distance_to_point(home_coordinates, point): """Calculate the distance between home coordinates and the point.""" # Swap coordinates to match: (latitude, longitude). return GeoRssDistanceHelper._distance_to_coordinates( home_coordinates, (point.latitude, point.longitude) ) @staticmethod def _distance_to_polygon(home_coordinates, polygon): """Calculate the distance between home coordinates and the polygon.""" distance = float("inf") # Calculate distance from polygon by calculating the distance # to each point of the polygon but not to each edge of the # polygon; should be good enough for point in polygon.points: distance = min( distance, GeoRssDistanceHelper._distance_to_coordinates( home_coordinates, (point.latitude, point.longitude) ), ) return distance @staticmethod def _distance_to_coordinates(home_coordinates, coordinates): """Calculate the distance between home coordinates and the coordinates.""" # Expecting coordinates in format: (latitude, longitude). return haversine(coordinates, home_coordinates) exxamalte-python-georss-client-a61a96f/georss_client/xml_parser/000077500000000000000000000000001455637777200252575ustar00rootroot00000000000000exxamalte-python-georss-client-a61a96f/georss_client/xml_parser/__init__.py000066400000000000000000000061731455637777200273770ustar00rootroot00000000000000"""XML Parser.""" import logging import dateparser import xmltodict from georss_client.consts import ( XML_TAG_CHANNEL, XML_TAG_DC_DATE, XML_TAG_FEED, XML_TAG_GEO_LAT, XML_TAG_GEO_LONG, XML_TAG_GEORSS_POINT, XML_TAG_GEORSS_POLYGON, XML_TAG_GML_POS, XML_TAG_GML_POS_LIST, XML_TAG_HEIGHT, XML_TAG_LAST_BUILD_DATE, XML_TAG_PUB_DATE, XML_TAG_PUBLISHED, XML_TAG_RSS, XML_TAG_TTL, XML_TAG_UPDATED, XML_TAG_WIDTH, ) from georss_client.xml_parser.feed import Feed _LOGGER = logging.getLogger(__name__) DEFAULT_NAMESPACES = { "http://www.w3.org/2005/Atom": None, "http://purl.org/dc/elements/1.1/": "dc", "http://www.georss.org/georss": "georss", "http://www.w3.org/2003/01/geo/wgs84_pos#": "geo", "http://www.w3.org/2003/01/geo/": "geo", "http://www.opengis.net/gml": "gml", "http://www.gdacs.org/": "gdacs", } KEYS_DATE = [ XML_TAG_DC_DATE, XML_TAG_LAST_BUILD_DATE, XML_TAG_PUB_DATE, XML_TAG_PUBLISHED, XML_TAG_UPDATED, ] KEYS_FLOAT = [XML_TAG_GEO_LAT, XML_TAG_GEO_LONG] KEYS_FLOAT_LIST = [ XML_TAG_GEORSS_POLYGON, XML_TAG_GML_POS_LIST, XML_TAG_GML_POS, XML_TAG_GEORSS_POINT, ] KEYS_INT = [XML_TAG_HEIGHT, XML_TAG_TTL, XML_TAG_WIDTH] class XmlParser: """Built-in XML parser.""" def __init__(self, additional_namespaces=None): """Initialise the XML parser.""" self._namespaces = DEFAULT_NAMESPACES if additional_namespaces: self._namespaces.update(additional_namespaces) @staticmethod def postprocessor(path, key, value): """Conduct type conversion for selected keys.""" try: if key in KEYS_DATE and value: return key, dateparser.parse(value) if key in KEYS_FLOAT and value: return key, float(value) if key in KEYS_FLOAT_LIST and value: # Turn white-space separated list of numbers into # list of floats. coordinate_values = value.split() point_coordinates = [] for i in range(0, len(coordinate_values)): point_coordinates.append(float(coordinate_values[i])) return key, point_coordinates if key in KEYS_INT and value: return key, int(value) except (ValueError, TypeError) as error: _LOGGER.warning("Unable to process (%s/%s): %s", key, value, error) return key, value def parse(self, xml): """Parse the provided xml.""" if xml: parsed_dict = xmltodict.parse( xml, process_namespaces=True, namespaces=self._namespaces, postprocessor=XmlParser.postprocessor, ) if XML_TAG_RSS in parsed_dict: rss = parsed_dict.get(XML_TAG_RSS) if XML_TAG_CHANNEL in rss: channel = rss.get(XML_TAG_CHANNEL) return Feed(channel) if XML_TAG_FEED in parsed_dict: feed = parsed_dict.get(XML_TAG_FEED) return Feed(feed) return None exxamalte-python-georss-client-a61a96f/georss_client/xml_parser/feed.py000066400000000000000000000044261455637777200265420ustar00rootroot00000000000000"""GeoRSS feed models.""" from __future__ import annotations import logging from georss_client.consts import ( XML_TAG_COPYRIGHT, XML_TAG_DOCS, XML_TAG_ENTRY, XML_TAG_GENERATOR, XML_TAG_IMAGE, XML_TAG_ITEM, XML_TAG_LANGUAGE, XML_TAG_RIGHTS, XML_TAG_SUBTITLE, XML_TAG_TTL, ) from georss_client.xml_parser.feed_image import FeedImage from georss_client.xml_parser.feed_item import FeedItem from georss_client.xml_parser.feed_or_feed_item import FeedOrFeedItem _LOGGER = logging.getLogger(__name__) class Feed(FeedOrFeedItem): """Represents a feed.""" @property def subtitle(self) -> str | None: """Return the subtitle of this feed.""" return self._attribute_with_text([XML_TAG_SUBTITLE]) @property def copyright(self) -> str | None: """Return the copyright of this feed.""" return self._attribute_with_text([XML_TAG_COPYRIGHT, XML_TAG_RIGHTS]) @property def rights(self) -> str | None: """Return the rights of this feed.""" return self.copyright @property def generator(self) -> str | None: """Return the generator of this feed.""" return self._attribute_with_text([XML_TAG_GENERATOR]) @property def language(self) -> str | None: """Return the language of this feed.""" return self._attribute([XML_TAG_LANGUAGE]) @property def docs(self) -> str | None: """Return the docs URL of this feed.""" return self._attribute_with_text([XML_TAG_DOCS]) @property def ttl(self) -> int | None: """Return the ttl of this feed.""" return self._attribute([XML_TAG_TTL]) @property def image(self): """Return the image of this feed.""" image = self._attribute([XML_TAG_IMAGE]) if image: return FeedImage(image) return None @property def entries(self): """Return the entries of this feed.""" items = self._attribute([XML_TAG_ITEM, XML_TAG_ENTRY]) entries = [] if items and isinstance(items, list): for item in items: entries.append(FeedItem(item)) else: # A single item in the feed is not represented as an array. entries.append(FeedItem(items)) return entries exxamalte-python-georss-client-a61a96f/georss_client/xml_parser/feed_dict_source.py000066400000000000000000000052221455637777200311200ustar00rootroot00000000000000"""GeoRSS feed dict source.""" from __future__ import annotations from georss_client.consts import ( XML_ATTR_HREF, XML_CDATA, XML_TAG_CONTENT, XML_TAG_DESCRIPTION, XML_TAG_LINK, XML_TAG_SUMMARY, XML_TAG_TITLE, ) class FeedDictSource: """Represents a subset of a feed based on a dict.""" def __init__(self, source): """Initialise feed.""" self._source = source def __repr__(self): """Return string representation of this feed item.""" return f"<{self.__class__.__name__}({self.link})>" def _attribute(self, names): """Get an attribute from this feed or feed item.""" if self._source and names: # Try each name, and return the first value that is not None. for name in names: value = self._source.get(name, None) if value: return value return None def _attribute_with_text(self, names): """Get an attribute with text from this feed or feed item.""" value = self._attribute(names) if value and isinstance(value, dict) and XML_CDATA in value: # Value value = value.get(XML_CDATA) return value @staticmethod def _attribute_in_structure(obj, keys): """Return the attribute found under the chain of keys.""" key = keys.pop(0) if key in obj: return ( FeedDictSource._attribute_in_structure(obj[key], keys) if keys else obj[key] ) @property def title(self) -> str | None: """Return the title of this feed or feed item.""" return self._attribute_with_text([XML_TAG_TITLE]) @property def description(self) -> str | None: """Return the description of this feed or feed item.""" return self._attribute_with_text( [XML_TAG_DESCRIPTION, XML_TAG_SUMMARY, XML_TAG_CONTENT] ) @property def summary(self) -> str | None: """Return the summary of this feed or feed item.""" return self.description @property def content(self) -> str | None: """Return the content of this feed or feed item.""" return self.description @property def link(self) -> str | None: """Return the link of this feed or feed item.""" link = self._attribute([XML_TAG_LINK]) if link and XML_ATTR_HREF in link: link = link.get(XML_ATTR_HREF) return link def get_additional_attribute(self, name): """Get an additional attribute not provided as property.""" return self._attribute([name]) exxamalte-python-georss-client-a61a96f/georss_client/xml_parser/feed_image.py000066400000000000000000000013201455637777200276720ustar00rootroot00000000000000"""GeoRSS feed image.""" from __future__ import annotations from georss_client.consts import XML_TAG_HEIGHT, XML_TAG_URL, XML_TAG_WIDTH from georss_client.xml_parser.feed_dict_source import FeedDictSource class FeedImage(FeedDictSource): """Represents a feed image.""" @property def url(self) -> str | None: """Return the url of this feed image.""" return self._attribute([XML_TAG_URL]) @property def height(self) -> int | None: """Return the height of this feed image.""" return self._attribute([XML_TAG_HEIGHT]) @property def width(self) -> int | None: """Return the width of this feed image.""" return self._attribute([XML_TAG_WIDTH]) exxamalte-python-georss-client-a61a96f/georss_client/xml_parser/feed_item.py000066400000000000000000000114551455637777200275600ustar00rootroot00000000000000"""GeoRSS feed item.""" from __future__ import annotations from georss_client.consts import ( XML_TAG_GEO_LAT, XML_TAG_GEO_LONG, XML_TAG_GEO_POINT, XML_TAG_GEORSS_POINT, XML_TAG_GEORSS_POLYGON, XML_TAG_GEORSS_WHERE, XML_TAG_GML_EXTERIOR, XML_TAG_GML_LINEAR_RING, XML_TAG_GML_POINT, XML_TAG_GML_POLYGON, XML_TAG_GML_POS, XML_TAG_GML_POS_LIST, XML_TAG_GUID, XML_TAG_ID, XML_TAG_SOURCE, ) from georss_client.xml_parser.feed_or_feed_item import FeedOrFeedItem from georss_client.xml_parser.geometry import Geometry, Point, Polygon class FeedItem(FeedOrFeedItem): """Represents a feed item.""" def __repr__(self): """Return string representation of this feed item.""" return f"<{self.__class__.__name__}({self.guid})>" @property def guid(self) -> str | None: """Return the guid of this feed item.""" return self._attribute_with_text([XML_TAG_GUID, XML_TAG_ID]) @property def id(self) -> str | None: """Return the id of this feed item.""" return self.guid @property def source(self) -> str | None: """Return the source of this feed item.""" return self._attribute([XML_TAG_SOURCE]) @property def geometry(self) -> Geometry | None: """Return the geometry of this feed item.""" # -0.5 119.8 point = self._attribute([XML_TAG_GEORSS_POINT]) if point: return Point(point[0], point[1]) # GML where = self._attribute([XML_TAG_GEORSS_WHERE]) if where: # Point: # # # 44.11 -66.23 # # pos = self._attribute_in_structure( where, [XML_TAG_GML_POINT, XML_TAG_GML_POS] ) if pos: return Point(pos[0], pos[1]) # Polygon: # # # # # # -71.106216 42.366661 # -71.105576 42.367104 # -71.104378 42.367134 # -71.103729 42.366249 # -71.098793 42.363331 # -71.101028 42.362541 # -71.106865 42.366123 # -71.106216 42.366661 # # # # # pos_list = self._attribute_in_structure( where, [ XML_TAG_GML_POLYGON, XML_TAG_GML_EXTERIOR, XML_TAG_GML_LINEAR_RING, XML_TAG_GML_POS_LIST, ], ) if pos_list: return self._create_polygon(pos_list) # # 38.3728 # 15.7213 # point = self._attribute([XML_TAG_GEO_POINT]) if point: lat = point.get(XML_TAG_GEO_LAT) long = point.get(XML_TAG_GEO_LONG) if long and lat: return Point(lat, long) # 119.948006 # -23.126413 lat = self._attribute([XML_TAG_GEO_LAT]) long = self._attribute([XML_TAG_GEO_LONG]) if long and lat: return Point(lat, long) # # -34.937663524 148.597260613 # -34.9377026399999 148.597169138 # -34.9377002169999 148.59708737 # -34.9376945989999 148.59705595 # -34.9376863529999 148.596955098 # -34.937663524 148.597260613 # polygon = self._attribute([XML_TAG_GEORSS_POLYGON]) if polygon: # For now, only supporting the first polygon. if isinstance(polygon, list) and isinstance(polygon[0], list): polygon = polygon[0] return self._create_polygon(polygon) # None of the above return None @staticmethod def _create_polygon(coordinates): """Create a polygon from the provided coordinates.""" if coordinates: if len(coordinates) % 2 != 0: # Not even number of coordinates - chop last entry. coordinates = coordinates[0 : len(coordinates) - 1] points = [] for i in range(0, len(coordinates), 2): points.append(Point(coordinates[i], coordinates[i + 1])) return Polygon(points) return None exxamalte-python-georss-client-a61a96f/georss_client/xml_parser/feed_or_feed_item.py000066400000000000000000000054501455637777200312410ustar00rootroot00000000000000"""GeoRSS feed or feed item.""" from __future__ import annotations import datetime from georss_client.consts import ( XML_ATTR_TERM, XML_TAG_AUTHOR, XML_TAG_CATEGORY, XML_TAG_CONTRIBUTOR, XML_TAG_DC_DATE, XML_TAG_LAST_BUILD_DATE, XML_TAG_MANAGING_EDITOR, XML_TAG_NAME, XML_TAG_PUB_DATE, XML_TAG_PUBLISHED, XML_TAG_UPDATED, ) from georss_client.xml_parser.feed_dict_source import FeedDictSource class FeedOrFeedItem(FeedDictSource): """Represents the common base of feed and its items.""" @property def category(self) -> list | None: """Return the categories of this feed item.""" category = self._attribute([XML_TAG_CATEGORY]) if category: if isinstance(category, str) or isinstance(category, dict): # If it's a string or a dict, wrap in list. category = [category] result = [] for item in category: if XML_ATTR_TERM in item: # item = item.get(XML_ATTR_TERM) result.append(item) return result return None @property def published_date(self) -> datetime.datetime | None: """Return the published date of this feed or feed item.""" return self._attribute([XML_TAG_PUB_DATE, XML_TAG_PUBLISHED, XML_TAG_DC_DATE]) @property def pub_date(self) -> datetime.datetime | None: """Return the published date of this feed or feed item.""" return self.published_date @property def updated_date(self) -> datetime.datetime | None: """Return the updated date of this feed or feed item.""" return self._attribute([XML_TAG_LAST_BUILD_DATE, XML_TAG_UPDATED]) @property def last_build_date(self) -> datetime.datetime | None: """Return the last build date of this feed.""" return self.updated_date @property def author(self) -> str | None: """Return the author of this feed.""" # jrc-ems@ec.europa.eu managing_editor = self._attribute([XML_TAG_MANAGING_EDITOR]) if managing_editor: return managing_editor # # Istituto Nazionale di Geofisica e Vulcanologia # http://www.ingv.it # author = self._attribute([XML_TAG_AUTHOR, XML_TAG_CONTRIBUTOR]) if author: name = author.get(XML_TAG_NAME, None) return name return None @property def contributor(self) -> str | None: """Return the contributor of this feed.""" return self.author @property def managing_editor(self) -> str | None: """Return the managing editor of this feed.""" return self.author exxamalte-python-georss-client-a61a96f/georss_client/xml_parser/geometry.py000066400000000000000000000032411455637777200274640ustar00rootroot00000000000000"""Geometry models.""" from __future__ import annotations class Geometry: """Represents a geometry.""" class Point(Geometry): """Represents a point.""" def __init__(self, latitude, longitude): """Initialise point.""" self._latitude = latitude self._longitude = longitude def __repr__(self): """Return string representation of this point.""" return "<{}(latitude={}, longitude={})>".format( self.__class__.__name__, self.latitude, self.longitude ) @property def latitude(self) -> float | None: """Return the latitude of this point.""" return self._latitude @property def longitude(self) -> float | None: """Return the longitude of this point.""" return self._longitude class Polygon(Geometry): """Represents a polygon.""" def __init__(self, points): """Initialise polygon.""" self._points = points def __repr__(self): """Return string representation of this polygon.""" return f"<{self.__class__.__name__}(centroid={self.centroid})>" @property def points(self) -> list | None: """Return the points of this polygon.""" return self._points @property def centroid(self) -> Point: """Find the polygon's centroid as a best approximation.""" longitudes_list = [point.longitude for point in self.points] latitudes_list = [point.latitude for point in self.points] number_of_points = len(self.points) longitude = sum(longitudes_list) / number_of_points latitude = sum(latitudes_list) / number_of_points return Point(latitude, longitude) exxamalte-python-georss-client-a61a96f/pyproject.toml000066400000000000000000000036441455637777200231660ustar00rootroot00000000000000[tool.black] target-version = ['py38', 'py39', 'py310', 'py311', 'py312'] [tool.isort] profile = "black" src_paths = ["georss_client", "tests"] [tool.ruff] target-version = "py38" select = [ "B007", # Loop control variable {name} not used within loop body "B014", # Exception handler with duplicate exception "C", # complexity "D", # docstrings "E", # pycodestyle "F", # pyflakes/autoflake "ICN001", # import concentions; {name} should be imported as {asname} "PGH004", # Use specific rule codes when using noqa "PLC0414", # Useless import alias. Import alias does not rename original package. "SIM105", # Use contextlib.suppress({exception}) instead of try-except-pass "SIM117", # Merge with-statements that use the same scope "SIM118", # Use {key} in {dict} instead of {key} in {dict}.keys() "SIM201", # Use {left} != {right} instead of not {left} == {right} "SIM212", # Use {a} if {a} else {b} instead of {b} if not {a} else {a} "SIM300", # Yoda conditions. Use 'age == 42' instead of '42 == age'. "SIM401", # Use get from dict with default instead of an if block "T20", # flake8-print "TRY004", # Prefer TypeError exception for invalid type "RUF006", # Store a reference to the return value of asyncio.create_task "UP", # pyupgrade "W", # pycodestyle ] ignore = [ "D202", # No blank lines allowed after function docstring "D203", # 1 blank line required before class docstring "D213", # Multi-line docstring summary should start at the second line "D406", # Section name should end with a newline "D407", # Section name underlining "E501", # line too long "E731", # do not assign a lambda expression, use a def # Ignored due to performance: https://github.com/charliermarsh/ruff/issues/2923 "UP038", # Use `X | Y` in `isinstance` call instead of `(X, Y)` ] [tool.pytest.ini_options] testpaths = [ "tests", ] exxamalte-python-georss-client-a61a96f/requirements-test.txt000066400000000000000000000000561455637777200245050ustar00rootroot00000000000000pytest pytest-timeout pytest-xdist pytest-cov exxamalte-python-georss-client-a61a96f/requirements.txt000066400000000000000000000001061455637777200235240ustar00rootroot00000000000000haversine>=2.8.1 xmltodict>=0.13.0 requests>=2.31.0 dateparser>=1.2.0 exxamalte-python-georss-client-a61a96f/setup.cfg000066400000000000000000000005341455637777200220660ustar00rootroot00000000000000[flake8] exclude = .git,.tox,venv,bin,lib,deps,build doctests = True # To work with Black # E501: line too long # W503: Line break occurred before a binary operator # E203: Whitespace before ':' # D202 No blank lines allowed after function docstring # W504 line break after binary operator ignore = E501, W503, E203, D202, W504 exxamalte-python-georss-client-a61a96f/setup.py000066400000000000000000000025261455637777200217620ustar00rootroot00000000000000"""Setup of georss_client library.""" import os from setuptools import find_packages, setup NAME = "georss_client" AUTHOR = "Malte Franken" AUTHOR_EMAIL = "coding@subspace.de" DESCRIPTION = "A GeoRSS client library." URL = "https://github.com/exxamalte/python-georss-client" REQUIRES = [ "haversine>=2.8.1", "xmltodict>=0.13.0", "requests>=2.31.0", "dateparser>=1.2.0", ] with open("README.md") as fh: long_description = fh.read() HERE = os.path.abspath(os.path.dirname(__file__)) VERSION = {} with open(os.path.join(HERE, NAME, "__version__.py")) as f: exec(f.read(), VERSION) # pylint: disable=exec-used setup( name=NAME, version=VERSION["__version__"], author=AUTHOR, author_email=AUTHOR_EMAIL, description=DESCRIPTION, license="Apache-2.0", long_description=long_description, long_description_content_type="text/markdown", url=URL, packages=find_packages(exclude=("tests*",)), classifiers=[ "Programming Language :: Python :: 3.8", "Programming Language :: Python :: 3.9", "Programming Language :: Python :: 3.10", "Programming Language :: Python :: 3.11", "Programming Language :: Python :: 3.12", "License :: OSI Approved :: Apache Software License", "Operating System :: OS Independent", ], install_requires=REQUIRES, ) exxamalte-python-georss-client-a61a96f/tests/000077500000000000000000000000001455637777200214055ustar00rootroot00000000000000exxamalte-python-georss-client-a61a96f/tests/__init__.py000066400000000000000000000005371455637777200235230ustar00rootroot00000000000000"""Tests for georss-client library.""" from georss_client.feed import GeoRssFeed from georss_client.feed_entry import FeedEntry class MockGeoRssFeed(GeoRssFeed): """Mock GeoRSS feed.""" def _new_entry(self, home_coordinates, rss_entry, global_data): """Generate a new entry.""" return FeedEntry(home_coordinates, rss_entry) exxamalte-python-georss-client-a61a96f/tests/fixtures/000077500000000000000000000000001455637777200232565ustar00rootroot00000000000000exxamalte-python-georss-client-a61a96f/tests/fixtures/generic_feed_1.xml000066400000000000000000000032261455637777200266220ustar00rootroot00000000000000 Attribution 1 1234 Title 1 2018-09-23T08:30:00 2018-09-23T08:35:00 -37.2345 149.1234 2345 Title 2 2018-09-23T08:40:00 2018-09-23T08:45:00 -37.4567 149.3456 Title 3 2018-09-23T08:50:00 2018-09-23T08:55:00 -37.6789 149.5678 2018-09-23T09:00:00 2018-09-23T09:05:00 -37.8901 149.7890 5678 Title 5 2018-09-23T09:10:00 2018-09-23T09:15:00 -37.6789 149.5678 6789 Title 6 2018-09-23T09:20:00 2018-09-23T09:25:00 exxamalte-python-georss-client-a61a96f/tests/fixtures/generic_feed_2.xml000066400000000000000000000007261455637777200266250ustar00rootroot00000000000000 Title 1 2018-09-23 08:30:00 1234 Category 1 149.1234 -37.2345 exxamalte-python-georss-client-a61a96f/tests/fixtures/generic_feed_3.xml000066400000000000000000000113261455637777200266240ustar00rootroot00000000000000 Title 1 2018-09-23 08:30:00 1234 Category 1 -34.937663524 148.597260613 -34.9377026399999 148.597169138 -34.9377002169999 148.59708737 -34.9376945989999 148.59705595 -34.9376863529999 148.596955098 -34.937644093 148.596885162 -34.937577786 148.596806399 -34.937470286 148.596710107 -34.9373719229999 148.596710377 -34.9372816329999 148.596754801 -34.9371067559999 148.596801847 -34.9369941959999 148.596833284 -34.936907488 148.596867297 -34.936816829 148.59694105 -34.936707218 148.597012356 -34.93659451 148.597192568 -34.9367060779999 148.597376943 -34.9367854569999 148.597650825 -34.937114943 148.597659041 -34.937336692 148.597589818 -34.937417185 148.597501208 -34.9375217259999 148.597421427 -34.937663524 148.597260613 Title 2 2018-09-23 09:30:00 2345 Category 2 -34.937170989 148.597182317 -34.937663524 148.597260613 -34.9377026399999 148.597169138 -34.9377002169999 148.59708737 -34.9376945989999 148.59705595 -34.9376863529999 148.596955098 -34.937644093 148.596885162 -34.937577786 148.596806399 -34.937470286 148.596710107 -34.9373719229999 148.596710377 -34.9372816329999 148.596754801 -34.9371067559999 148.596801847 -34.9369941959999 148.596833284 -34.936907488 148.596867297 -34.936816829 148.59694105 -34.936707218 148.597012356 -34.93659451 148.597192568 -34.9367060779999 148.597376943 -34.9367854569999 148.597650825 -34.937114943 148.597659041 -34.937336692 148.597589818 -34.937417185 148.597501208 -34.9375217259999 148.597421427 -34.937663524 148.597260613 Title 3 2018-09-23 10:30:00 3456 Category 3 -29.956955208 152.447148625 -29.9576690449999 152.447624518 -29.9589182629999 152.446315814 -29.960405426 152.44536403 -29.962963347 152.445066597 -29.964391023 152.445720949 -29.9658186999999 152.445483003 -29.966473052 152.444531218 -29.967722269 152.444055326 -29.968852512 152.44381738 -29.969030973 152.441735352 -29.9680791869999 152.440069729 -29.9671274029999 152.437928214 -29.967841242 152.436500538 -29.968852512 152.434180563 -29.970756082 152.43251494 -29.970518135 152.430670858 -29.969982757 152.429600101 -29.9693878909999 152.426744747 -29.9693878909999 152.425911936 -29.970042243 152.423413502 -29.97010173 152.420558149 -29.97010173 152.419130473 -29.970934541 152.416869984 -29.972600164 152.41419309 -29.973432975 152.411754144 -29.9728975959999 152.410861845 -29.972600164 152.408006492 -29.9733140009999 152.405805491 -29.9724217039999 152.403663976 -29.970518135 152.404853707 -29.9684361069999 152.4075306 -29.9667704839999 152.409136735 -29.9641530769999 152.410088521 -29.9620115619999 152.409910061 -29.960762346 152.41181363 -29.958680317 152.411992089 -29.956419828 152.41127825 -29.952969611 152.411992089 -29.950352204 152.412765414 -29.950292716 152.442211244 -29.952493718 152.441021513 -29.954635233 152.439831783 -29.956538802 152.439355891 -29.957371613 152.439474863 -29.95725264 152.441021513 -29.957371613 152.442687136 -29.95606291 152.443400974 -29.9536834479999 152.444709679 -29.9518988529999 152.445899408 -29.9505306629999 152.447089139 -29.949400418 152.447802978 -29.949519392 152.447862464 -29.953742935 152.447624518 -29.9554085579999 152.447148625 -29.956955208 152.447148625 -29.9483653529999 152.448029026 -29.949793029 152.447672107 -29.949793029 152.44779108 -29.9508637869999 152.446839296 -29.952410437 152.445649565 -29.955384763 152.443745996 -29.9574073049999 152.442615752 -29.9571098719999 152.439284507 -29.9547898979999 152.439581939 -29.951637112 152.441128589 -29.9487817589999 152.44350805 -29.9477704879999 152.441901914 -29.94705665 152.441723454 -29.9456289729999 152.442080374 -29.9439633499999 152.442913185 -29.9425356739999 152.446482376 -29.94277362 152.449694649 -29.943249512 152.451895651 -29.944915135 152.450884379 -29.946342811 152.450527461 -29.9472945959999 152.450408487 -29.9482463799999 152.449278244 -29.9483653529999 152.448029026 exxamalte-python-georss-client-a61a96f/tests/fixtures/generic_feed_4.xml000066400000000000000000000017711455637777200266300ustar00rootroot00000000000000 Attribution 1 1234 Title 1 UPDATED 2018-09-23T08:30:00 2018-09-23T08:35:00 -37.2345 149.1234 2345 Title 2 2018-09-23T08:40:00 2018-09-23T08:45:00 -37.4567 149.3456 6789 Title 6 2018-09-23T09:20:00 2018-09-23T09:25:00 -37.6789 149.5678 exxamalte-python-georss-client-a61a96f/tests/fixtures/generic_feed_5.xml000066400000000000000000000005171455637777200266260ustar00rootroot00000000000000 Attribution 1 1234 Title 1 -37.2345 149.1234 exxamalte-python-georss-client-a61a96f/tests/fixtures/generic_feed_6.xml000066400000000000000000000007071455637777200266300ustar00rootroot00000000000000 Title 1 2018-09-23 08:30:00 1234 Category 1 149.1234 -37.2345 exxamalte-python-georss-client-a61a96f/tests/fixtures/xml_parser_bom_1.xml000066400000000000000000000002441455637777200272310ustar00rootroot00000000000000 Title 1 exxamalte-python-georss-client-a61a96f/tests/fixtures/xml_parser_complex_1.xml000066400000000000000000000105101455637777200301200ustar00rootroot00000000000000 Feed Title 1 Feed Subtitle 1 Feed Description 1 Feed Link 1 Sun, 09 Dec 2018 08:30:00 GMT 2018-12-09T08:45:00+00:00 Feed Copyright 1 Feed Generator 1 Feed Language 1 http://docs.url/documentation.html 42 Feed Author 1 Feed Category 1 Image Title 1 http://image.url/image.png http://feed.link/feed.rss Image Description 1 123 234 Feed Random 1 Title 1 Description 1 Link 1 Sun, 09 Dec 2018 07:30:00 GMT 2018-12-09T07:45:00+00:00 GUID 1 Source 1 Category 1 Random 1 -37.4567 149.3456 Title 2 Description 2 Sun, 09 Dec 2018 07:35:00 GMT 2018-12-09T07:50:00+00:00 GUID 2 -37.5678 149.4567 Title 3 Description 3 Sun, 09 Dec 2018 07:40:00 GMT 2018-12-09T07:55:00+00:00 GUID 3 Category 3A Category 3B Category 3C -37.6789 149.5678 Title 4 Description 4 Author 4 Sun, 30 Sep 2018 21:36:48 +1000 -37.7890 149.6789 Title 5 Description 5 Thu, 20 Sep 2018 18:01:55 +0200 -30.1 150.1 -30.2 150.2 -30.4 150.4 -30.8 150.8 -30.1 150.1 Title 6 Description 6 Sun, 7 Oct 2018 19:52:00 PDT -30.1 150.1 -30.2 150.2 -30.4 150.4 -30.8 150.8 -30.1 150.1 exxamalte-python-georss-client-a61a96f/tests/fixtures/xml_parser_complex_2.xml000066400000000000000000000020001455637777200301140ustar00rootroot00000000000000 Feed Title 1 Feed Subtitle 1 INVALID Author 1 2018-12-09T09:00:00+00:00 Feed Rights 1 Feed Generator 1 Image Title 1 http://image.url/image.png http://feed.link/feed.rss Title 6 INVALID DATE -37.4567 149.3456 exxamalte-python-georss-client-a61a96f/tests/fixtures/xml_parser_complex_3.xml000066400000000000000000000012641455637777200301300ustar00rootroot00000000000000 <description/> <language/> <pubDate/> <lastBuildDate/> <ttl/> <rights type="text">Feed Rights 1</rights> <item> <title/> <published/> <georss:point/> </item> <item> <title/> <geo:Point> <random/> </geo:Point> </item> </channel> </rss> ��������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������exxamalte-python-georss-client-a61a96f/tests/fixtures/xml_parser_simple_1.xml�����������������������0000664�0000000�0000000�00000000326�14556377772�0027746�0����������������������������������������������������������������������������������������������������ustar�00root����������������������������root����������������������������0000000�0000000������������������������������������������������������������������������������������������������������������������������������������������������������������������������<?xml version="1.0" encoding="UTF-8"?> <rss xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" version="2.0"> <channel> <item> <title>Title 1 exxamalte-python-georss-client-a61a96f/tests/fixtures/xml_parser_simple_2.xml000066400000000000000000000003041455637777200277430ustar00rootroot00000000000000 Title 1 exxamalte-python-georss-client-a61a96f/tests/fixtures/xml_parser_simple_3.xml000066400000000000000000000003321455637777200277450ustar00rootroot00000000000000 Title 1 exxamalte-python-georss-client-a61a96f/tests/test_feed.py000066400000000000000000000220101455637777200237140ustar00rootroot00000000000000"""Tests for feed.""" import datetime import unittest from unittest import mock import pytest import requests from georss_client.consts import UPDATE_ERROR, UPDATE_OK from georss_client.feed import GeoRssFeed from tests import MockGeoRssFeed from tests.utils import load_fixture HOME_COORDINATES_1 = (-31.0, 151.0) HOME_COORDINATES_2 = (-37.0, 150.0) class TestGeoRssFeed(unittest.TestCase): """Test cases for GeoRSS feed.""" @mock.patch("requests.Request") @mock.patch("requests.Session") def test_update_ok(self, mock_session, mock_request): """Test updating feed is ok.""" mock_session.return_value.__enter__.return_value.send.return_value.ok = True mock_session.return_value.__enter__.return_value.send.return_value.text = ( load_fixture("generic_feed_1.xml") ) feed = MockGeoRssFeed(HOME_COORDINATES_1, None) assert ( repr(feed) == "" ) status, entries = feed.update() assert status == UPDATE_OK self.assertIsNotNone(entries) assert len(entries) == 5 feed_entry = entries[0] assert feed_entry.title == "Title 1" assert feed_entry.external_id == "1234" assert feed_entry.category == "Category 1" assert feed_entry.published == datetime.datetime(2018, 9, 23, 8, 30) assert feed_entry.updated == datetime.datetime(2018, 9, 23, 8, 35) assert feed_entry.coordinates == (-37.2345, 149.1234) self.assertAlmostEqual(feed_entry.distance_to_home, 714.4, 1) feed_entry = entries[1] assert feed_entry.title == "Title 2" assert feed_entry.external_id == "2345" self.assertIsNone(feed_entry.attribution) assert repr(feed_entry) == "" feed_entry = entries[2] assert feed_entry.title == "Title 3" assert feed_entry.external_id == "Title 3" feed_entry = entries[3] self.assertIsNone(feed_entry.title) assert feed_entry.external_id == hash(feed_entry.coordinates) feed_entry = entries[4] assert feed_entry.title == "Title 5" assert feed_entry.external_id == "5678" @mock.patch("requests.Request") @mock.patch("requests.Session") def test_update_ok_feed_2(self, mock_session, mock_request): """Test updating feed is ok.""" mock_session.return_value.__enter__.return_value.send.return_value.ok = True mock_session.return_value.__enter__.return_value.send.return_value.text = ( load_fixture("generic_feed_2.xml") ) feed = MockGeoRssFeed(HOME_COORDINATES_1, None) status, entries = feed.update() assert status == UPDATE_OK self.assertIsNotNone(entries) assert len(entries) == 1 feed_entry = entries[0] assert feed_entry.title == "Title 1" assert feed_entry.external_id == "1234" assert feed_entry.category == "Category 1" assert feed_entry.coordinates == (-37.2345, 149.1234) self.assertAlmostEqual(feed_entry.distance_to_home, 714.4, 1) @mock.patch("requests.Request") @mock.patch("requests.Session") def test_update_ok_feed_3(self, mock_session, mock_request): """Test updating feed is ok.""" mock_session.return_value.__enter__.return_value.send.return_value.ok = True mock_session.return_value.__enter__.return_value.send.return_value.text = ( load_fixture("generic_feed_3.xml") ) feed = MockGeoRssFeed(HOME_COORDINATES_1, None) status, entries = feed.update() assert status == UPDATE_OK self.assertIsNotNone(entries) assert len(entries) == 3 feed_entry = entries[0] assert feed_entry.external_id == "1234" assert feed_entry.coordinates == ( pytest.approx(-34.93728111547821), pytest.approx(148.59710883878262), ) self.assertAlmostEqual(feed_entry.distance_to_home, 491.7, 1) feed_entry = entries[1] assert feed_entry.external_id == "2345" assert feed_entry.coordinates == (-34.937170989, 148.597182317) self.assertAlmostEqual(feed_entry.distance_to_home, 491.8, 1) feed_entry = entries[2] assert feed_entry.external_id == "3456" assert feed_entry.coordinates == ( pytest.approx(-29.962746645660683), pytest.approx(152.43090880416074), ) self.assertAlmostEqual(feed_entry.distance_to_home, 176.5, 1) @mock.patch("requests.Request") @mock.patch("requests.Session") def test_update_ok_feed_6(self, mock_session, mock_request): """Test updating feed is ok.""" mock_session.return_value.__enter__.return_value.send.return_value.ok = True mock_session.return_value.__enter__.return_value.send.return_value.text = ( load_fixture("generic_feed_6.xml") ) feed = MockGeoRssFeed(HOME_COORDINATES_1, None) status, entries = feed.update() assert status == UPDATE_OK self.assertIsNotNone(entries) assert len(entries) == 1 feed_entry = entries[0] assert feed_entry.title == "Title 1" assert feed_entry.external_id == "1234" assert feed_entry.category == "Category 1" assert feed_entry.coordinates == (-37.2345, 149.1234) self.assertAlmostEqual(feed_entry.distance_to_home, 714.4, 1) @mock.patch("requests.Request") @mock.patch("requests.Session") def test_update_ok_with_radius_filtering(self, mock_session, mock_request): """Test updating feed is ok.""" mock_session.return_value.__enter__.return_value.send.return_value.ok = True mock_session.return_value.__enter__.return_value.send.return_value.text = ( load_fixture("generic_feed_1.xml") ) feed = MockGeoRssFeed(HOME_COORDINATES_2, None, filter_radius=90.0) status, entries = feed.update() assert status == UPDATE_OK self.assertIsNotNone(entries) assert len(entries) == 4 self.assertAlmostEqual(entries[0].distance_to_home, 82.0, 1) self.assertAlmostEqual(entries[1].distance_to_home, 77.0, 1) self.assertAlmostEqual(entries[2].distance_to_home, 84.6, 1) @mock.patch("requests.Request") @mock.patch("requests.Session") def test_update_ok_with_radius_and_category_filtering( self, mock_session, mock_request ): """Test updating feed is ok.""" mock_session.return_value.__enter__.return_value.send.return_value.ok = True mock_session.return_value.__enter__.return_value.send.return_value.text = ( load_fixture("generic_feed_1.xml") ) feed = MockGeoRssFeed( HOME_COORDINATES_2, None, filter_radius=90.0, filter_categories=["Category 2"], ) status, entries = feed.update() assert status == UPDATE_OK self.assertIsNotNone(entries) assert len(entries) == 1 self.assertAlmostEqual(entries[0].distance_to_home, 77.0, 1) feed = MockGeoRssFeed( HOME_COORDINATES_2, None, filter_radius=90.0, filter_categories=["Category 4"], ) status, entries = feed.update() assert status == UPDATE_OK self.assertIsNotNone(entries) assert len(entries) == 0 @mock.patch("requests.Request") @mock.patch("requests.Session") def test_update_error(self, mock_session, mock_request): """Test updating feed results in error.""" mock_session.return_value.__enter__.return_value.send.return_value.ok = False feed = MockGeoRssFeed(HOME_COORDINATES_1, None) status, entries = feed.update() assert status == UPDATE_ERROR @mock.patch("requests.Request") @mock.patch("requests.Session") def test_update_with_request_exception(self, mock_session, mock_request): """Test updating feed raises exception.""" mock_session.return_value.__enter__.return_value.send.side_effect = ( requests.exceptions.RequestException ) feed = GeoRssFeed(HOME_COORDINATES_1, None) status, entries = feed.update() assert status == UPDATE_ERROR self.assertIsNone(entries) @mock.patch("requests.Request") @mock.patch("requests.Session") def test_update_bom(self, mock_session, mock_request): """Test updating feed with BOM (byte order mark) is ok.""" mock_session.return_value.__enter__.return_value.send.return_value.ok = True mock_session.return_value.__enter__.return_value.send.return_value.text = ( load_fixture("xml_parser_bom_1.xml") ) feed = MockGeoRssFeed(HOME_COORDINATES_1, None) assert ( repr(feed) == "" ) status, entries = feed.update() assert status == UPDATE_OK self.assertIsNotNone(entries) assert len(entries) == 0 exxamalte-python-georss-client-a61a96f/tests/test_feed_entry.py000066400000000000000000000045361455637777200251520ustar00rootroot00000000000000"""Tests for feed entry.""" import datetime import unittest from unittest import mock from georss_client import FeedEntry class TestFeedEntry(unittest.TestCase): """Test cases or feed entry.""" def test_simple_feed_entry(self): """Test feed entry behaviour.""" feed_entry = FeedEntry(None, None) assert repr(feed_entry) == "" self.assertIsNone(feed_entry.geometry) self.assertIsNone(feed_entry.coordinates) self.assertIsNone(feed_entry.title) self.assertIsNone(feed_entry.category) self.assertIsNone(feed_entry.attribution) self.assertIsNone(feed_entry.description) self.assertIsNone(feed_entry.published) self.assertIsNone(feed_entry.updated) self.assertIsNone( feed_entry._search_in_external_id(r"External ID (?P.+)$") ) self.assertIsNone( feed_entry._search_in_title(r"Title (?P.+)$") ) self.assertIsNone( feed_entry._search_in_description(r"Description (?P.+)$") ) def test_feed_entry_search_in_attributes(self): """Test feed entry behaviour.""" rss_entry = mock.MagicMock() type(rss_entry).guid = mock.PropertyMock(return_value="Test 123") type(rss_entry).title = mock.PropertyMock(return_value="Title 123") type(rss_entry).description = mock.PropertyMock(return_value="Description 123") type(rss_entry).category = mock.PropertyMock( return_value=["Category 1", "Category 2"] ) updated = datetime.datetime(2019, 4, 1, 8, 30, tzinfo=datetime.timezone.utc) type(rss_entry).updated_date = mock.PropertyMock(return_value=updated) feed_entry = FeedEntry(None, rss_entry) assert repr(feed_entry) == "" assert ( feed_entry._search_in_external_id(r"Test (?P.+)$") == "123" ) assert feed_entry._search_in_title(r"Title (?P.+)$") == "123" assert ( feed_entry._search_in_description(r"Description (?P.+)$") == "123" ) assert feed_entry.category == "Category 1" assert feed_entry.description == "Description 123" assert feed_entry.updated == updated exxamalte-python-georss-client-a61a96f/tests/test_feed_manager.py000066400000000000000000000140541455637777200254170ustar00rootroot00000000000000"""Test for the Feed Manager.""" import datetime import unittest from unittest import mock from georss_client.feed_manager import FeedManagerBase from tests import MockGeoRssFeed from tests.utils import load_fixture HOME_COORDINATES_1 = (-31.0, 151.0) HOME_COORDINATES_2 = (-37.0, 150.0) class TestFeedManager(unittest.TestCase): """Test cases for feed manager.""" @mock.patch("requests.Request") @mock.patch("requests.Session") def test_feed_manager(self, mock_session, mock_request): """Test the feed manager.""" mock_session.return_value.__enter__.return_value.send.return_value.ok = True mock_session.return_value.__enter__.return_value.send.return_value.text = ( load_fixture("generic_feed_1.xml") ) feed = MockGeoRssFeed(HOME_COORDINATES_1, None) # This will just record calls and keep track of external ids. generated_entity_external_ids = [] updated_entity_external_ids = [] removed_entity_external_ids = [] def _generate_entity(external_id): """Generate new entity.""" generated_entity_external_ids.append(external_id) def _update_entity(external_id): """Update entity.""" updated_entity_external_ids.append(external_id) def _remove_entity(external_id): """Remove entity.""" removed_entity_external_ids.append(external_id) feed_manager = FeedManagerBase( feed, _generate_entity, _update_entity, _remove_entity ) assert ( repr(feed_manager) == ")>" ) feed_manager.update() entries = feed_manager.feed_entries self.assertIsNotNone(entries) assert len(entries) == 5 self.assertIsNotNone(feed_manager.last_update) assert feed_manager.last_timestamp == datetime.datetime(2018, 9, 23, 9, 10) assert len(generated_entity_external_ids) == 5 assert len(updated_entity_external_ids) == 0 assert len(removed_entity_external_ids) == 0 feed_entry = entries.get("1234") assert feed_entry.title == "Title 1" assert feed_entry.external_id == "1234" assert feed_entry.coordinates == (-37.2345, 149.1234) self.assertAlmostEqual(feed_entry.distance_to_home, 714.4, 1) assert repr(feed_entry) == "" feed_entry = entries.get("2345") assert feed_entry.title == "Title 2" assert feed_entry.external_id == "2345" feed_entry = entries.get("Title 3") assert feed_entry.title == "Title 3" assert feed_entry.external_id == "Title 3" external_id = hash((-37.8901, 149.7890)) feed_entry = entries.get(external_id) self.assertIsNone(feed_entry.title) assert feed_entry.external_id == external_id feed_entry = entries.get("5678") assert feed_entry.title == "Title 5" assert feed_entry.external_id == "5678" # Simulate an update with several changes. generated_entity_external_ids.clear() updated_entity_external_ids.clear() removed_entity_external_ids.clear() mock_session.return_value.__enter__.return_value.send.return_value.text = ( load_fixture("generic_feed_4.xml") ) feed_manager.update() entries = feed_manager.feed_entries self.assertIsNotNone(entries) assert len(entries) == 3 assert len(generated_entity_external_ids) == 1 assert len(updated_entity_external_ids) == 2 assert len(removed_entity_external_ids) == 3 feed_entry = entries.get("1234") assert feed_entry.title == "Title 1 UPDATED" feed_entry = entries.get("2345") assert feed_entry.title == "Title 2" feed_entry = entries.get("6789") assert feed_entry.title == "Title 6" # Simulate an update with no data. generated_entity_external_ids.clear() updated_entity_external_ids.clear() removed_entity_external_ids.clear() mock_session.return_value.__enter__.return_value.send.return_value.ok = False feed_manager.update() entries = feed_manager.feed_entries assert len(entries) == 0 assert len(generated_entity_external_ids) == 0 assert len(updated_entity_external_ids) == 0 assert len(removed_entity_external_ids) == 3 @mock.patch("requests.Request") @mock.patch("requests.Session") def test_feed_manager_no_timestamp(self, mock_session, mock_request): """Test updating feed is ok.""" mock_session.return_value.__enter__.return_value.send.return_value.ok = True mock_session.return_value.__enter__.return_value.send.return_value.text = ( load_fixture("generic_feed_5.xml") ) feed = MockGeoRssFeed(HOME_COORDINATES_1, None) # This will just record calls and keep track of external ids. generated_entity_external_ids = [] updated_entity_external_ids = [] removed_entity_external_ids = [] def _generate_entity(external_id): """Generate new entity.""" generated_entity_external_ids.append(external_id) def _update_entity(external_id): """Update entity.""" updated_entity_external_ids.append(external_id) def _remove_entity(external_id): """Remove entity.""" removed_entity_external_ids.append(external_id) feed_manager = FeedManagerBase( feed, _generate_entity, _update_entity, _remove_entity ) assert ( repr(feed_manager) == ")>" ) feed_manager.update() entries = feed_manager.feed_entries self.assertIsNotNone(entries) assert len(entries) == 1 self.assertIsNone(feed_manager.last_timestamp) exxamalte-python-georss-client-a61a96f/tests/test_geo_rss_distance_helper.py000066400000000000000000000054051455637777200276740ustar00rootroot00000000000000"""Tests for georss distance helper.""" import unittest from unittest.mock import MagicMock from georss_client.geo_rss_distance_helper import GeoRssDistanceHelper from georss_client.xml_parser.geometry import Point, Polygon class TestGeoRssDistanceHelper(unittest.TestCase): """Tests for the GeoJSON distance helper.""" def test_extract_coordinates_from_point(self): """Test extracting coordinates from point.""" mock_point = Point(-30.0, 151.0) latitude, longitude = GeoRssDistanceHelper.extract_coordinates(mock_point) assert latitude == -30.0 assert longitude == 151.0 def test_extract_coordinates_from_polygon(self): """Test extracting coordinates from polygon.""" mock_polygon = Polygon( [ Point(-30.0, 151.0), Point(-30.0, 151.5), Point(-30.5, 151.5), Point(-30.5, 151.0), Point(-30.0, 151.0), ] ) latitude, longitude = GeoRssDistanceHelper.extract_coordinates(mock_polygon) self.assertAlmostEqual(latitude, -30.2, 1) self.assertAlmostEqual(longitude, 151.2, 1) def test_extract_coordinates_from_unsupported_geometry(self): """Test extracting coordinates from unsupported geometry.""" mock_unsupported_geometry = MagicMock() latitude, longitude = GeoRssDistanceHelper.extract_coordinates( mock_unsupported_geometry ) self.assertIsNone(latitude) self.assertIsNone(longitude) def test_distance_to_point(self): """Test calculating distance to point.""" home_coordinates = [-31.0, 150.0] mock_point = Point(-30.0, 151.0) distance = GeoRssDistanceHelper.distance_to_geometry( home_coordinates, mock_point ) self.assertAlmostEqual(distance, 146.8, 1) def test_distance_to_polygon(self): """Test calculating distance to point.""" home_coordinates = [-31.0, 150.0] mock_polygon = Polygon( [ Point(-30.0, 151.0), Point(-30.0, 151.5), Point(-30.5, 151.5), Point(-30.5, 151.0), Point(-30.0, 151.0), ] ) distance = GeoRssDistanceHelper.distance_to_geometry( home_coordinates, mock_polygon ) self.assertAlmostEqual(distance, 110.6, 1) def test_distance_to_unsupported_geometry(self): """Test calculating distance to unsupported geometry.""" home_coordinates = [-31.0, 150.0] mock_unsupported_geometry = MagicMock() distance = GeoRssDistanceHelper.distance_to_geometry( home_coordinates, mock_unsupported_geometry ) assert distance == float("inf") exxamalte-python-georss-client-a61a96f/tests/test_init.py000066400000000000000000000000341455637777200237560ustar00rootroot00000000000000"""Tests for base class.""" exxamalte-python-georss-client-a61a96f/tests/test_xml_parser.py000066400000000000000000000262121455637777200251750ustar00rootroot00000000000000"""Tests for XML parser.""" import datetime import unittest from pyexpat import ExpatError from georss_client.xml_parser import XmlParser from georss_client.xml_parser.geometry import Point, Polygon from tests.utils import load_fixture class TestXmlParser(unittest.TestCase): """Test the XML parser.""" def test_simple_1(self): """Test parsing various actual XML files.""" xml_parser = XmlParser() xml = load_fixture("xml_parser_simple_1.xml") feed = xml_parser.parse(xml) self.assertIsNotNone(feed) self.assertIsNotNone(feed.entries) assert len(feed.entries) == 1 def test_simple_2(self): """Test parsing various actual XML files.""" xml_parser = XmlParser() xml = load_fixture("xml_parser_simple_2.xml") feed = xml_parser.parse(xml) self.assertIsNotNone(feed) self.assertIsNotNone(feed.entries) assert len(feed.entries) == 1 def test_simple_3(self): """Test parsing various actual XML files.""" xml_parser = XmlParser() xml = load_fixture("xml_parser_simple_3.xml") feed = xml_parser.parse(xml) self.assertIsNone(feed) def test_complex_1(self): """Test parsing various actual XML files.""" xml_parser = XmlParser() xml = load_fixture("xml_parser_complex_1.xml") feed = xml_parser.parse(xml) self.assertIsNotNone(feed) assert feed.title == "Feed Title 1" assert feed.subtitle == "Feed Subtitle 1" assert feed.description == "Feed Description 1" assert feed.summary == "Feed Description 1" assert feed.content == "Feed Description 1" assert feed.link == "Feed Link 1" assert feed.published_date == datetime.datetime( 2018, 12, 9, 8, 30, tzinfo=datetime.timezone.utc ) assert feed.pub_date == datetime.datetime( 2018, 12, 9, 8, 30, tzinfo=datetime.timezone.utc ) assert feed.updated_date == datetime.datetime( 2018, 12, 9, 8, 45, tzinfo=datetime.timezone.utc ) assert feed.last_build_date == datetime.datetime( 2018, 12, 9, 8, 45, tzinfo=datetime.timezone.utc ) assert feed.copyright == "Feed Copyright 1" assert feed.rights == "Feed Copyright 1" assert feed.generator == "Feed Generator 1" assert feed.language == "Feed Language 1" assert feed.docs == "http://docs.url/documentation.html" assert feed.ttl == 42 assert feed.author == "Feed Author 1" assert feed.contributor == "Feed Author 1" assert feed.managing_editor == "Feed Author 1" assert feed.category == ["Feed Category 1"] self.assertIsNotNone(feed.image) assert feed.image.title == "Image Title 1" assert feed.image.url == "http://image.url/image.png" assert feed.image.link == "http://feed.link/feed.rss" assert feed.image.description == "Image Description 1" assert feed.image.width == 123 assert feed.image.height == 234 assert feed.get_additional_attribute("random") == "Feed Random 1" assert repr(feed) == "" self.assertIsNotNone(feed.entries) assert len(feed.entries) == 6 feed_entry = feed.entries[0] assert feed_entry.title == "Title 1" assert feed_entry.description == "Description 1" assert feed_entry.link == "Link 1" assert feed_entry.published_date == datetime.datetime( 2018, 12, 9, 7, 30, tzinfo=datetime.timezone.utc ) assert feed_entry.updated_date == datetime.datetime( 2018, 12, 9, 7, 45, tzinfo=datetime.timezone.utc ) assert feed_entry.guid == "GUID 1" assert feed_entry.id == "GUID 1" assert feed_entry.source == "Source 1" assert feed_entry.category == ["Category 1"] self.assertIsInstance(feed_entry.geometry, Point) assert feed_entry.geometry.latitude == -37.4567 assert feed_entry.geometry.longitude == 149.3456 assert feed_entry.get_additional_attribute("random") == "Random 1" assert repr(feed_entry) == "" feed_entry = feed.entries[1] assert feed_entry.title == "Title 2" assert feed_entry.description == "Description 2" assert feed_entry.link == "Link 2" assert feed_entry.published_date == datetime.datetime( 2018, 12, 9, 7, 35, tzinfo=datetime.timezone.utc ) assert feed_entry.updated_date == datetime.datetime( 2018, 12, 9, 7, 50, tzinfo=datetime.timezone.utc ) assert feed_entry.guid == "GUID 2" assert feed_entry.category == ["Category 2"] self.assertIsInstance(feed_entry.geometry, Point) assert feed_entry.geometry.latitude == -37.5678 assert feed_entry.geometry.longitude == 149.4567 feed_entry = feed.entries[2] assert feed_entry.title == "Title 3" assert feed_entry.description == "Description 3" assert feed_entry.published_date == datetime.datetime( 2018, 12, 9, 7, 40, tzinfo=datetime.timezone.utc ) assert feed_entry.updated_date == datetime.datetime( 2018, 12, 9, 7, 55, tzinfo=datetime.timezone.utc ) assert feed_entry.guid == "GUID 3" assert feed_entry.category == ["Category 3A", "Category 3B", "Category 3C"] self.assertIsInstance(feed_entry.geometry, Point) assert feed_entry.geometry.latitude == -37.6789 assert feed_entry.geometry.longitude == 149.5678 feed_entry = feed.entries[3] assert feed_entry.title == "Title 4" assert feed_entry.description == "Description 4" assert feed_entry.author == "Author 4" assert feed_entry.contributor == "Author 4" assert feed_entry.category == ["Category 4A", "Category 4B"] assert feed_entry.published_date == datetime.datetime( 2018, 9, 30, 21, 36, 48, tzinfo=datetime.timezone(datetime.timedelta(hours=10), "AEST"), ) self.assertIsInstance(feed_entry.geometry, Point) assert feed_entry.geometry.latitude == -37.789 assert feed_entry.geometry.longitude == 149.6789 feed_entry = feed.entries[4] assert feed_entry.title == "Title 5" assert feed_entry.description == "Description 5" assert feed_entry.published_date == datetime.datetime( 2018, 9, 20, 18, 1, 55, tzinfo=datetime.timezone(datetime.timedelta(hours=2), "CEST"), ) self.assertIsInstance(feed_entry.geometry, Polygon) assert feed_entry.geometry.centroid.latitude == -30.32 assert feed_entry.geometry.centroid.longitude == 150.32 feed_entry = feed.entries[5] assert feed_entry.title == "Title 6" assert feed_entry.description == "Description 6" assert feed_entry.published_date == datetime.datetime( 2018, 10, 7, 19, 52, tzinfo=datetime.timezone(datetime.timedelta(hours=-7), "PDT"), ) self.assertIsInstance(feed_entry.geometry, Polygon) assert feed_entry.geometry.centroid.latitude == -30.32 assert feed_entry.geometry.centroid.longitude == 150.32 def test_complex_2(self): """Test parsing various actual XML files.""" xml_parser = XmlParser() xml = load_fixture("xml_parser_complex_2.xml") feed = xml_parser.parse(xml) self.assertIsNotNone(feed) assert feed.title == "Feed Title 1" assert feed.subtitle == "Feed Subtitle 1" assert feed.ttl == "INVALID" assert feed.author == "Author 1" assert feed.last_build_date == datetime.datetime( 2018, 12, 9, 9, 0, tzinfo=datetime.timezone.utc ) assert feed.updated_date == datetime.datetime( 2018, 12, 9, 9, 0, tzinfo=datetime.timezone.utc ) assert feed.copyright == "Feed Rights 1" assert feed.rights == "Feed Rights 1" assert feed.generator == "Feed Generator 1" self.assertIsNotNone(feed.image) assert feed.image.title == "Image Title 1" assert feed.image.url == "http://image.url/image.png" assert feed.image.link == "http://feed.link/feed.rss" self.assertIsNone(feed.image.description) self.assertIsNone(feed.image.width) self.assertIsNone(feed.image.height) self.assertIsNone(feed.docs) self.assertIsNotNone(feed.entries) assert len(feed.entries) == 1 feed_entry = feed.entries[0] assert feed_entry.title == "Title 6" self.assertIsNone(feed_entry.published_date) def test_complex_3(self): """Test parsing various actual XML files.""" xml_parser = XmlParser() xml = load_fixture("xml_parser_complex_3.xml") feed = xml_parser.parse(xml) self.assertIsNotNone(feed) self.assertIsNone(feed.title) self.assertIsNone(feed.subtitle) self.assertIsNone(feed.description) self.assertIsNone(feed.language) self.assertIsNone(feed.published_date) self.assertIsNone(feed.last_build_date) self.assertIsNone(feed.ttl) assert feed.rights == "Feed Rights 1" self.assertIsNone(feed.image) self.assertIsNotNone(feed.entries) assert len(feed.entries) == 2 feed_entry = feed.entries[0] self.assertIsNone(feed_entry.title) self.assertIsNone(feed_entry.published_date) self.assertIsNone(feed_entry.geometry) feed_entry = feed.entries[1] self.assertIsNone(feed_entry.title) self.assertIsNone(feed_entry.geometry) def test_byte_order_mark(self): """Test parsing an XML file with byte order mark.""" xml_parser = XmlParser() # Create XML starting with byte order mark. xml = ( "\xef\xbb\xbf" "Title 1" "" ) # This will raise an error because the parser can't handle with self.assertRaises(ExpatError): xml_parser.parse(xml) class TestGeometries(unittest.TestCase): """Test geometries.""" def test_point(self): """Test point.""" point = Point(-37.1234, 149.2345) assert point.latitude == -37.1234 assert point.longitude == 149.2345 assert repr(point) == "" def test_polygon(self): """Test polygon.""" polygon = Polygon( [ Point(-30.1, 150.1), Point(-30.2, 150.2), Point(-30.4, 150.4), Point(-30.8, 150.8), Point(-30.1, 150.1), ] ) assert len(polygon.points) == 5 assert polygon.centroid.latitude == -30.32 assert polygon.centroid.longitude == 150.32 assert ( repr(polygon) == ")>" ) exxamalte-python-georss-client-a61a96f/tests/utils.py000066400000000000000000000003531455637777200231200ustar00rootroot00000000000000"""Test utilities.""" import os def load_fixture(filename): """Load a fixture.""" path = os.path.join(os.path.dirname(__file__), "fixtures", filename) with open(path, encoding="utf-8") as fptr: return fptr.read()