Last active
June 16, 2021 20:07
-
-
Save process/e55f623bf355f59b2615802115ffd4a5 to your computer and use it in GitHub Desktop.
Script to automatically ingest SKAN IDs from plist sources, and update xcode projects. Python3 with standard libs.
This file contains 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
# Written by Justin Chines (@process) and Doug Kent | |
import argparse, os, re, plistlib, urllib.request | |
parser = argparse.ArgumentParser(description="Update an app Plist with new SKAdNetwork IDs from given sources.") | |
parser.add_argument("plist", | |
help="Path to the .plist that will be updated with IDs found in the sources.") | |
parser.add_argument("sources", | |
nargs='+', | |
help="Sources to load SKAdNetwork IDs from. Sources can be URLs or system paths") | |
args = parser.parse_args() | |
# Use regex to extract adnetwork ids | |
def extract_skanids(data: str) -> [str]: | |
return re.findall("[a-z0-9]*\.skadnetwork", data) | |
skanids = set() | |
for source in args.sources: | |
print("Loading: " + source) | |
source = source.strip() | |
if not source: continue # skip newlines | |
if os.path.isfile(source): | |
with open(source, 'r') as f: | |
data = f.read() | |
skanids.update(extract_skanids(data)) | |
else: | |
req = urllib.request.Request(source, headers={"User-Agent": "Mozilla/5.0"}) | |
data = urllib.request.urlopen(req).read().decode("utf-8") | |
skanids.update(extract_skanids(data)) | |
with open(args.plist, 'rb') as plist_file: | |
plist = plistlib.load(plist_file) | |
old_skanids = set() | |
for item in plist['SKAdNetworkItems']: | |
old_skanids.add(item['SKAdNetworkIdentifier']) | |
to_remove = old_skanids.difference(skanids) | |
to_add = skanids.difference(old_skanids) | |
print(f"\nRemoving: {len(to_remove)} items") | |
for item in to_remove: | |
print(item) | |
print(f"\nAdding: {len(to_add)} items") | |
for item in to_add: | |
print(item) | |
if len(to_add) or len(to_remove): | |
print("\nSaving new plist") | |
plist_data = [{'SKAdNetworkIdentifier': adid} for adid in sorted(skanids)] | |
plist['SKAdNetworkItems'] = plist_data | |
with open(args.plist, 'wb') as plist_file: | |
plistlib.dump(plist, plist_file) | |
print(f"{len(skanids)} SKAdNetwork IDs were saved.") | |
else: | |
print("\nNothing to add or remove. Not updating plist.") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment