From 26f746ae0b6285f3584c4dd571ae9c0da49bd062 Mon Sep 17 00:00:00 2001 From: Yuval Adam <_@yuv.al> Date: Sun, 22 Oct 2023 21:10:04 +0200 Subject: Rename build to fetch --- Pipfile | 1 + README.md | 6 ++++++ pakarlib/build.py | 64 ------------------------------------------------------- pakarlib/fetch.py | 64 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 71 insertions(+), 64 deletions(-) delete mode 100644 pakarlib/build.py create mode 100644 pakarlib/fetch.py diff --git a/Pipfile b/Pipfile index da0e66b..87a5e38 100644 --- a/Pipfile +++ b/Pipfile @@ -9,4 +9,5 @@ requests = "*" [dev-packages] [scripts] +fetch = "python pakarlib/fetch.py" build = "python pakarlib/build.py" diff --git a/README.md b/README.md index 6246f0e..e39d59c 100644 --- a/README.md +++ b/README.md @@ -6,6 +6,12 @@ Unified data for Pikud Ha'Oref. All data is versioned, but you can run the build $ pipenv run build ``` +Or fetch all files: + +```bash +$ pipenv run fetch +``` + Currently supports: - Fetching cities and districts metadata in 4 languages (he, ar, en, ru) from Pikud Ha'Oref unofficial ajax APIs diff --git a/pakarlib/build.py b/pakarlib/build.py deleted file mode 100644 index 3f9336b..0000000 --- a/pakarlib/build.py +++ /dev/null @@ -1,64 +0,0 @@ -import requests - -from pathlib import Path - -DATA_DIR = Path(__file__).parent / "data" - -def get_cities(): - print("Fetching cities...") - LANGS = ["he", "ar", "en", "ru"] - BASE_URL = "https://www.oref.org.il/Shared/Ajax/GetCitiesMix.aspx" - for lang in LANGS: - print(f"Fetching {lang}...") - res = requests.get(BASE_URL, params={"lang": lang}) - if "Access Denied" in res.text: - print("Access denied, must run from Israeli IP") - break - with open(DATA_DIR / "cities"/ f"{lang}.json", "wb") as f: - f.write(res.content) - -def get_districts(): - print("Fetching districts...") - LANGS = ["he", "ar", "en", "ru"] - BASE_URL = "https://www.oref.org.il/Shared/Ajax/GetDistricts.aspx" - for lang in LANGS: - print(f"Fetching {lang}...") - res = requests.get(BASE_URL, params={"lang": lang}) - if "Access Denied" in res.text: - print("Access denied, must run from Israeli IP") - break - with open(DATA_DIR / "districts"/ f"{lang}.json", "wb") as f: - f.write(res.content) - -def get_segments(): - print("Fetching segments...") - LOCALES = ["iw_IL", "en_US", "ru_RU", "ar_EG"] - for locale in LOCALES: - print(f"Fetching {locale}...") - URL = "https://dist.meser-hadash.org.il/smart-dist/services/anonymous/segments/android" - res = requests.get(URL, params={"instance": "1544803905", "locale": locale}) - locale = locale.replace("iw", "he") - with open(DATA_DIR / "segments" / f"{locale[:2]}.json", "wb") as f: - f.write(res.content) - -def get_polygons(): - print("Fetching segments...") - BASE_URL = "https://dist.meser-hadash.org.il/smart-dist/services/anonymous/polygon/id/android" - sess = requests.Session() - - for i in range(5000000, 5005000): - res = sess.get(BASE_URL, params={"id": str(i), "instance": "1544803905", "locale": "iw_IL"}) - if "CDATA" in res.text: - print(res.text) - print(f"{i} - got CDATA error, skipping") - else: - with open(DATA_DIR / "polygons" / f"{i}.json", "wb") as f: - cont = res.content - print(f"{i} - writing {len(cont)} bytes") - f.write(cont) - -if __name__ == "__main__": - get_cities() - get_districts() - get_segments() - get_polygons() diff --git a/pakarlib/fetch.py b/pakarlib/fetch.py new file mode 100644 index 0000000..3f9336b --- /dev/null +++ b/pakarlib/fetch.py @@ -0,0 +1,64 @@ +import requests + +from pathlib import Path + +DATA_DIR = Path(__file__).parent / "data" + +def get_cities(): + print("Fetching cities...") + LANGS = ["he", "ar", "en", "ru"] + BASE_URL = "https://www.oref.org.il/Shared/Ajax/GetCitiesMix.aspx" + for lang in LANGS: + print(f"Fetching {lang}...") + res = requests.get(BASE_URL, params={"lang": lang}) + if "Access Denied" in res.text: + print("Access denied, must run from Israeli IP") + break + with open(DATA_DIR / "cities"/ f"{lang}.json", "wb") as f: + f.write(res.content) + +def get_districts(): + print("Fetching districts...") + LANGS = ["he", "ar", "en", "ru"] + BASE_URL = "https://www.oref.org.il/Shared/Ajax/GetDistricts.aspx" + for lang in LANGS: + print(f"Fetching {lang}...") + res = requests.get(BASE_URL, params={"lang": lang}) + if "Access Denied" in res.text: + print("Access denied, must run from Israeli IP") + break + with open(DATA_DIR / "districts"/ f"{lang}.json", "wb") as f: + f.write(res.content) + +def get_segments(): + print("Fetching segments...") + LOCALES = ["iw_IL", "en_US", "ru_RU", "ar_EG"] + for locale in LOCALES: + print(f"Fetching {locale}...") + URL = "https://dist.meser-hadash.org.il/smart-dist/services/anonymous/segments/android" + res = requests.get(URL, params={"instance": "1544803905", "locale": locale}) + locale = locale.replace("iw", "he") + with open(DATA_DIR / "segments" / f"{locale[:2]}.json", "wb") as f: + f.write(res.content) + +def get_polygons(): + print("Fetching segments...") + BASE_URL = "https://dist.meser-hadash.org.il/smart-dist/services/anonymous/polygon/id/android" + sess = requests.Session() + + for i in range(5000000, 5005000): + res = sess.get(BASE_URL, params={"id": str(i), "instance": "1544803905", "locale": "iw_IL"}) + if "CDATA" in res.text: + print(res.text) + print(f"{i} - got CDATA error, skipping") + else: + with open(DATA_DIR / "polygons" / f"{i}.json", "wb") as f: + cont = res.content + print(f"{i} - writing {len(cont)} bytes") + f.write(cont) + +if __name__ == "__main__": + get_cities() + get_districts() + get_segments() + get_polygons() -- cgit v1.3.1