phoneloc

package module
v0.0.0-...-7099d5c Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 4, 2021 License: MIT Imports: 5 Imported by: 0

README

PhoneLoc-手机归属地查询

将46w+的手机号段归属地数据编码到一个不到2MB的二进制文件中,并利用golang提供极致的查询性能。

数据文件结构

  • 版本信息 100byte
  • 号段映射区 100byte
  • 数据块记录区 46100003byte

数据文件思路

充分利用手机号段的特性,将数据按照索引位置存储。查询时直接按照偏移量定位即可。看一下计算方法就能明白:

# 以1891508为例
hexdump -Cv -n1 -s189 phone.dat # 读取号段索引
# 计算记录区偏移量 200+(49-1)*3*10000+1508*3 = 1444724
hexdump -Cv -n3 -s1444724 phone.dat # 读取记录区数据 即可得到归属地的adcode
# 00160b74  90 e3 84    得到的数据为0x84e390  最高2位表示运营商(0b00其他 0b01移动 0b10电信 0b11联通)
# 8最高两位0b10也就是电信 最高两位置0后得到归属城市的adcode 0x04e390(10进制320400,也就是常州)

Benchmarks

go test -v -run="none" -bench=. -benchmem -benchtime=1s
goos: darwin
goarch: amd64
pkg: github.com/yzchan/phoneloc
cpu: Intel(R) Core(TM) i5-7360U CPU @ 2.30GHz
BenchmarkFind
BenchmarkFind-4         10484691               116.0 ns/op           112 B/op          1 allocs/op
PASS
ok      github.com/yzchan/phoneloc      1.266s

Features

  • 地址编码
  • 增加邮编和城市区号

Thinks

Documentation

Index

Constants

View Source
const (
	Province int = 1
	City     int = 2
)

Variables

View Source
var DistrictMapping = map[int]District{}/* 377 elements not displayed */

Functions

This section is empty.

Types

type District

type District struct {
	Level    int
	AdCode   int
	Name     string
	FullName string
	TelCode  string
	Postcode string
}

func (*District) GetProvince

func (d *District) GetProvince() *District

func (*District) IsCity

func (d *District) IsCity() bool

func (*District) IsDirectCity

func (d *District) IsDirectCity() bool

func (*District) IsProvince

func (d *District) IsProvince() bool

type Parser

type Parser struct {
	// contains filtered or unexported fields
}

func NewParser

func NewParser(file string) (p *Parser, err error)

func (*Parser) Find

func (p *Parser) Find(sec int) (loc *PhoneLoc, err error)

func (*Parser) Macs

func (p *Parser) Macs() (ret []int)

func (*Parser) Version

func (p *Parser) Version() string

type PhoneLoc

type PhoneLoc struct {
	Section  int    `json:"section"`  // 号段
	Prov     string `json:"prov"`     // 归属省
	City     string `json:"city"`     // 归属城市
	Adcode   int    `json:"adcode"`   // 归属城市的行政区划代码
	Postcode string `json:"postcode"` // 邮编
	TelCode  string `json:"telcode"`  // 电话区号
	Sp       string `json:"sp"`       // 运营商
	Virtual  bool   `json:"virtual"`  // 是否虚拟号段
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL