Last active
September 7, 2023 14:49
-
-
Save Facenapalm/3c8e8a2444a36b6a463c2b25baa0fcf1 to your computer and use it in GitHub Desktop.
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
# Copyright (c) 2023 Facenapalm | |
# | |
# Permission is hereby granted, free of charge, to any person obtaining a copy | |
# of this software and associated documentation files (the "Software"), to deal | |
# in the Software without restriction, including without limitation the rights | |
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
# copies of the Software, and to permit persons to whom the Software is | |
# furnished to do so, subject to the following conditions: | |
# | |
# The above copyright notice and this permission notice shall be included in all | |
# copies or substantial portions of the Software. | |
# | |
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
# SOFTWARE. | |
import re | |
import argparse | |
from steam_parser import SteamPage, remove_duplicates, descriptions_data | |
def generate_source(steam_page): | |
date = steam_page.get_retrieve_date() | |
return f'\tS248\tQ337535\tS1733\t"{steam_page.get_id()}"\tS813\t+{date.year}-{date.month:02d}-{date.day:02d}T00:00:00Z/11' | |
def parse_page(steam_id): | |
result = ['CREATE'] | |
steam_page = SteamPage(steam_id) | |
title = steam_page.get_title() | |
instance = steam_page.get_instance() | |
status = steam_page.get_release_status() | |
source = generate_source(steam_page) | |
if not re.match(r'^[A-Za-zĀ-ſ0-9\s,.?!\-+*/=_–—:;~\'’"„“«»\(\)\[\]&]+$', title): | |
raise RuntimeError(f'game title `{title}` filtered out') | |
if status == 'unreleased': | |
raise RuntimeError('unreleased applications are not supported') | |
base_property = None | |
base_game = None | |
if instance == 'game': | |
result.append('LAST\tDar\t"لعبة فيديو"') | |
if status == 'early access': | |
instance = 'early access' | |
elif instance == 'dlc': | |
base_property = 'P8646' | |
base_game = steam_page.get_dlc_base_game_item() | |
elif instance == 'mod': | |
base_property = 'P7075' | |
base_game = steam_page.get_mod_base_game_item() | |
if instance not in descriptions_data: | |
raise RuntimeError(f'{instance} items are not supported') | |
platforms = [item.getID() for item in steam_page.get_platform_items()] | |
date = steam_page.get_release_date() | |
for langcode, _, description in descriptions_data[instance]: | |
result.append(f'LAST\tL{langcode}\t"{title}"') | |
result.append(f'LAST\tD{langcode}\t"{description.format(date.year)}"') | |
result.append(f'LAST\tP31\t{steam_page.get_instance_item().getID()}' + source) | |
result.append(f'LAST\tP1733\t"{steam_id}"' + ''.join(f'\tP400\t{platform}' for platform in platforms)) | |
if base_property and base_game: | |
result.append(f'LAST\t{base_property}\t{base_game.getID()}' + source) | |
result.append('LAST\tP437\tQ269415\tS3452\tQ337535') # distribution format = digital distribution | |
result.append('LAST\tP750\tQ337535' + source) # distributed by = Steam | |
line = f'LAST\tP577\t+{date.year}-{date.month:02d}-{date.day:02d}T00:00:00Z/11' | |
if status == 'early_access': | |
line += '\tP3831\tQ17042291' # subject has role = early access | |
result.append(line + source) | |
for platform in platforms: | |
result.append(f'LAST\tP400\t{platform}' + source) | |
for gamemode in steam_page.get_gamemode_items(): | |
result.append(f'LAST\tP404\t{gamemode.getID()}' + source) | |
for language, qualifiers in steam_page.get_language_items(): | |
result.append(f'LAST\tP407\t{language.getID()}' + ''.join(f'\tP518\t{qualifier.getID()}' for qualifier in qualifiers) + source) | |
metacritic = steam_page.get_metacritic_id() | |
if metacritic: | |
result.append(f'LAST\tP1712\t"{metacritic}"\tP400\tQ16338' + source) | |
return result | |
def main(): | |
parser = argparse.ArgumentParser() | |
parser.add_argument('-input', '-i', default='input.txt', type=argparse.FileType('r')) | |
parser.add_argument('-output', '-o', default='output.txt', type=argparse.FileType('w', encoding='utf-8')) | |
args = parser.parse_args() | |
ids = remove_duplicates([line.strip() for line in args.input]) | |
for steam_id in ids: | |
try: | |
args.output.write('\n'.join(parse_page(steam_id)) + '\n\n\n') | |
args.output.flush() | |
except RuntimeError as error: | |
print(f"{steam_id}: {error}") | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment