Skip to content

Commit

Permalink
Github Actions Enhancements (#143)
Browse files Browse the repository at this point in the history
* Add python caching

* Expand git diff, cleanup temp file. Print validated shows and seasons

* Reorganize script, add comments

* Move tvdb init after checking for new mappings
  • Loading branch information
evie-lau authored Oct 8, 2024
1 parent 7bf452f commit 79918db
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 14 deletions.
5 changes: 5 additions & 0 deletions .github/workflows/validate-seasons.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@ jobs:
uses: actions/setup-python@v5
with:
python-version: ${{ env.PYTHON_VERSION }}
- name: Cache python env
uses: actions/cache@v4
with:
path: ${{ env.pythonLocation }}
key: ${{ env.pythonLocation }}-${{ hashFiles('validate-tvdb.py') }}
- name: Install dependencies
run: |
python -m pip install --upgrade pip
Expand Down
33 changes: 19 additions & 14 deletions validate-tvdb.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,6 @@
from dotenv import load_dotenv
from pathlib import Path

# Load API Key and initialize tvdb
load_dotenv()
apikey = os.getenv("TVDB_API_KEY")
tvdb = tvdb_v4_official.TVDB(apikey)

def getTvdbId(showName):
# Get TVDB ID of show
showId = None
Expand Down Expand Up @@ -47,24 +42,27 @@ def validateShowSeasons(showName, seasonsToFind):
print("Did not find season(s): " + str(invalidSeasons) + " in show: " + showName)
return errors

# Parse temp.yaml and validate shows/seasons against TVDB
def validateMappings():
errors = 0
with open("temp.yaml") as f:
mappings = yaml.safe_load(f)
for show in sorted(mappings['entries'], key=lambda entry: (entry['title'], entry['seasons'])):
showName = show['title']
seasons = [s['season'] for s in show['seasons'] if 'season' in s]
# print(showName + ": " + str(seasons))
print("Validating: " + showName + ": " + str(seasons))
errors += validateShowSeasons(showName, seasons)
return errors

# Get diff compared to main branch
def get_diff(file_path, commit_old='origin/master', commit_new='HEAD'):
diff_output = subprocess.run(
['git', 'diff', commit_old, commit_new, '--', file_path],
['git', 'diff', '-U20', commit_old, commit_new, '--', file_path],
capture_output=True, text=True
)
return diff_output.stdout

# Parse diff for changed mapping entries
def extract_changed_groups(diff_output):
changes = []
change_group = []
Expand All @@ -90,12 +88,7 @@ def extract_changed_groups(diff_output):
sys.exit()
return changes

def extractNewMappings():
diff_output = get_diff("custom_mappings.yaml")
change_groups = extract_changed_groups(diff_output)
createTempYaml(change_groups)

# Create new yaml with changed entries
# Create temp yaml with changed entries
def createTempYaml(change_groups):
lines = []
lines.append("entries:\n")
Expand All @@ -108,12 +101,24 @@ def createTempYaml(change_groups):
with open('temp.yaml', 'w') as file:
file.write(''.join(lines))

# Parse `git diff` for new mapped entries and write into temp yaml file
def extractNewMappings():
diff_output = get_diff("custom_mappings.yaml")
change_groups = extract_changed_groups(diff_output)
createTempYaml(change_groups)

def cleanup():
os.remove("temp.yaml")


# TODO: cross reference anilist-id show name
extractNewMappings()
# Load API Key and initialize tvdb
load_dotenv()
apikey = os.getenv("TVDB_API_KEY")
tvdb = tvdb_v4_official.TVDB(apikey)

errors = validateMappings()
if errors != 0:
sys.exit("Found "+ str(errors) + " error(s) in the season mappings")
# cleanup()
cleanup()

0 comments on commit 79918db

Please sign in to comment.