Skip to content

Commit

Permalink
feat: Now support alpha2 code (#95)
Browse files Browse the repository at this point in the history
  • Loading branch information
sumit-158 committed Feb 6, 2023
1 parent a66d07f commit 6eda756
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 19 deletions.
29 changes: 18 additions & 11 deletions app/knowledge_panels.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,14 @@
from .config import openFoodFacts, settings
from .exception_wrapper import no_exception
from .i18n import translate as _
from .models import HungerGameFilter, Taxonomies, country_to_ISO_code, facet_plural, singularize
from .models import (
HungerGameFilter,
Taxonomies,
alpha2_to_country_name,
country_name_to_alpha2,
pluralize,
singularize,
)
from .off import data_quality, last_edit, wikidata_helper


Expand All @@ -22,7 +29,7 @@ def __init__(
self.value = value
self.sec_facet = singularize(sec_facet)
self.sec_value = sec_value
self.country = country
self.country = alpha2_to_country_name(country)

async def hunger_game_kp(self):
query = {}
Expand Down Expand Up @@ -96,18 +103,18 @@ async def data_quality_kp(self):
description = ""
if self.facet == "country":
self.country = self.value
country_code = country_to_ISO_code(value=self.value)
country_code = country_name_to_alpha2(value=self.value)
url = openFoodFacts(country_code)
path = ""
self.facet = self.value = None
if self.sec_facet == "country":
self.country = self.sec_value
country_code = country_to_ISO_code(value=self.sec_value)
country_code = country_name_to_alpha2(value=self.sec_value)
url = openFoodFacts(country_code)
path = ""
self.sec_facet = self.sec_value = None
if self.country is not None:
country_code = country_to_ISO_code(value=self.country)
country_code = country_name_to_alpha2(value=self.country)
url = openFoodFacts(country_code)
path = ""
description += f"{self.country} "
Expand Down Expand Up @@ -155,16 +162,16 @@ async def last_edits_kp(self):
description = ""
if self.facet == "country":
self.country = self.value
country_code = country_to_ISO_code(value=self.value)
country_code = country_name_to_alpha2(value=self.value)
url = openFoodFacts(country_code)
self.facet = self.value = None
if self.sec_facet == "country":
self.country = self.sec_value
country_code = country_to_ISO_code(value=self.sec_value)
country_code = country_name_to_alpha2(value=self.sec_value)
url = openFoodFacts(country_code)
self.sec_facet = self.sec_value = None
if self.country is not None:
country_code = country_to_ISO_code(value=self.country)
country_code = country_name_to_alpha2(value=self.country)
url = openFoodFacts(country_code)
source_url = f"{url}?sort_by=last_modified_t"
description += f"{self.country} "
Expand All @@ -174,11 +181,11 @@ async def last_edits_kp(self):
description += f"{self.facet}"
source_url = f"{url}/{self.facet}?sort_by=last_modified_t"
if self.value is not None:
query[f"{facet_plural(facet=self.facet)}_tags_en"] = self.value
query[f"{pluralize(facet=self.facet)}_tags_en"] = self.value
description += f" {self.value}"
source_url = f"{url}/{self.facet}/{self.value}?sort_by=last_modified_t"
if self.sec_value and self.sec_facet is not None:
query[f"{facet_plural(facet=self.sec_facet)}_tags_en"] = self.sec_value
query[f"{pluralize(facet=self.sec_facet)}_tags_en"] = self.sec_value
description += f" {self.sec_facet} {self.sec_value}"
source_url = f"{url}/{self.facet}/{self.value}/{self.sec_facet}/{self.sec_value}?sort_by=last_modified_t" # noqa: E501
data = await last_edit(url=url, query=query)
Expand All @@ -202,7 +209,7 @@ async def last_edits_kp(self):
async def _wikidata_kp(self, facet, value):
query = {}
if value:
query["tagtype"] = facet_plural(facet=facet)
query["tagtype"] = pluralize(facet=facet)
query["fields"] = "wikidata"
query["tags"] = value
return await wikidata_helper(query=query, value=value)
Expand Down
26 changes: 18 additions & 8 deletions app/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,21 +43,31 @@ def list():
return [c.value for c in Taxonomies]


def country_to_ISO_code(value: str):
def alpha2_to_country_name(value: Optional[str]):
"""
Helper function that return ISO code for country
Helper function to return country name for aplha2 code
"""
country_data = pycountry.countries.get(name=value)
if country_data is not None:
country_iso_code = country_data.alpha_2
return f"{country_iso_code.lower()}-en"
if value is not None and len(value) == 2:
country = pycountry.countries.get(alpha_2=value)
if country is not None:
return f"{country.name}"
return value


def country_name_to_alpha2(value: Optional[str]):
"""
Helper function that return alpha2 code for country name
"""
country = pycountry.countries.get(name=value)
if country is not None:
return f"{(country.alpha_2).lower()}-en"
return "world"


inflectEngine = inflect.engine()


def facet_plural(facet: str):
def pluralize(facet: str):
"""
Return plural form of facet
"""
Expand Down Expand Up @@ -123,7 +133,7 @@ def country_query():
query = Query(
default=None,
title="Country tag string",
description="To return knowledge panels for specific country, ex: `france`.",
description="To return knowledge panels for specific country, ex: `france` or `fr`.",
)
return query

Expand Down

0 comments on commit 6eda756

Please sign in to comment.