Created
September 21, 2013 22:44
-
-
Save lc0/6654893 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import.packages <- function(pkg) { | |
| if (!require(pkg, character.only=TRUE)) { | |
| install.packages(pkg, dep=TRUE) | |
| require(pkg, character.only=TRUE) | |
| } | |
| } | |
| ip2coordinates <- function(ip, session=getCurlHandle()) { | |
| api <- "http://www.datasciencetoolkit.org/ip2coordinates/" | |
| get.ips <- getURL(paste(api, URLencode(ip), sep=""), curl=session) | |
| result <- ldply(fromJSON(get.ips), data.frame) | |
| names(result)[1] <- "ip.address" | |
| return(result) | |
| } | |
| load.users <- function(conn) { | |
| filename = sprintf('users.txt', date) | |
| print(filename) | |
| file = file.path("data", filename) | |
| if(!file.exists(file)) { | |
| print('There is not downloaded data, going to download') | |
| query <- "SELECT YOUR,SCHEMA FROM ACCOUNTS" | |
| df <- dbGetQuery(conn, query) | |
| write.table(df, file=file) | |
| return(df) | |
| } | |
| else { | |
| return (as.data.frame(read.table(file))) | |
| } | |
| } | |
| import.packages("RCurl") | |
| import.packages("plyr") | |
| import.packages("RJSONIO") | |
| pkg <- "RMySQL" | |
| # MySQL C Connector http://dev.mysql.com/downloads/connector/c/ | |
| if (!require(pkg, character.only=TRUE)) { | |
| Sys.setenv(MYSQL_HOME="c:\\Program Files\\MySQL\\MySQL Server 5.5") | |
| install.packages(pkg, type="source") | |
| require(pkg, character.only=TRUE) | |
| } | |
| import.packages("maps") | |
| import.packages("RgoogleMaps") | |
| import.packages("RColorBrewer") | |
| import.packages("mapdata") | |
| source("config.R") | |
| cols = brewer.pal(7,"Set2") | |
| if (require('RMySQL', character.only=TRUE)) { | |
| prod <- dbConnect(MySQL(), user=production.user, password=production.pass, | |
| dbname=production.db, host=production.host) | |
| } | |
| users <- load.users(prod) | |
| users$month <- as.integer(substr(users[,2], 6, 7)) | |
| month=-1 | |
| for (d in 1: length(users$a_ip)){ | |
| if (month != users$month[d]) { | |
| month <- users$month[d] | |
| print(month) | |
| dev.off() | |
| filename <- sprintf('%s-%s.png', substr(users$a_datecreated[d], 3, 4), users$month[d]) | |
| filename = file.path("map-images", filename) | |
| png(file=filename, width = 1280, height = 720) | |
| #data(worldMapEnv) | |
| #data(worldHires) | |
| map('world',col="#191919",bg="black",fill=T,mar=rep(0,4),border=0, resolution=0, xlim = c(-180, 180), ylim = c(-90, 90)) | |
| } | |
| print(users$a_alias[d]) | |
| result <- ip2coordinates(users$a_ip[d]) | |
| #options(error=traceback) | |
| try(points(result$longitude, result$latitude, col=cols[users$a_id[d] %% 7], pch=16, lwd=1), TRUE) | |
| if (d>10000) { | |
| dev.off() | |
| return(1) | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ip2coordinates <- function(ip, session=getCurlHandle()) { | |
| api <- "http://www.datasciencetoolkit.org/ip2coordinates/" | |
| get.ips <- getURL(paste(api, URLencode(ip), sep=""), curl=session) | |
| result <- ldply(fromJSON(get.ips), data.frame) | |
| names(result)[1] <- "ip.address" | |
| return(result) | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Sys.setenv(MYSQL_HOME="c:\\Program Files\\MySQL\\MySQL Server 5.5") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment