Skip to content

Commit

Permalink
add mcc table
Browse files Browse the repository at this point in the history
  • Loading branch information
septs committed Jan 12, 2025
1 parent 0ae9ab4 commit 8024b5f
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 2 deletions.
33 changes: 33 additions & 0 deletions carriers/mcc-table.schema.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
{
"$schema": "http://json-schema.org/draft-07/schema#",
"title": "MCC Table",
"type": "array",
"items": {
"$ref": "#/definitions/entry"
},
"definitions": {
"entry": {
"type": "object",
"properties": {
"mcc": {
"type": "string",
"description": "The Mobile Country Code"
},
"iso": {
"type": "string",
"description": "The ISO country code associated with the MCC code"
},
"smallestDigitsMCC": {
"type": "number",
"description": "The smallest number of digits in the MNC code"
}
},
"additionalProperties": false,
"required": [
"mcc",
"iso",
"smallestDigitsMCC"
]
}
}
}
4 changes: 3 additions & 1 deletion download.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from requests import Session
from requests_cache import CachedSession

from handlers import google, mccmnc_dot_net, mccmnc_dot_com
from handlers import google, mccmnc_dot_net, mccmnc_dot_com, mcc_table
from handlers.database import db
from handlers.rebuild import rebuild_carriers, export_carriers
from handlers.utils import CustomEncoder, emit_file
Expand All @@ -34,6 +34,8 @@ def main():
save_file(list(export_carriers()), name="unified")
print("Downloading from Google")
save_file(list(google.fetch(session)), name="google")
save_file(list(mcc_table.fetch(session)), name="mcc-table")



def save_file(carriers: list[dict[str, str]], name: str):
Expand Down
3 changes: 2 additions & 1 deletion handlers/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
__all__ = [
"google",
"mccmnc_dot_net",
"mccmnc_dot_com"
"mccmnc_dot_com",
"mcc_table",
]
32 changes: 32 additions & 0 deletions handlers/mcc_table.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import base64
import os
import re
from urllib.parse import urljoin

import requests

BASE_URL = "https://android.googlesource.com/platform/frameworks/opt/telephony/"
BRANCH = "HEAD"
FILEPATH = "src/java/com/android/internal/telephony/MccTable.java"

RE_MCC_ENTRY = re.compile(r"new MccEntry\((?P<mcc>\d+), \"(?P<iso>\w+)\", (?P<n>\d+)\)")


def fetch(session: requests.Session):
response = session.get(
url=urljoin(BASE_URL, os.path.join("+", BRANCH, FILEPATH)),
params={"format": "TEXT"},
)
response.raise_for_status()
codeblock = base64.decodebytes(response.content).decode("utf-8")

entries = [
{
"mcc": entry.group("mcc"),
"iso": entry.group("iso").upper(),
"smallestDigitsMCC": int(entry.group("n")),
}
for entry in RE_MCC_ENTRY.finditer(codeblock)
]

return sorted(entries, key=lambda _: int(_["mcc"]))
5 changes: 5 additions & 0 deletions npm/carriers/mcc-table.json.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import type {google} from "./types";

declare const entries: readonly google.MCCEntry[];

export = entries;
6 changes: 6 additions & 0 deletions npm/carriers/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,10 @@ export namespace google {
readonly iccid_prefix?: readonly string[];
readonly privilege_access_rule?: readonly string[];
}

export interface MCCEntry {
readonly mcc: string;
readonly iso: string;
readonly smallestDigitsMCC: number;
}
}

0 comments on commit 8024b5f

Please sign in to comment.