-
Notifications
You must be signed in to change notification settings - Fork 5
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
integrate TED-API v3 and add eForms sample data #518
Merged
Merged
Changes from 1 commit
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
5636c54
integrate TED-API v3 and add eForms sample data
CaptainOfHacks 2b0f0d8
fix tests
CaptainOfHacks 9f028d2
fix tests
CaptainOfHacks 0d5939a
fix tests
CaptainOfHacks 800f480
fix tests
CaptainOfHacks f5ca3d0
Update ted_api.py
CaptainOfHacks 7d8774d
fix tests
CaptainOfHacks File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,33 +1,31 @@ | ||
import base64 | ||
import json | ||
import pathlib | ||
from datetime import date | ||
from typing import List | ||
from typing import List, Generator | ||
|
||
import requests | ||
|
||
from ted_sws import config | ||
from ted_sws.event_manager.services.log import log_warning | ||
from ted_sws.notice_fetcher.adapters.ted_api_abc import TedAPIAdapterABC, RequestAPI | ||
|
||
DEFAULT_TED_API_QUERY_RESULT_SIZE = {"pageSize": 100, | ||
"pageNum": 1, | ||
"scope": 3 | ||
DEFAULT_TED_API_QUERY_RESULT_SIZE = {"limit": 100, | ||
"page": 1, | ||
"scope": "ALL", | ||
} | ||
|
||
DEFAULT_TED_API_QUERY_RESULT_FIELDS = {"fields": ["AA", "AC", "CY", "DD", "DI", "DS", "TVL", "TY", | ||
"DT", "MA", "NC", "ND", "OC", "OJ", "OL", "OY", | ||
"PC", "PD", "PR", "RC", "RN", "RP", "TD", "TVH", | ||
"CONTENT", | ||
# INFO: This query result fields is not supported correctly by TED-API. | ||
#"notice-type", "award-criterion-type", "corporate-body", | ||
#"funding", "notice-identifier", "notice-version" | ||
]} | ||
|
||
TOTAL_DOCUMENTS_NUMBER = "total" | ||
RESPONSE_RESULTS = "results" | ||
DEFAULT_TED_API_QUERY_RESULT_FIELDS = {"fields": ["ND", "PD", "RN"]} | ||
|
||
TOTAL_DOCUMENTS_NUMBER = "totalNoticeCount" | ||
RESPONSE_RESULTS = "notices" | ||
DOCUMENT_CONTENT = "content" | ||
RESULT_PAGE_NUMBER = "pageNum" | ||
RESULT_PAGE_NUMBER = "page" | ||
TED_API_FIELDS = "fields" | ||
DOCUMENT_CONTENT_FIELD = "CONTENT" | ||
LINKS_TO_CONTENT_KEY = "links" | ||
XML_CONTENT_KEY = "xml" | ||
MULTIPLE_LANGUAGE_CONTENT_KEY = "MUL" | ||
ENGLISH_LANGUAGE_CONTENT_KEY = "ENG" | ||
DOCUMENT_NOTICE_ID_KEY = "ND" | ||
|
||
|
||
class TedRequestAPI(RequestAPI): | ||
|
@@ -40,13 +38,12 @@ def __call__(self, api_url: str, api_query: dict) -> dict: | |
:return: dict | ||
""" | ||
|
||
response = requests.get(api_url, params=api_query) | ||
response = requests.post(api_url, json=api_query) | ||
if response.ok: | ||
response_content = json.loads(response.text) | ||
return response_content | ||
else: | ||
raise Exception(f"The TED-API call failed with: {response}") | ||
|
||
raise Exception(f"The TED-API call failed with: {response}, {response.content}, {api_url}") | ||
|
||
|
||
class TedAPIAdapter(TedAPIAdapterABC): | ||
|
@@ -71,7 +68,7 @@ def get_by_wildcard_date(self, wildcard_date: str) -> List[dict]: | |
:return: List[str] | ||
""" | ||
|
||
query = {"q": f"PD=[{wildcard_date}]"} | ||
query = {"query": f"PD={wildcard_date}"} | ||
|
||
return self.get_by_query(query=query) | ||
|
||
|
@@ -83,48 +80,82 @@ def get_by_range_date(self, start_date: date, end_date: date) -> List[dict]: | |
:return:List[str] | ||
""" | ||
|
||
date_filter = f">={start_date.strftime('%Y%m%d')} AND <={end_date.strftime('%Y%m%d')}" | ||
date_filter = f"PD>={start_date.strftime('%Y%m%d')} AND PD<={end_date.strftime('%Y%m%d')}" | ||
|
||
query = {"q": f"PD=[{date_filter}]"} | ||
query = {"query": date_filter} | ||
|
||
return self.get_by_query(query=query) | ||
|
||
def get_by_query(self, query: dict, result_fields: dict = None) -> List[dict]: | ||
def _retrieve_document_content(self, document_content: dict) -> str: | ||
""" | ||
Method to retrieve a document content from the TedApi API | ||
:param document_content: | ||
:return:str ' | ||
""" | ||
xml_links = document_content[LINKS_TO_CONTENT_KEY][XML_CONTENT_KEY] | ||
language_key = MULTIPLE_LANGUAGE_CONTENT_KEY | ||
if language_key not in xml_links.keys(): | ||
if ENGLISH_LANGUAGE_CONTENT_KEY in xml_links.keys(): | ||
language_key = ENGLISH_LANGUAGE_CONTENT_KEY | ||
else: | ||
language_key = xml_links.keys()[0] | ||
|
||
log_warning( | ||
f"Language key {MULTIPLE_LANGUAGE_CONTENT_KEY} not found in {document_content[DOCUMENT_NOTICE_ID_KEY]}," | ||
f" and will be used language key {language_key}!") | ||
|
||
xml_document_content_link = xml_links[language_key] | ||
response = requests.get(xml_document_content_link) | ||
|
||
if response.ok: | ||
return response.text | ||
else: | ||
raise Exception(f"The notice content can't be loaded!: {response}, {response.content}") | ||
|
||
def get_generator_by_query(self, query: dict, result_fields: dict = None) -> Generator[dict, None, None]: | ||
""" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. is there a test for this function? |
||
Method to get a documents content by passing a query to the API (json) | ||
:param query: | ||
:param result_fields: | ||
:return:List[str] | ||
:return:Generator[dict] | ||
""" | ||
query.update(DEFAULT_TED_API_QUERY_RESULT_SIZE) | ||
query.update(result_fields or DEFAULT_TED_API_QUERY_RESULT_FIELDS) | ||
response_body = self.request_api(api_url=self.ted_api_url, api_query=query) | ||
|
||
documents_number = response_body[TOTAL_DOCUMENTS_NUMBER] | ||
result_pages = 1 + int(documents_number) // 100 | ||
documents_content = response_body[RESPONSE_RESULTS] | ||
|
||
for page_number in range(2, result_pages + 1): | ||
query[RESULT_PAGE_NUMBER] = page_number | ||
response_body = self.request_api(api_url=self.ted_api_url, api_query=query) | ||
documents_content += response_body[RESPONSE_RESULTS] | ||
if DOCUMENT_CONTENT_FIELD in query[TED_API_FIELDS]: | ||
decoded_documents_content = [] | ||
for document_content in documents_content: | ||
document_content[DOCUMENT_CONTENT] = base64.b64decode(document_content[DOCUMENT_CONTENT]).decode( | ||
encoding="utf-8") | ||
decoded_documents_content.append(document_content) | ||
return decoded_documents_content | ||
if result_pages > 1: | ||
for page_number in range(2, result_pages + 1): | ||
query[RESULT_PAGE_NUMBER] = page_number | ||
response_body = self.request_api(api_url=self.ted_api_url, api_query=query) | ||
documents_content += response_body[RESPONSE_RESULTS] | ||
for document_content in documents_content: | ||
document_content[DOCUMENT_CONTENT] = self._retrieve_document_content(document_content) | ||
del document_content[LINKS_TO_CONTENT_KEY] | ||
yield document_content | ||
else: | ||
return documents_content | ||
for document_content in documents_content: | ||
document_content[DOCUMENT_CONTENT] = self._retrieve_document_content(document_content) | ||
del document_content[LINKS_TO_CONTENT_KEY] | ||
yield document_content | ||
|
||
def get_by_query(self, query: dict, result_fields: dict = None) -> List[dict]: | ||
""" | ||
Method to get a documents content by passing a query to the API (json) | ||
:param query: | ||
:param result_fields: | ||
:return:List[dict] | ||
""" | ||
return list(self.get_generator_by_query(query=query, result_fields=result_fields)) | ||
|
||
def get_by_id(self, document_id: str) -> dict: | ||
""" | ||
Method to get a document content by passing an ID | ||
:param document_id: | ||
:return: str | ||
:return: dict | ||
""" | ||
|
||
query = {"q": f"ND=[{document_id}]"} | ||
query = {"query": f"ND={document_id}"} | ||
|
||
return self.get_by_query(query=query)[0] |
31 changes: 31 additions & 0 deletions
31
tests/e2e/notice_fetcher/_test_generate_eforms_sample_dataset.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import pathlib | ||
|
||
TED_API_EFORMS_QUERY = """ | ||
TD NOT IN (C E G I D P M Q O R 0 1 2 3 4 5 6 7 8 9 B S Y V F A H J K) AND | ||
notice-subtype IN ({eforms_subtype}) AND | ||
FT~"eforms-sdk-{eforms_sdk_version}" | ||
""" | ||
|
||
EFORMS_SUBTYPES = [10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24] | ||
EFORMS_SDK_VERSIONS = [f"1.{version}" for version in range(3, 11)] | ||
|
||
|
||
def _test_generate_eforms_sample_dataset(ted_document_search): | ||
results_path = pathlib.Path(__file__).parent / "eforms_samples" | ||
|
||
for eforms_sdk_version in EFORMS_SDK_VERSIONS: | ||
for eforms_subtype in EFORMS_SUBTYPES: | ||
results_dir_path = results_path / f"eforms_sdk_v{eforms_sdk_version}" / f"eform_subtype_{eforms_subtype}" | ||
|
||
print(f"Load for {results_dir_path}") | ||
query = {"query": TED_API_EFORMS_QUERY.format(eforms_sdk_version=eforms_sdk_version, | ||
eforms_subtype=eforms_subtype)} | ||
print(query) | ||
notices = ted_document_search.get_generator_by_query(query=query) | ||
for sample_id in range(1, 2): | ||
notice = next(notices, None) | ||
if notice is None: | ||
break | ||
results_dir_path.mkdir(parents=True, exist_ok=True) | ||
result_notice_xml_path = results_dir_path / f"{notice['ND']}.xml" | ||
result_notice_xml_path.write_text(notice["content"], encoding="utf-8") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is there a test for this function?