|
1 | 1 | -module(ip2location). |
2 | | --export([apiversion/0, getapiversion/0, new/1, query/1, close/0]). |
| 2 | +-export([apiversion/0, getapiversion/0, new/1, query/1, close/0, openws/3, lookup/3, getcredit/0]). |
3 | 3 | -record(ip2locationrecord, { |
4 | 4 | country_short = "-", |
5 | 5 | country_long = "-", |
|
27 | 27 | -define(IF(Cond), (case (Cond) of true -> (0); false -> (1) end)). |
28 | 28 |
|
29 | 29 | apiversion() -> |
30 | | - "8.3.0". |
| 30 | + "8.4.0". |
31 | 31 |
|
32 | 32 | getapiversion() -> |
33 | 33 | io:format("API Version: ~p~n", [apiversion()]). |
@@ -382,3 +382,136 @@ close() -> |
382 | 382 | _ -> |
383 | 383 | ets:delete(mymeta) |
384 | 384 | end. |
| 385 | + |
| 386 | +closews() -> |
| 387 | + case ets:info(myws) of |
| 388 | + undefined -> |
| 389 | + ok; |
| 390 | + _ -> |
| 391 | + ets:delete(myws), |
| 392 | + ok |
| 393 | + end. |
| 394 | + |
| 395 | +configurews(APIKey, APIPackage, UseSSL) -> |
| 396 | + _ = closews(), |
| 397 | + |
| 398 | + case ets:info(myws) of |
| 399 | + undefined -> |
| 400 | + ets:new(myws, [set, named_table]), |
| 401 | + ets:insert(myws, {apikey, APIKey}), |
| 402 | + ets:insert(myws, {apipackage, APIPackage}), |
| 403 | + ets:insert(myws, {usessl, UseSSL}), |
| 404 | + ok; |
| 405 | + _ -> |
| 406 | + ok |
| 407 | + end. |
| 408 | + |
| 409 | +checkparams(APIKey, APIPackage) -> |
| 410 | + RegExp = "^[\\dA-Z]{10}$", |
| 411 | + RegExp2 = "^WS\\d+$", |
| 412 | + case re:run(APIKey, RegExp) of |
| 413 | + {match, _} -> |
| 414 | + case re:run(APIPackage, RegExp2) of |
| 415 | + nomatch -> |
| 416 | + io:format("Invalid package name.~n", []), |
| 417 | + halt(); |
| 418 | + {match, _} -> |
| 419 | + ok % do nothing |
| 420 | + end; |
| 421 | + nomatch -> |
| 422 | + io:format("Invalid API key.~n", []), |
| 423 | + halt() |
| 424 | + end. |
| 425 | + |
| 426 | +openws(APIKey, APIPackage, UseSSL) -> |
| 427 | + case checkparams(APIKey, APIPackage) of |
| 428 | + ok -> |
| 429 | + case UseSSL of |
| 430 | + false -> |
| 431 | + configurews(APIKey, APIPackage, UseSSL); |
| 432 | + _ -> |
| 433 | + configurews(APIKey, APIPackage, true) |
| 434 | + end; |
| 435 | + _ -> |
| 436 | + -1 % should have been halted in checkparams |
| 437 | + end. |
| 438 | + |
| 439 | +lookup(IPAddress, AddOn, Lang) -> |
| 440 | + ssl:start(), |
| 441 | + inets:start(), |
| 442 | + |
| 443 | + case ets:info(myws) of |
| 444 | + undefined -> |
| 445 | + io:format("Run openws first.~n", []), |
| 446 | + halt(); |
| 447 | + _ -> |
| 448 | + case ets:lookup(myws, apikey) of |
| 449 | + [] -> |
| 450 | + io:format("Run openws first.~n", []), |
| 451 | + halt(); |
| 452 | + [{_, APIKey}] -> |
| 453 | + case ets:lookup(myws, apipackage) of |
| 454 | + [] -> |
| 455 | + io:format("Run openws first.~n", []), |
| 456 | + halt(); |
| 457 | + [{_, APIPackage}] -> |
| 458 | + case ets:lookup(myws, usessl) of |
| 459 | + [] -> |
| 460 | + io:format("Run openws first.~n", []), |
| 461 | + halt(); |
| 462 | + [{_, UseSSL}] -> |
| 463 | + case UseSSL of |
| 464 | + true -> |
| 465 | + Protocol = "https"; |
| 466 | + _ -> |
| 467 | + Protocol = "http" |
| 468 | + end, |
| 469 | + MyParams = uri_string:compose_query([{"key", APIKey}, {"package", APIPackage}, {"ip", IPAddress}, {"addon", AddOn}, {"lang", Lang}]), |
| 470 | + |
| 471 | + case httpc:request(get, {Protocol ++ "://api.ip2location.com/v2/?" ++ MyParams, []}, [{ssl, [{versions, ['tlsv1.2']}]}, {autoredirect, false}], []) of |
| 472 | + {ok, {{_, 200, _}, _, Body}} -> |
| 473 | + jiffy:decode(unicode:characters_to_binary(Body,unicode,utf8),[return_maps]); |
| 474 | + {error, Reason} -> |
| 475 | + {error, Reason} |
| 476 | + end |
| 477 | + end |
| 478 | + end |
| 479 | + end |
| 480 | + end. |
| 481 | + |
| 482 | +getcredit() -> |
| 483 | + ssl:start(), |
| 484 | + inets:start(), |
| 485 | + |
| 486 | + case ets:info(myws) of |
| 487 | + undefined -> |
| 488 | + io:format("Run openws first.~n", []), |
| 489 | + halt(); |
| 490 | + _ -> |
| 491 | + case ets:lookup(myws, apikey) of |
| 492 | + [] -> |
| 493 | + io:format("Run openws first.~n", []), |
| 494 | + halt(); |
| 495 | + [{_, APIKey}] -> |
| 496 | + case ets:lookup(myws, usessl) of |
| 497 | + [] -> |
| 498 | + io:format("Run openws first.~n", []), |
| 499 | + halt(); |
| 500 | + [{_, UseSSL}] -> |
| 501 | + case UseSSL of |
| 502 | + true -> |
| 503 | + Protocol = "https"; |
| 504 | + _ -> |
| 505 | + Protocol = "http" |
| 506 | + end, |
| 507 | + MyParams = uri_string:compose_query([{"key", APIKey}, {"check", "true"}]), |
| 508 | + |
| 509 | + case httpc:request(get, {Protocol ++ "://api.ip2location.com/v2/?" ++ MyParams, []}, [{ssl, [{versions, ['tlsv1.2']}]}, {autoredirect, false}], []) of |
| 510 | + {ok, {{_, 200, _}, _, Body}} -> |
| 511 | + jiffy:decode(unicode:characters_to_binary(Body,unicode,utf8),[return_maps]); |
| 512 | + {error, Reason} -> |
| 513 | + {error, Reason} |
| 514 | + end |
| 515 | + end |
| 516 | + end |
| 517 | + end. |
0 commit comments