ls0f/phone

repository·master·Indexed 22 days ago

https://github.com/ls0f/phone

A Python library and data provider for looking up Chinese mobile phone number information, including province, city, postcode, area code, and carrier type. It provides a Phone class for lookups and a binary data file (phone.dat) with a defined structure for use in other languages such as Lua, Go, Node.js, and PHP.

Tokens
745
Snippets
3
Records
4
Agent score
28%

What's inside ls0f-phone

  1. Use the Phone class to find phone number information

    master

    To look up information for a phone number, import the Phone class, instantiate it, and use the .find() method. The method accepts a phone number (e.g., an integer) and returns the associated data.

    from phone import Phone
    p  = Phone()
    p.find(1888888)
  2. Reference the phone.dat file format

    master

    The phone.dat file is a binary format structured as follows:

    File Structure

    SectionSizeDescription
    Header8 bytes4 bytes for version, 4 bytes for the offset of the first index
    Record AreaVariableContains the actual location data
    Index AreaVariableContains the lookup indices

    Data Formats

    Record Area

    Each record is a null-terminated string in the format: <Province>|<City>|<Postcode>|<Long-distance Area Code>\0

    Index Area

    Each index entry is exactly 9 bytes (<iiB format): <First 7 digits of phone number><Offset to Record Area><Card Type>

    Card Type Mapping

    When parsing the index, the Card Type byte corresponds to:

    • 1: China Mobile (移动)
    • 2: China Unicom (联通)
    • 3: China Telecom (电信)
    • 4: Telecom Virtual Operator (电信虚拟运营商)
    • 5: Unicom Virtual Operator (联通虚拟运营商)
    • 6: Mobile Virtual Operator (移动虚拟运营商)

    Parsing Logic

    1. Parse the 8-byte header to find the offset of the first index in the Index Area.
    2. Perform a binary search in the Index Area using the first 7 digits of the phone number to find the corresponding offset in the Record Area.
    3. Navigate to the offset in the Record Area and read the data until the null terminator (\0) is encountered.
            | 4 bytes |                     <- phone.dat version
            ------------
            | 4 bytes |                     <- Offset to the first index
            -----------------------
            |  offset - 8            |      <- Record Area
            -----------------------
            |  index                 |      <- Index Area