Skip to content

Commit

Permalink
initializing...
Browse files Browse the repository at this point in the history
  • Loading branch information
ken-morel committed May 31, 2024
1 parent ba56bd7 commit 09e14fa
Show file tree
Hide file tree
Showing 4 changed files with 158 additions and 0 deletions.
45 changes: 45 additions & 0 deletions .github/workflows/geoCountry.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
name: geoCountry

on:
issues:
types: [opened]

jobs:
locate:
permissions:
issues: write
contents: write
runs-on: ubuntu-latest
if: startsWith(github.event.issue.title, 'locate:')

steps:
- uses: actions/checkout@v3

# Set up a Python 3.10 (64-bit) instance
- name: Setting up Python
uses: actions/setup-python@v4
with:
python-version: "3.10"
architecture: "x64"

# Use pip to install the dependencies and then run the script
- name: geoLocate
env:
ISSUE_NUMBER: ${{ github.event.issue.number }}
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
REPOSITORY_OWNER: ${{ github.repository_owner }}
run: |
pip install -r requirements.txt
python main.py
# Create new commit with the changed files and push it to GitHub
- name: Commit and push changes
env:
ISSUE_TITLE: ${{ github.event.issue.title }}
ISSUE_AUTHOR: ${{ github.event.issue.user.login }}
run: |
git config --global user.name "github-actions[bot]"
git config --global user.email "github-actions[bot]@users.noreply.github.com"
git add .
git commit -m "${ISSUE_TITLE} by ${ISSUE_AUTHOR}"
git push
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# county geo-json

Good day, country-geojson is a little initiative to provide
the geojson data for markup of a specific country, or countries.

It is inspired from Play-Chess of the1Riddle and relies on similar concepts.

<!-- BEGIN countryList -->
<!-- END countryList -->


<!-- BEGIN geoJSON -->
<!-- END geoJSON -->
98 changes: 98 additions & 0 deletions main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
import json

import os
import sys
import requests
from github import Github

import pycountry
from urllib.parse import urlencode


def get_country_json(nameOrAlpha: str):
country = pycountry.lookup(nameOrAlpha)

countryCode = country.alpha_3

re = requests.get(
'https://geodata.ucdavis.edu'
f'/gadm/gadm4.1/json/gadm41_{countryCode}_0.json'
)

if re.status_code != 200:
sys.exit(5)

text = re.text

# the API provides ready prepare data for geoJSON
open(f'data/{country.alpha_3}.json', 'w').write(text)

return text


def parse_issue_country(title: str):
"""Parse issue title and return a tuple with (action, <move>)"""
if title.startswith('locate: '):
return title[8:]


def main(issue, issue_author, repo_owner):
country_name = parse_issue_country(issue.title)

geojson = get_country_json(country_name)

insert_json(geojson)


def insert_json(text: str):
with open('./README.md') as file:
readme = file.read()
readme = replace_text_between(readme, 'geoJSON', text)

table = '|couyntry|link|'
for country in pycountry.countries:
issue_link = create_issue_link(country.name)
table += f'\n|{country.name}|{issue_link}'

readme = replace_text_between(readme, 'countryList', table)

with open('README.md', 'w') as file:
file.write(readme)

# # # # from the1Riddle/Play-Chess


def create_issue_link(country):
return "https://github.com/{repo}/issues/new?{params}".format(
repo=os.environ["GITHUB_REPOSITORY"],
params=urlencode({
'title': f'locate: {country}',
'body': 'Do not touch anything just submit the issue',
}, safe="{}")
)


def replace_text_between(original_text, markerName, replacement_text):
"""Replace text between `marker['begin']` and `marker['end']`
with `replacement`"""
begin = f'<!-- BEGIN {markerName} -->'
end = f'<!-- END {markerName} -->'

if original_text.find(begin) == -1 or original_text.find(end) == -1:
return original_text

leading_text = original_text.split(begin)[0]
trailing_text = original_text.split(end)[1]

return leading_text + begin + replacement_text + end + trailing_text


if __name__ == '__main__':
repo = Github(
os.environ['GITHUB_TOKEN']
).get_repo(os.environ['GITHUB_REPOSITORY'])
issue = repo.get_issue(number=int(os.environ['ISSUE_NUMBER']))
issue_author = '@' + issue.user.login
repo_owner = '@' + os.environ['REPOSITORY_OWNER']

ret, reason = main(issue)
2 changes: 2 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
pycountry
github

0 comments on commit 09e14fa

Please sign in to comment.