Commit 4b7224e5 authored by Kourser's avatar Kourser
Browse files

feat: synchronize multilingual App Store listings and screenshots by API

parent 6b893340
Loading
Loading
Loading
Loading
+50 −10
Changes for appstore/README.md: 50 added lines, 10 removed lines.
Original line number Diff line number Diff line
@@ -26,14 +26,15 @@ confidentialité sont enregistrés. Les textes préparés sont conservés dans
- [Assistance](https://kourser.pages.git.cythin.eu/podbook/reference/depannage/).
- [Données et confidentialité](https://kourser.pages.git.cythin.eu/podbook/reference/confidentialite/).

Le site a été publié par le [pipeline GitLab 333](https://git.cythin.eu/Kourser/podbook/-/pipelines/333),
commit `4545b540`. Le domaine de namespace est utilisé comme pour Envorenn.
Le site et la galerie multilingue ont été déployés par le
[pipeline GitLab 335](https://git.cythin.eu/Kourser/podbook/-/pipelines/335),
commit `6b893340`. Le domaine de namespace est utilisé comme pour Envorenn.

## Suite de la préparation

Compléter les droits sur le contenu, les déclarations de confidentialité App
Store, l’âge recommandé, le tarif, les territoires et les coordonnées de vérification.
Préparer des captures réelles et un build signé pour chaque plateforme, puis
Fournir un build signé pour chaque plateforme, puis
valider la traduction et la narration sur appareil réel avant soumission.

App Store Connect signale un contrat Apple Developer mis à jour à revoir par
@@ -83,14 +84,49 @@ du lecteur reste préservée.
### Enregistrement App Store

Les fichiers locaux et les captures ne prouvent pas leur envoi à Apple.
`scripts/store-browser.mjs` utilise uniquement les contrôles visibles du
navigateur CUA authentifié ; `upload-status.json` conserve sa progression.
Aucune soumission ni publication d’app n’est effectuée par ce chemin.
Utiliser désormais l’API officielle avec `scripts/asc_sync.py`, comme pour
Skingomz, et une clé de rôle **App Manager**. Le script lit les variables
`ASC_DELIVER_KEY_ID`, `ASC_DELIVER_KEY_PATH` et `ASC_ISSUER_ID` dans
l’environnement ou dans un fichier existant fourni avec `--credentials-env`.
Il ne copie aucune clé dans le dépôt et ne journalise pas les jetons ni les
URL temporaires d’envoi. Le runtime Python fourni contient `cryptography`.

L’enregistrement du nom **Podbook** a été refusé par Apple pour la nouvelle
fiche anglaise, car ce nom est déjà utilisé. Une variante de présentation
doit être choisie avant de finaliser cette localisation. Le statut d’envoi
doit distinguer les éléments préparés, enregistrés et vérifiés après rechargement.
```sh
python3 appstore/scripts/asc_sync.py --credentials-env /chemin/local/release.env --action inspect
python3 appstore/scripts/asc_sync.py --credentials-env /chemin/local/release.env --action sync --screenshots
python3 appstore/scripts/asc_sync.py --credentials-env /chemin/local/release.env --action verify --screenshots
```

La synchronisation cible uniquement Podbook `6833936787`, version `0.1.0`,
dans l’état de préparation avant soumission. Elle complète les noms,
sous-titres, liens de confidentialité et métadonnées des deux plateformes.
Les images utilisent la nouvelle **App Asset Library** : spécifications lues
à chaque exécution, réservation, transfert, traitement, placement par langue
et appareil, puis ordre bibliothèque/lecture/voix vérifié. Les noms de fichiers
incluent une empreinte et les ressources existantes sont réutilisées lors
d’une reprise. Aucun build, soumission, publication ou accord n’est envoyé.
Le résultat vérifié est écrit dans `build/store-capture/api-verification.json`.

Le nom anglais choisi, **Podbook – Books & Audio**, est enregistré dans les
deux variantes anglaises et dans la fiche italienne. Le qualificatif brésilien
est **Podbook – Livros e áudio**, car Apple refuse aussi le nom court dans cette
langue. Le nom de marque du site reste Podbook.

Le contrôle API du 12 novembre 2026 a vérifié les informations communes des
24 localisations Apple (dont deux variantes anglaises), les 48 fiches de
version iOS/macOS et 216 placements de captures : trois images dans chacun
des 72 groupes langue/appareil. Les 207 images distinctes ont terminé leur
traitement chez Apple ; les deux variantes anglaises partagent leurs images.
Le bilan sans erreur est conservé dans `publication-receipt.json`.
L’app reste en préparation avant soumission.

Références Apple : [images](https://developer.apple.com/documentation/appstoreconnectapi/uploading-and-managing-image-assets),
[placements](https://developer.apple.com/documentation/appstoreconnectapi/placing-assets-on-your-app-store-surfaces),
[spécifications](https://developer.apple.com/documentation/appstoreconnectapi/discovering-asset-specifications).

`scripts/store-browser.mjs` conserve l’ancien chemin par contrôles visibles
CUA, mais son journal `upload-status.json` ne fait pas foi : l’API a montré
que certains enregistrements effectués par l’interface n’avaient pas persisté.


Full-resolution JPEG encoding can be optimized once after capture using
@@ -99,3 +135,7 @@ as the fixture generator. It retains all pixel dimensions and opaque RGB,
updates provenance hashes, and skips captures already optimized. The 216 images
occupy 47.4 MB. `website/scripts/check-pages-size.mjs` validates the uncompressed
Pages output against a 95 MB budget before deployment.

`python3 website/scripts/verify-pages.py` vérifie les 24 présentations publiées
et les empreintes des 216 images servies par GitLab Pages. Le résultat est
conservé localement dans `build/store-capture/pages-verification.json`.
+2 −1
Changes for appstore/localizations/pt-BR.json: 2 added lines, 1 removed line.
Original line number Diff line number Diff line
@@ -40,5 +40,6 @@
  },
  "marketingUrl": "https://kourser.pages.git.cythin.eu/podbook/pt-BR/presentation/",
  "supportUrl": "https://kourser.pages.git.cythin.eu/podbook/pt-BR/reference/depannage/",
  "privacyPolicyUrl": "https://kourser.pages.git.cythin.eu/podbook/pt-BR/reference/confidentialite/"
  "privacyPolicyUrl": "https://kourser.pages.git.cythin.eu/podbook/pt-BR/reference/confidentialite/",
  "appStoreName": "Podbook – Livros e áudio"
}
+51 −0
Changes for appstore/publication-receipt.json: 51 added lines, 0 removed lines.
Original line number Diff line number Diff line
{
  "appId": "6833936787",
  "version": "0.1.0",
  "verifiedAt": "2026-11-12T18:32:39Z",
  "state": "PREPARE_FOR_SUBMISSION",
  "commonInfoLocalizations": 24,
  "versionLocalizations": 48,
  "appStoreLanguages": 23,
  "englishStoreVariants": [
    "en-US",
    "en-GB"
  ],
  "uniqueAppleImages": 207,
  "screenshotPlacements": 216,
  "screenshotGroups": 72,
  "screenshotsPerGroup": 3,
  "website": {
    "baseUrl": "https://kourser.pages.git.cythin.eu/podbook/",
    "verifiedAt": "2026-11-12T18:24:42Z",
    "presentationLocales": [
      "fr",
      "en",
      "es",
      "it",
      "de",
      "pt",
      "pl",
      "nl",
      "cs",
      "sv",
      "da",
      "fi",
      "el",
      "hu",
      "ro",
      "sk",
      "sl",
      "hr",
      "bg",
      "zh-Hans",
      "ja",
      "ko",
      "ru",
      "pt-BR"
    ],
    "verifiedScreenshotCount": 216
  },
  "websiteOnlyLanguage": "bg",
  "issues": [],
  "submissionPerformed": false
}
+293 −0
Changes for appstore/scripts/asc_sync.py: 293 added lines, 0 removed lines.
Original line number Diff line number Diff line
#!/usr/bin/env python3
"""Synchronize Podbook's draft listing using the official App Store Connect API.

Requires cryptography. Credentials are read from ASC_* environment variables
or --credentials-env; private keys and bearer tokens are never persisted.
Does not upload builds, submit for review, publish, or accept agreements.
"""
import argparse
import base64
import hashlib
import json
import os
from pathlib import Path
import shlex
import time
import urllib.error
import urllib.parse
import urllib.request
import http.client
from concurrent.futures import ThreadPoolExecutor, as_completed
from cryptography.hazmat.primitives import hashes, serialization
from cryptography.hazmat.primitives.asymmetric import ec, utils

ROOT = Path(__file__).resolve().parents[2]
APP = '6833936787'
BASE = 'https://api.appstoreconnect.apple.com'

def credentials(path):
    values = dict(os.environ)
    if path:
        for line in Path(path).expanduser().read_text().splitlines():
            if '=' not in line or line.lstrip().startswith('#'):
                continue
            key, value = line.split('=', 1)
            parts = shlex.split(value, comments=True)
            values[key.strip()] = parts[0] if parts else ''
    return values

def encoded(value):
    return base64.urlsafe_b64encode(value).rstrip(b'=')

class API:
    def __init__(self, config):
        self.config = config
        self.expires = 0

    def token(self):
        if time.time() < self.expires - 30:
            return self.bearer
        self.expires = int(time.time()) + 1100
        kid = self.config['ASC_DELIVER_KEY_ID']
        header = encoded(json.dumps({'alg':'ES256','kid':kid,'typ':'JWT'}).encode())
        payload = encoded(json.dumps({'iss':self.config['ASC_ISSUER_ID'], 'iat':int(time.time()), 'exp':self.expires, 'aud':'appstoreconnect-v1'}).encode())
        body = header + b'.' + payload
        key = serialization.load_pem_private_key(Path(self.config['ASC_DELIVER_KEY_PATH']).expanduser().read_bytes(), password=None)
        r, s = utils.decode_dss_signature(key.sign(body, ec.ECDSA(hashes.SHA256())))
        self.bearer = (body + b'.' + encoded(r.to_bytes(32,'big')+s.to_bytes(32,'big'))).decode()
        return self.bearer

    def call(self, path, method='GET', data=None):
        if path.startswith('https://'):
            assert urllib.parse.urlparse(path).netloc == 'api.appstoreconnect.apple.com'
        else:
            path = BASE + path
        body = json.dumps(data).encode() if data is not None else None
        request = urllib.request.Request(path, body, {'Authorization':'Bearer '+self.token(), 'Content-Type':'application/json'}, method=method)
        for attempt in range(4):
            try:
                with urllib.request.urlopen(request, timeout=30) as response:
                    result = response.read()
                    return json.loads(result) if result else {}
            except urllib.error.HTTPError as error:
                if method in ['GET','PATCH'] and error.code in [429,500,502,503,504] and attempt < 3:
                    time.sleep(2**attempt)
                    continue
                details = json.loads(error.read())
                raise RuntimeError(f'{method} {urllib.parse.urlparse(path).path}: HTTP {error.code}: '+json.dumps(details.get('errors',details),ensure_ascii=False)) from None
            except (urllib.error.URLError,TimeoutError,http.client.RemoteDisconnected):
                if method not in ['GET','PATCH'] or attempt == 3: raise
                time.sleep(2**attempt)

    def all(self, path):
        result = []
        while path:
            response = self.call(path)
            result.extend(response['data'])
            path = response.get('links',{}).get('next')
        return result

    def update(self, resource, attributes):
        changes = {key:value for key,value in attributes.items() if resource['attributes'].get(key) != value}
        if changes:
            return self.call(f'/v1/{resource["type"]}/{resource["id"]}', 'PATCH', {'data':{'type':resource['type'],'id':resource['id'],'attributes':changes}})['data']
        return resource

    def create(self, kind, attributes, relationships):
        return self.call('/v1/'+kind, 'POST', {'data':{'type':kind,'attributes':attributes,'relationships':{name:{'data':value} for name,value in relationships.items()}}})['data']

def relation(kind, identifier):
    return {'type':kind,'id':identifier}

def catalogues():
    return [json.loads(path.read_text()) for path in sorted((ROOT/'appstore/localizations').glob('*.json')) if path.name != 'manifest.json' and path.stem != 'bg']

def version_fields(catalog, platform):
    source = catalog['platforms'][platform]
    return {key:source[key] for key in ['description','promotionalText','keywords','supportUrl','marketingUrl']}

def info_fields(catalog):
    return {'name':catalog.get('appStoreName',catalog['name']), 'subtitle':catalog['subtitle'], 'privacyPolicyUrl':catalog['privacyPolicyUrl']}

def sync_metadata(api, versions, info, verify):
    issues, targets = [], []
    common = {item['attributes']['locale']:item for item in api.all(f'/v1/appInfos/{info["id"]}/appInfoLocalizations?limit=200')}
    for catalog in catalogues():
        locale = catalog['appStoreLocale']
        try:
            fields = info_fields(catalog)
            if locale not in common:
                if verify: raise RuntimeError('Missing app info '+locale)
                common[locale] = api.create('appInfoLocalizations', {'locale':locale,**fields}, {'appInfo':relation('appInfos',info['id'])})
            elif not verify and any(common[locale]['attributes'].get(k)!=v for k,v in fields.items()):
                api.update(common[locale], fields)
                common[locale] = api.call('/v1/appInfoLocalizations/'+common[locale]['id'])['data']
            persisted = common[locale]
            assert all(persisted['attributes'].get(k)==v for k,v in fields.items()), 'App info mismatch '+locale
        except (RuntimeError,AssertionError) as error:
            issues.append(str(error)); print('INFO ERROR '+locale+' '+str(error),flush=True)
    for version in versions:
        assert version['attributes']['versionString']=='0.1.0'
        assert version['attributes']['appStoreState']=='PREPARE_FOR_SUBMISSION'
        platform = {'IOS':'ios','MAC_OS':'macos'}[version['attributes']['platform']]
        existing = {item['attributes']['locale']:item for item in api.all(f'/v1/appStoreVersions/{version["id"]}/appStoreVersionLocalizations?limit=200')}
        for catalog in catalogues():
            locale = catalog['appStoreLocale']
            fields = version_fields(catalog, platform)
            if locale not in existing:
                if verify: raise RuntimeError('Missing version localization '+platform+'/'+locale)
                existing[locale] = api.create('appStoreVersionLocalizations', {'locale':locale,**fields}, {'appStoreVersion':relation('appStoreVersions',version['id'])})
            elif not verify and any(existing[locale]['attributes'].get(k)!=v for k,v in fields.items()):
                api.update(existing[locale], fields)
                existing[locale] = api.call('/v1/appStoreVersionLocalizations/'+existing[locale]['id'])['data']
            persisted = existing[locale]
            assert all(persisted['attributes'].get(k)==v for k,v in fields.items()), 'Version mismatch '+platform+'/'+locale
            targets.append((catalog,platform,persisted))
            print('VERIFIED metadata '+platform+'/'+locale,flush=True)
    return targets, issues

def image_groups(api):
    reference = api.call('/v1/appAssetLibraryRefData')['data'][0]['attributes']
    placement = next(x for x in reference['placementTypes'] if x['placementTypeId']=='APP_SCREENSHOT')
    assert 'APP_SCREENSHOTS_AND_PREVIEWS' in placement['acceptsAssetCategories']
    result = {}
    for device, size in {'iphone':(1206,2622),'ipad':(2064,2752),'mac':(2560,1600)}.items():
        spec = next(x for x in reference['imageSpecs'] if (x['dimensions']['minWidth'],x['dimensions']['minHeight'])==size and 'APP_SCREENSHOT' in x['compatiblePlacementTypes'])
        choices = [x['placementGroupId'] for x in placement['specMappings'] if spec['specId'] in x['specs']]
        assert len(choices)==1, (device,choices)
        result[device] = (choices[0],spec['specId'])
    print('Verified live Apple specifications: '+json.dumps(result),flush=True)
    return result

def upload_image(api, path, existing, library):
    checksum = hashlib.sha256(path.read_bytes()).hexdigest()
    filename = 'Podbook_'+path.parent.parent.name+'_'+path.parent.name+'_'+path.stem+'_'+checksum[:12]+'.jpg'
    matches = [item for item in existing if item['attributes']['fileName']==filename]
    if matches:
        image = matches[0]
    else:
        image = api.create('appAssetLibraryImages', {'fileName':filename,'fileSize':path.stat().st_size,'category':'APP_SCREENSHOTS_AND_PREVIEWS','referenceName':filename}, {'assetLibrary':relation('appAssetLibraries',library)})
    attrs = image['attributes']
    if attrs['state'] in ['PREPARE_FOR_SUBMISSION','APPROVED']:
        return image  # The freshly fetched library already confirms processing.
    if attrs['state']=='AWAITING_UPLOAD':
        content = path.read_bytes()
        for operation in attrs['uploadOperations']:
            url = urllib.parse.urlparse(operation['url'])
            assert url.scheme=='https' and url.hostname.endswith('.apple.com'), 'Unexpected upload destination'
            headers = {header['name']:header['value'] for header in operation['requestHeaders']}
            part = content[operation['offset']:operation['offset']+operation['length']]
            request = urllib.request.Request(operation['url'],part,headers,method=operation['method'])
            try:
                with urllib.request.urlopen(request,timeout=60) as response: response.read()
            except urllib.error.HTTPError as error:
                raise RuntimeError('Apple image transfer failed: HTTP '+str(error.code)) from None
        image = api.update(image, {'uploaded':True})
    for attempt in range(45):
        image = api.call('/v1/appAssetLibraryImages/'+image['id'])['data']
        state = image['attributes']['state']
        if state in ['PREPARE_FOR_SUBMISSION','APPROVED']: return image
        if state=='FAILED': raise RuntimeError('Image processing failed '+filename+': '+json.dumps(image['attributes'].get('stateDetails')))
        time.sleep(2)
    raise RuntimeError('Image processing still pending '+filename)

def sync_screenshots(api, targets, verify, workers):
    groups = image_groups(api)
    library = api.call(f'/v1/apps/{APP}/assetLibrary')['data']['id']
    existing = api.all(f'/v1/appAssetLibraries/{library}/images?limit=200')
    paths = {ROOT/f'website/static/img/screenshots/{catalog["appLocale"]}/{device}/{scene}.jpg'
             for catalog,platform,_ in targets for device in (['mac'] if platform=='macos' else ['iphone','ipad'])
             for scene in ['01-library','02-reader','03-settings']}
    images, issues = {}, []
    if not verify:
        with ThreadPoolExecutor(max_workers=workers) as pool:
            futures = {pool.submit(upload_image,api,path,existing,library):path for path in sorted(paths)}
            for future in as_completed(futures):
                path = futures[future]
                try:
                    images[path] = future.result()
                    print(f'IMAGE {len(images)}/{len(paths)} '+str(path.relative_to(ROOT)),flush=True)
                except Exception as error:
                    issues.append(str(error)); print('IMAGE ERROR '+str(error),flush=True)
    else:
        for path in paths:
            checksum = hashlib.sha256(path.read_bytes()).hexdigest()
            filename = 'Podbook_'+path.parent.parent.name+'_'+path.parent.name+'_'+path.stem+'_'+checksum[:12]+'.jpg'
            image = next((x for x in existing if x['attributes']['fileName']==filename),None)
            assert image and image['attributes']['state'] in ['PREPARE_FOR_SUBMISSION','APPROVED'], 'Missing processed image '+filename
            images[path] = image
    if issues: return [],issues
    def place_target(target):
        catalog,platform,localization = target
        receipts = []
        endpoint = f'/v1/appStoreVersionLocalizations/{localization["id"]}/placements'
        placements = api.all(endpoint+'?filter[placementType]=APP_SCREENSHOT&include=image&limit=200')
        for device in (['mac'] if platform=='macos' else ['iphone','ipad']):
            group,spec = groups[device]
            ordered = []
            for scene in ['01-library','02-reader','03-settings']:
                path = ROOT/f'website/static/img/screenshots/{catalog["appLocale"]}/{device}/{scene}.jpg'
                image = images[path]
                assert image['attributes']['specId']==spec, 'Wrong processed image specification'
                found = [x for x in placements if x['attributes']['placementGroup']==group and x['relationships'].get('image',{}).get('data',{}).get('id')==image['id']]
                if found:
                    placement = found[0]
                elif not verify:
                    placement = api.create('appAssetLibraryPlacements', {'placementType':'APP_SCREENSHOT','placementGroup':group}, {'image':relation('appAssetLibraryImages',image['id']), 'appStoreVersionLocalization':relation('appStoreVersionLocalizations',localization['id'])})
                else:
                    raise RuntimeError('Missing placement '+catalog['appStoreLocale']+'/'+device+'/'+scene)
                ordered.append(placement['id'])
            if not verify:
                api.create('appAssetLibraryPlacementOrderingRequests', {'placementGroup':group}, {'orderedPlacements':[relation('appAssetLibraryPlacements',identifier) for identifier in ordered], 'appStoreVersionLocalization':relation('appStoreVersionLocalizations',localization['id'])})
            actual = api.all(endpoint+'?'+urllib.parse.urlencode({'filter[placementType]':'APP_SCREENSHOT','filter[placementGroup]':group,'sort':'placementGroupPosition','limit':200}))
            assert [x['id'] for x in actual]==ordered, 'Unexpected screenshot count/order'
            # The live API returns ACTIVE for placements on draft versions;
            # some API documentation also names PARENT_PREPARE_FOR_SUBMISSION.
            assert all(x['attributes']['state'] in ['ACTIVE','PARENT_PREPARE_FOR_SUBMISSION'] for x in actual), 'Placement processing incomplete'
            receipts.append({'locale':catalog['appStoreLocale'],'sourceAppLocale':catalog['appLocale'],'platform':platform,'device':device,'count':len(actual),'placementIds':ordered})
            print('VERIFIED screenshots '+platform+'/'+catalog['appStoreLocale']+'/'+device,flush=True)
        return receipts
    receipts = []
    with ThreadPoolExecutor(max_workers=workers) as pool:
        futures = [pool.submit(place_target,target) for target in targets]
        for future in as_completed(futures):
            receipts.extend(future.result())
    receipts.sort(key=lambda item:(item['platform'],item['locale'],item['device']))
    return receipts,issues

def main():
    parser = argparse.ArgumentParser(description=__doc__)
    parser.add_argument('--credentials-env')
    parser.add_argument('--action', choices=['inspect','sync','verify'], default='inspect')
    parser.add_argument('--screenshots', action='store_true')
    parser.add_argument('--workers',type=int,default=4)
    options = parser.parse_args()
    api = API(credentials(options.credentials_env))
    app = api.call('/v1/apps/'+APP)['data']
    assert app['attributes']['bundleId'] == 'eu.cythin.podbook'
    versions = api.all(f'/v1/apps/{APP}/appStoreVersions?limit=200')
    infos = api.all(f'/v1/apps/{APP}/appInfos?limit=200')
    if options.action != 'inspect':
        info = next(x for x in infos if x['attributes']['appStoreState']=='PREPARE_FOR_SUBMISSION')
        targets, issues = sync_metadata(api, versions, info, options.action=='verify')
        receipts = []
        if options.screenshots:
            receipts, image_issues = sync_screenshots(api, targets, options.action=='verify', options.workers)
            issues.extend(image_issues)
        receipt = {'appId':APP,'version':'0.1.0','verifiedAt':time.strftime('%Y-%m-%dT%H:%M:%SZ',time.gmtime()),'metadataLocalizations':len(targets),'screenshots':receipts,'issues':issues,'submissionPerformed':False}
        output = ROOT/'build/store-capture/api-verification.json'
        output.parent.mkdir(parents=True,exist_ok=True)
        output.write_text(json.dumps(receipt,ensure_ascii=False,indent=2)+'\n')
        print('RESULT '+json.dumps({'metadataLocalizations':len(targets),'screenshotGroups':len(receipts),'screenshots':sum(x['count'] for x in receipts),'issues':len(issues)}),flush=True)
        if issues: raise SystemExit(1)
        return
    for version in versions:
        attrs = version['attributes']
        localizations = api.all(f'/v1/appStoreVersions/{version["id"]}/appStoreVersionLocalizations?limit=200')
        print(json.dumps({'platform':attrs['platform'],'version':attrs['versionString'],'state':attrs['appStoreState'],'id':version['id'],'locales':[{k:item['attributes'].get(k) for k in ['locale']}|{'id':item['id']} for item in localizations]}, ensure_ascii=False),flush=True)
    for info in infos:
        locales = api.all(f'/v1/appInfos/{info["id"]}/appInfoLocalizations?limit=200')
        print(json.dumps({'appInfo':info['id'],'state':info['attributes'].get('appStoreState'),'localizations':[{'id':x['id'],**{k:x['attributes'].get(k) for k in ['locale','name','subtitle']}} for x in locales]},ensure_ascii=False),flush=True)

if __name__ == '__main__':
    main()
+49 −0

File added.

Preview size limit exceeded, changes collapsed.