Poll Helldivers API and Cache News Feed #46965
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| name: Poll Helldivers API and Cache News Feed | |
| on: | |
| schedule: | |
| # Runs at every 10th minute | |
| - cron: '*/12 * * * *' | |
| jobs: | |
| fetch_news: | |
| runs-on: ubuntu-latest | |
| steps: | |
| - name: Check out repository | |
| uses: actions/checkout@v2 | |
| - name: Setup Python | |
| uses: actions/setup-python@v2 | |
| with: | |
| python-version: '3.x' | |
| - name: Install dependencies | |
| run: | | |
| python -m pip install --upgrade pip | |
| pip install requests | |
| - name: Fetch and update feed data | |
| run: | | |
| import json | |
| import re | |
| import requests | |
| # Fetch the current war ID | |
| def fetch_war_id(): | |
| url = 'https://api.live.prod.thehelldiversgame.com/api/WarSeason/current/WarID' | |
| response = requests.get(url) | |
| return response.json()['id'] | |
| WAR_ID = fetch_war_id() | |
| # purely wanting the defense time remaining from this | |
| # fetch current campaigns from helldiverstrainingmanual api, until more robust solution devd | |
| def fetch_campaigns(): | |
| url = f'https://helldiverstrainingmanual.com/api/v1/war/campaign' | |
| response = requests.get(url) | |
| campaign_info = response.json() | |
| return campaign_info | |
| def fetch_planet_info(): | |
| url = f'https://helldiverstrainingmanual.com/api/v1/planets' | |
| response = requests.get(url) | |
| planet_info = response.json() | |
| return planet_info | |
| def fetch_galaxy_stats(): | |
| url = f'https://api.live.prod.thehelldiversgame.com/api/Stats/War/{WAR_ID}/Summary' | |
| response = requests.get(url) | |
| galaxy_stats = response.json() | |
| return galaxy_stats | |
| def sanitize_message(message): | |
| # Remove HTML-like tags from the message | |
| return re.sub(r'<[^>]*>', '', message) | |
| def fetch_news(from_timestamp): | |
| url = f'https://helldiverstrainingmanual.com/api/v1/war/news?from={from_timestamp}' | |
| response = requests.get(url) | |
| news_items = response.json() | |
| # Sanitize each news item's message | |
| for item in news_items: | |
| if 'message' in item: | |
| item['message'] = sanitize_message(item['message']) | |
| return news_items | |
| # Load existing feed data | |
| feed_file = 'feed/news.json' | |
| try: | |
| with open(feed_file, 'r') as file: | |
| news_data = json.load(file) | |
| last_published = news_data[-1]['published'] | |
| except (FileNotFoundError, IndexError): | |
| news_data = [] | |
| # Fetch new items | |
| new_news = fetch_news(last_published + 1) | |
| # Update the news data with only new unique ids and the last published number | |
| if new_news: | |
| for item in new_news: | |
| if not any(existing_item['id'] == item['id'] for existing_item in news_data): | |
| news_data.append(item) | |
| last_published = new_news[-1]['published'] | |
| # Save updated news data | |
| with open(feed_file, 'w') as file: | |
| json.dump(news_data, file, indent=2) | |
| # Fetch and save planet info | |
| planet_info = fetch_planet_info() | |
| with open('planets/additionalPlanetInfo.json', 'w') as file: | |
| json.dump(planet_info, file, indent=2) | |
| # Fetch and save campaign info for defense time remaining | |
| campaign_info = fetch_campaigns() | |
| with open('planets/campaignInfo.json', 'w') as file: | |
| json.dump(campaign_info, file, indent=2) | |
| # Fetch and save galaxy stats | |
| galaxy_stats = fetch_galaxy_stats() | |
| with open('planets/galaxyStatistics.json', 'w') as file: | |
| json.dump(galaxy_stats, file, indent=2) | |
| shell: python | |
| - name: Commit and push changes | |
| run: | | |
| git config --local user.email "action@github.com" | |
| git config --local user.name "GitHub Action" | |
| git add feed/news.json planets/additionalPlanetInfo.json planets/galaxyStatistics.json planets/campaignInfo.json | |
| git commit -m "Update news, additional planet info, and galaxy statistics data" -a || echo "No changes to commit" | |
| git push |