Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Canada DMA targeting #8

Open
wants to merge 1 commit into from

1 participant

@doubleyou
Owner

See this: http://en.wikipedia.org/wiki/List_of_television_stations_in_North_America_by_media_market

This is an incomplete list, actually. I could find a handful of IPs that don't match any of the listed cities. Moreover, sometimes egeoip:lookup/1 returns just coordinates, without a city name or zip code.

Anyway, that's much better than nothing, allows us to target the majority of the country. In case of non-existing city we just return 0 as a DMA code.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
Showing with 110 additions and 5 deletions.
  1. +10 −5 src/egeoip.erl
  2. +86 −0 src/egeoip_ca_dma.erl
  3. +14 −0 test/egeoip_tests.erl
View
15 src/egeoip.erl
@@ -386,21 +386,26 @@ get_record(D=#geoipdb{record_length = Length,
<<_:Seek3/binary, RawLat:24/little, RawLon:24/little, _/binary>> = Data,
Lat = (RawLat / 10000) - 180,
Lon = (RawLon / 10000) - 180,
- {DmaCode, AreaCode} = get_record_ex(Type, Country, Data, Seek3 + 6),
- #geoip{country_code = Country,
+ GeoIP = #geoip{country_code = Country,
country_code3 = Country3,
country_name = CountryName,
region = Region,
city = City,
postal_code = Postal,
latitude = Lat,
- longitude = Lon,
+ longitude = Lon
+ },
+ {DmaCode, AreaCode} = get_record_ex(Type, GeoIP, Data, Seek3 + 6),
+ GeoIP#geoip{
dma_code = DmaCode,
- area_code = AreaCode}.
+ area_code = AreaCode
+ }.
-get_record_ex(?GEOIP_CITY_EDITION_REV1, "US", Data, Seek) ->
+get_record_ex(?GEOIP_CITY_EDITION_REV1, #geoip{country_code="US"}, Data,Seek) ->
<<_:Seek/binary, Combo:24/little, _/binary>> = Data,
{Combo div 1000, Combo rem 1000};
+get_record_ex(_Type, #geoip{country_code="CA", city=City}, _Data, _Seek) ->
+ {egeoip_ca_dma:dma_code(City), 0};
get_record_ex(_, _, _, _) ->
{0, 0}.
View
86 src/egeoip_ca_dma.erl
@@ -0,0 +1,86 @@
+-module(egeoip_ca_dma).
+
+-export([dma_code/1]).
+
+dma_code(City) ->
+ case dict:find(City, all()) of
+ {ok, [DMA]} -> DMA;
+ error -> 0
+ end.
+
+all() ->
+{dict,75,16,16,8,80,48,
+ {[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[]},
+ {{[[<<"Fredericton">>,12],
+ [<<"Rouyn-Noranda">>,31],
+ [<<"Levis">>,7],
+ [<<"Wingham">>,10],
+ [<<"Kelowna">>,15],
+ [<<"Peterborough">>,24]],
+ [[<<"Brandon">>,8],
+ [<<"Thunder Bay">>,30],
+ [<<"Terrace">>,35],
+ [<<"Laval">>,2],
+ [<<"Swift Current">>,42],
+ [<<"Orillia">>,18]],
+ [[<<"London">>,10],
+ [<<"Ottawa">>,4],
+ [<<"Belleville">>,24],
+ [<<"Carleton">>,32],
+ [<<"Matane">>,25]],
+ [[<<"Rivière-du-Loup">>,29]],
+ [[<<"Toronto">>,1],
+ [<<"Niagara Falls">>,1],
+ [<<"St. John's">>,17],
+ [<<"Kitimat">>,35]],
+ [[<<"Cambridge">>,9],
+ [<<"Vancouver-Victoria">>,3],
+ [<<"Shawinigan">>,20],
+ [<<"Sault Sainte Marie">>,37]],
+ [[<<"Halifax">>,11],
+ [<<"Trois-Rivières">>,20],
+ [<<"Montreal">>,2],
+ [<<"Petawawa">>,36],
+ [<<"Val-d'Or">>,31],
+ [<<"Prince Rupert">>,35]],
+ [[<<"Guelph">>,9],
+ [<<"Kingston">>,23],
+ [<<"Moncton">>,12],
+ [<<"Sudbury-Timmins-North Bay">>,16]],
+ [[<<"Moose Jaw">>,21],
+ [<<"Lethbridge">>,6],
+ [<<"Corner Brook">>,17],
+ [<<"Prince Albert">>,34]],
+ [[<<"Medicine Hat">>,39],
+ [<<"Barrie">>,18],
+ [<<"Saguenay">>,22],
+ [<<"Dartmouth">>,11],
+ [<<"Glace Bay">>,26]],
+ [[<<"Okanagan Valley">>,15],
+ [<<"Edmonton">>,5],
+ [<<"St. John">>,12],
+ [<<"Sydney">>,26],
+ [<<"Lloydminster">>,40],
+ [<<"Calgary">>,6],
+ [<<"Regina">>,21],
+ [<<"Campbellton">>,25]],
+ [[<<"Kitchener-Waterloo">>,9],
+ [<<"Gaspé">>,25],
+ [<<"Winnipeg">>,8],
+ [<<"Quebec City">>,7],
+ [<<"Prince George">>,38],
+ [<<"Charlottetown">>,28]],
+ [[<<"Hamilton">>,1],
+ [<<"Kamloops">>,15],
+ [<<"Dawson Creek">>,41],
+ [<<"Kenora">>,43]],
+ [[<<"Pembroke">>,36],
+ [<<"Windsor">>,13],
+ [<<"Red Deer">>,27],
+ [<<"Rimouski">>,25],
+ [<<"Bathurst">>,25]],
+ [[<<"Woodstock">>,10],
+ [<<"Sherbrooke">>,14],
+ [<<"Percé">>,25],
+ [<<"Saskatoon">>,19]],
+ [[<<"Trenton">>,24],[<<"Chaleur Bay">>,32],[<<"Yorkton">>,33]]}}}.
View
14 test/egeoip_tests.erl
@@ -13,6 +13,7 @@ run_test_() ->
{"egeoip_reserved", {generator, fun egeoip_reserved_gen/0}},
{"country_test", {generator, fun country_test_gen/0}},
{"country_test2", {generator, fun country_test2_gen/0}},
+ {"Canada test", fun canada/0},
{"non_parallel", fun non_parallel/0}]}}.
egeoip_reserved_gen() ->
@@ -196,3 +197,16 @@ country_test_data() ->
{"12.12.197.23", "US", "USA"},
{"12.12.199.3", "US", "USA"},
{"12.12.200.79", "US", "USA"}].
+
+canada() ->
+ ?assertMatch(
+ {ok, #geoip{country_code = "CA",
+ country_code3 = "CAN",
+ country_name = "Canada",
+ region = <<"ON">>,
+ city = <<"Toronto">>,
+ postal_code = <<"m5j2n1">>,
+ area_code = 0,
+ dma_code = 1}},
+ egeoip:lookup("209.15.233.59")
+ ).
Something went wrong with that request. Please try again.