Skip to content

Commit e34115b

Browse files
committed
1.2.0
Added plotmap function.
1 parent d99a41e commit e34115b

File tree

10 files changed

+420
-16
lines changed

10 files changed

+420
-16
lines changed

DESCRIPTION

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,15 @@
11
Package: ip2proxy
22
Type: Package
33
Title: Lookup for IP Address Proxy Information
4-
Version: 1.0.0
4+
Version: 1.2.0
55
Author: IP2Location
66
Maintainer: IP2Location <[email protected]>
77
Description: A R package to find the IP addresses which are used as VPN anonymizer, open proxies, web proxies and Tor exits.
88
The package lookup the proxy IP address from IP2Proxy BIN Data file. You may visit <https://lite.ip2location.com> for free database download.
99
License: MIT + file LICENSE
1010
Encoding: UTF-8
1111
Depends: R (>= 3.2.3)
12-
Imports: reticulate (>= 1.13), stringr (>= 1.4.0), jsonlite (>= 1.6)
13-
LazyData: true
12+
Imports: reticulate (>= 1.13), jsonlite (>= 1.6), ggplot2 (>= 3.4), maps (>= 3.4.1), scales (>= 1.2.1)
1413
SystemRequirements: IP2Proxy Python library
1514
<https://www.ip2location.com/development-libraries/ip2proxy/python>
16-
RoxygenNote: 7.1.1
15+
RoxygenNote: 7.2.3

NAMESPACE

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,12 @@
22

33
export(get_all)
44
export(is_proxy)
5+
export(lookup_web_service)
56
export(open)
7+
export(plot_map)
8+
import(ggplot2)
69
import(jsonlite)
10+
import(maps)
711
import(reticulate)
12+
import(scales)
13+
import(utils)

R/IP2Proxy.r

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
#' @return NULL
66
#' @import reticulate
77
#' @export
8-
#' @examples \donttest{
8+
#' @examples \dontrun{
99
#' open("~/IP-COUNTRY.BIN")
1010
#' }
1111
#'
@@ -19,14 +19,14 @@ open <- function(bin_location){
1919

2020
#' @title Lookup for IP address proxy information
2121
#'
22-
#' @description Find the country, region, city, ISP, domain name, usage types, asn, as name, last seen and threat type. The return values will be depending on the BIN data loaded.
22+
#' @description Find the country, region, city, ISP, domain name, usage types, asn, as name, last seen, threat type and provider. The return values will be depending on the BIN data loaded.
2323
#' @param ip IPv4 or IPv6 address
2424
#' @return Return all the proxy information about the IP address
2525
#' @import reticulate
2626
#' @import jsonlite
2727
#' @export
28-
#' @examples \donttest{
29-
#' get_all("8.8.8.8")
28+
#' @examples \dontrun{
29+
#' get_all("1.0.241.135")
3030
#' }
3131
#'
3232

@@ -45,8 +45,8 @@ get_all <- function(ip){
4545
#' @import reticulate
4646
#' @import jsonlite
4747
#' @export
48-
#' @examples \donttest{
49-
#' is_proxy("8.8.8.8")
48+
#' @examples \dontrun{
49+
#' is_proxy("1.0.241.135")
5050
#' }
5151
#'
5252

@@ -59,3 +59,29 @@ is_proxy <- function(ip){
5959
result = fromJSON(py$j1)
6060
return(result['is_proxy'])
6161
}
62+
63+
#' @title Lookup for IP address proxy information using IP2Proxy web service.
64+
#' @param api_key IP2Proxy web service API key
65+
#' @param ip IPv4 or IPv6 address
66+
#' @param package Package to use for IP2Proxy web service.
67+
#' @description Find the country, region, city, ISP, domain name, usage types, asn, as name, last seen, threat type and provider. The return values will be depending on the IP2Proxy web service package used.
68+
#' @return Return all the proxy information about the IP address
69+
#' @import reticulate
70+
#' @import jsonlite
71+
#' @export
72+
#' @examples \dontrun{
73+
#' lookup_web_service("1.0.241.135","PX1")
74+
#' }
75+
#'
76+
77+
lookup_web_service <- function(api_key, ip, package = 'PX1'){
78+
py_run_string("import IP2Proxy")
79+
py_run_string("import json")
80+
ws_initialize = paste("ws = IP2Proxy.IP2ProxyWebService('", api_key, "','", package, "',True)", sep = "")
81+
py_run_string(ws_initialize)
82+
API_result = paste("rec = ws.lookup('", ip, "')", sep = "")
83+
py_run_string(API_result)
84+
py_run_string("j = json.dumps(rec)")
85+
result = fromJSON(py$j)
86+
return(result)
87+
}

R/IP2Proxy_plotmap.R

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
if(getRversion() >= "2.15.1") utils::globalVariables(c("long", "lat", "group", "prop"))
2+
3+
#' @title Plot map using IP2Location data.
4+
#'
5+
#' @description Plot the country on the map based on IP addresses and its IP2Location country data.
6+
#' @param ips A vector of IP addresses to be plot on
7+
#' @return NULL
8+
#' @import maps
9+
#' @import ggplot2
10+
#' @import scales
11+
#' @import utils
12+
#' @export
13+
#' @examples \dontrun{
14+
#' plot_map(c("1.0.241.135", "1.2.3.4"))
15+
#' }
16+
#'
17+
18+
plot_map <- function(ips){
19+
countries = c()
20+
mapData = map_data("world")
21+
for ( i in ips ) {
22+
result = ip2proxy::get_all(i)
23+
countries = append(countries, toString(result["country_long"]))
24+
}
25+
26+
country_table <- table(countries)
27+
ipData <- data.frame(country_table)
28+
29+
variable1 = deparse(substitute(countries))
30+
variable1 = strsplit(variable1, "\\$")[[1]][2]
31+
32+
if(ncol(ipData) == 2) {
33+
names(ipData) <- c(variable1, "n")
34+
} else if(ncol(ipData) > 2){
35+
names(ipData)[length(ipData)] <- "n"
36+
}
37+
38+
prop <- as.vector(country_table)/sum(country_table)
39+
ipData <- data.frame(ipData, prop)
40+
41+
table.prop <- as.vector(country_table)/sum(country_table)
42+
table.perc <- format(round(table.prop*100, 1), nsmall = 1)
43+
table.perc <- gsub("$", "%", table.perc)
44+
ipData <- data.frame(ipData, table.perc)
45+
46+
names(ipData)[1] = "group"
47+
48+
path = find.package("ip2proxy")
49+
50+
#data <- read.csv("inst\\countrynames_mapping.txt", header=TRUE)
51+
data <- read.csv(paste(path, '/countrynames_mapping.txt', sep = ""), header=TRUE)
52+
53+
data$matched_country_name[data$matched_country_name == ''] <- NA
54+
55+
for(i in 1:nrow(data)){
56+
if(!is.na(data[i, "matched_country_name"])){
57+
old.name <- paste(data[i, "country_name"])
58+
new.name <- paste(data[i, "matched_country_name"])
59+
ipData[, "group"] <- gsub(old.name, new.name, ipData[, "group"])
60+
}
61+
}
62+
63+
worldMapIPs <- merge(mapData, ipData, by.x = "region", by.y = "group", all.x = TRUE)
64+
worldMapIPs <- worldMapIPs[order(worldMapIPs[, "order"]), ]
65+
worldMapIPs[is.na(worldMapIPs)] <- 0
66+
world <- map_data("world")
67+
p <- ggplot() +
68+
geom_polygon(data=world, aes(x=long, y=lat, group = group),
69+
colour = "#595959", fill = "white") + theme(panel.background = element_rect(fill = "#b2cce5"), plot.background = element_rect(fill = "#b2cce5"))
70+
71+
mapcolors = c("black", "#d6d6d6", "white")
72+
mapvalues = c(1, .025, 0)
73+
74+
75+
p + geom_polygon(data=worldMapIPs, aes(x=long, y=lat, group = group, fill = prop)) + geom_path(data=worldMapIPs, aes(x=long, y=lat, group=group), color="#595959", size=0.05) + scale_fill_gradientn(colours= mapcolors, values= mapvalues, labels = percent_format(), limits = c(0,1)) + theme(plot.title = element_text(size = 20, colour = "black", family = "sans", margin = unit(c(0, 0, 5, 0), "mm"))) + scale_y_continuous(name=NULL, breaks=NULL, expand = c(0,0)) + scale_x_continuous(name=NULL, breaks=NULL, expand = c(0,0)) + theme(legend.justification = c(0, 0), legend.position = c(0, 0.32), legend.background = element_blank(), legend.title = element_blank(), legend.text = element_text(size = rel(1.1), colour = "black", family = "sans")) + guides(fill = guide_colorbar(barwidth = rel(0.5), barheight = rel(5.0), ticks = F))
76+
}

0 commit comments

Comments
 (0)