diff --git a/.github/workflows/validate-seasons.yml b/.github/workflows/validate-seasons.yml index ef09da9..085fed3 100644 --- a/.github/workflows/validate-seasons.yml +++ b/.github/workflows/validate-seasons.yml @@ -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 diff --git a/validate-tvdb.py b/validate-tvdb.py index f04bcd8..7a422d4 100644 --- a/validate-tvdb.py +++ b/validate-tvdb.py @@ -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 @@ -47,6 +42,7 @@ 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: @@ -54,17 +50,19 @@ def validateMappings(): 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 = [] @@ -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") @@ -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() \ No newline at end of file +cleanup() \ No newline at end of file