Skip to content

armgit / soju

Public Imported from GitHub
0

Play Windows games on Apple Silicon Macs. Free, open-source app that installs the official Battle.net, Steam, Epic and GOG launchers and lists your games in one library.

3.0 KB
Raw Blame
#!/usr/bin/env python3
"""Soju service bridge. Account library snapshots are accepted only on stdin."""
import argparse
import json
import os
from pathlib import Path
import sqlite3
import sys
from library_services import (ServiceError, search, steam_owned, gog_owned, gog_users, xbox_owned,
                              save_account, account_path)

def stop(signum, frame):
    # subprocess.run kills its child when an exception interrupts it, so a request the
    # app cancels does not leave xodus-cli running.
    raise KeyboardInterrupt


def main():
    import signal
    signal.signal(signal.SIGTERM, stop)
    parser = argparse.ArgumentParser(description=__doc__)
    parser.add_argument('action', choices=['search', 'sync', 'forget', 'gog-users'])
    parser.add_argument('value', nargs='?', default='')
    parser.add_argument('--country', default='US')
    parser.add_argument('--base', default=os.environ.get('SOJU_BASE', str(Path.home()/'.battlenet-macos')))
    args = parser.parse_args()
    try:
        if args.action == 'search':
            result = search(args.value, args.country)
        elif args.action == 'gog-users':
            result = {'users': gog_users(args.base)}
        elif args.action == 'forget':
            account_path(args.base, args.value).unlink(missing_ok=True)
            result = {'message': 'Account import removed. Installed games are kept.'}
        else:
            payload = sys.stdin.read(8 * 1024 * 1024 + 1)
            if len(payload) > 8 * 1024 * 1024:
                raise ServiceError('Account settings are too large.')
            settings = json.loads(payload or '{}')
            if not isinstance(settings, dict):
                raise ServiceError('Invalid account settings.')
            if args.value == 'steam':
                games, label = steam_owned(settings)
            elif args.value == 'gog':
                games, label = gog_owned(args.base, settings)
            elif args.value == 'xbox':
                notices = []
                games, label = xbox_owned(args.base, settings, notices=notices)
            else:
                raise ServiceError('Unsupported account provider.')
            market = settings.get('market') if args.value == 'xbox' else None
            result = save_account(args.base, args.value, games, label, market or None)
            if args.value == 'xbox' and notices:
                result['message'] += ' ' + ' '.join(notices)
                result['notice'] = notices[0]
        print(json.dumps(result, ensure_ascii=False))
    except ServiceError as error:
        print(json.dumps({'error': str(error)}))
        return 1
    except sqlite3.Error:
        print(json.dumps({'error': 'Galaxy library is busy or its format changed. Let Galaxy finish loading, then retry.'}))
        return 1
    except Exception:
        # Do not include raw exceptions, response bodies or credentials in logs.
        print(json.dumps({'error': 'Could not complete this request. Your previous account import is unchanged.'}))
        return 1
    return 0

if __name__ == '__main__':
    sys.exit(main())