Skip to content

Instantly share code, notes, and snippets.

@xccds
Created April 29, 2012 12:27

Revisions

  1. xccds revised this gist May 1, 2012. No changes.
  2. xccds revised this gist May 1, 2012. 1 changed file with 7 additions and 4 deletions.
    11 changes: 7 additions & 4 deletions weather.R
    Original file line number Diff line number Diff line change
    @@ -13,24 +13,27 @@ fromurl<- function(finalurl) {
    currenttemp <- raw$current_observation$temp_c
    currentweather <- raw$current_observation$weather
    city <- as.character(raw$current_observation$display_location['full'])
    result <-list(city=city,current=paste(currenttemp,'°C ',currentweather,sep=''),tomorrow=paste(high,'°C','-',low,'°C ',condition,sep=''))
    result <-list(city=city,current=paste(currenttemp,'°C ',currentweather,sep=''),
    tomorrow=paste(high,'°C','-',low,'°C ',condition,sep=''))
    names(result) <-c('城市','当前', '明天')
    return(result)
    }
    # 提取天气预报的主函数
    getweather <- function(city='') {
    # 如果用户输入为空,则根据IP地址来查询
    if (city == '') {
    finalurl <- 'http://api.wunderground.com/api/a98d04ac43156c84/conditions/forecast/lang:CN/q/autoip.json'
    finalurl <- 'http://api.wunderground.com/api/yourkey/conditions/
    forecast/lang:CN/q/autoip.json'
    return(fromurl(finalurl))
    # 否则就调用google API,这时需要用XML包来解析数据得到经纬度
    } else {
    requestUrl<-paste("http://maps.googleapis.com/maps/api/geocode/xml?address=",city,"&sensor=false", sep="")
    requestUrl<-paste("http://maps.googleapis.com/maps/api/geocode/xml?address="
    ,city,"&sensor=false", sep="")
    xmlResult<-xmlTreeParse(requestUrl,isURL=TRUE)
    root <- xmlRoot(xmlResult)
    lat <-xmlValue(root[['result']][['geometry']][['location']][['lat']])
    lon <-xmlValue(root[['result']][['geometry']][['location']][['lng']])
    url <- 'http://api.wunderground.com/api/a98d04ac43156c84/conditions/forecast/lang:CN/q/'
    url <- 'http://api.wunderground.com/api/yourkey/conditions/forecast/lang:CN/q/'
    # 将经纬度与其它信息相结合,形成查询地址
    finalurl <- paste(url,as.character(lat),',',as.character(lon),'.json',sep='')
    return(fromurl(finalurl))
  3. xccds revised this gist May 1, 2012. 1 changed file with 29 additions and 22 deletions.
    51 changes: 29 additions & 22 deletions weather.R
    Original file line number Diff line number Diff line change
    @@ -1,31 +1,38 @@
    getweather <- function() {
    library(RCurl)
    library(RJSONIO)
    library(XML)
    # 输入需要查询的城市名称,若中国城市则输入拼音即可,如:beijin
    city <- as.character(readline('please input the city name:'))
    # 从google API处获得经纬度数据
    requestUrl<-paste("http://maps.googleapis.com/maps/api/geocode
    /xml?address=",city,"&sensor=false", sep="")
    xmlResult<-xmlTreeParse(requestUrl,isURL=TRUE)
    root <- xmlRoot(xmlResult)
    lat <-xmlValue(root[['result']][['geometry']][['location']][['lat']])
    lon <-xmlValue(root[['result']][['geometry']][['location']][['lng']])
    # 将经纬度数据输入到wunderground.com的API中获得天气数据
    # 注意你要申请自己的key放到your_key处
    url <- 'http://api.wunderground.com/api/your_key/conditions/
    forecast/lang:CN/q/'
    finalurl <- paste(url,as.character(lat),',',as.character(lon),
    '.json',sep='')
    # 加载所需扩展包
    library(RCurl)
    library(RJSONIO)
    library(XML)
    # 建立一个根据网址提取天气预报的子函数
    fromurl<- function(finalurl) {
    # 先读取网页,再解析JSON数据存在raw中
    web <- getURL(finalurl)
    raw <-fromJSON(web)
    high <- raw$forecast$simpleforecast$forecastday[[2]]$high['celsius']
    low <- raw$forecast$simpleforecast$forecastday[[2]]$low['celsius']
    condition <- raw$forecast$simpleforecast$forecastday[[2]]$conditions
    currenttemp <- raw$current_observation$temp_c
    currentweather <- raw$current_observation$weather
    result <-list(current=paste(currenttemp,'°C ',currentweather,sep=''),
    tomorrow=paste(high,'°C','-',low,'°C ',condition,sep=''))
    names(result) <-c('当前', '明天')
    city <- as.character(raw$current_observation$display_location['full'])
    result <-list(city=city,current=paste(currenttemp,'°C ',currentweather,sep=''),tomorrow=paste(high,'°C','-',low,'°C ',condition,sep=''))
    names(result) <-c('城市','当前', '明天')
    return(result)
    }
    # 提取天气预报的主函数
    getweather <- function(city='') {
    # 如果用户输入为空,则根据IP地址来查询
    if (city == '') {
    finalurl <- 'http://api.wunderground.com/api/a98d04ac43156c84/conditions/forecast/lang:CN/q/autoip.json'
    return(fromurl(finalurl))
    # 否则就调用google API,这时需要用XML包来解析数据得到经纬度
    } else {
    requestUrl<-paste("http://maps.googleapis.com/maps/api/geocode/xml?address=",city,"&sensor=false", sep="")
    xmlResult<-xmlTreeParse(requestUrl,isURL=TRUE)
    root <- xmlRoot(xmlResult)
    lat <-xmlValue(root[['result']][['geometry']][['location']][['lat']])
    lon <-xmlValue(root[['result']][['geometry']][['location']][['lng']])
    url <- 'http://api.wunderground.com/api/a98d04ac43156c84/conditions/forecast/lang:CN/q/'
    # 将经纬度与其它信息相结合,形成查询地址
    finalurl <- paste(url,as.character(lat),',',as.character(lon),'.json',sep='')
    return(fromurl(finalurl))
    }
    }
  4. xccds revised this gist Apr 29, 2012. 1 changed file with 10 additions and 5 deletions.
    15 changes: 10 additions & 5 deletions weather.R
    Original file line number Diff line number Diff line change
    @@ -5,22 +5,27 @@ getweather <- function() {
    # 输入需要查询的城市名称,若中国城市则输入拼音即可,如:beijin
    city <- as.character(readline('please input the city name:'))
    # 从google API处获得经纬度数据
    requestUrl<-paste("http://maps.googleapis.com/maps/api/geocode/xml?address=",city,"&sensor=false", sep="")
    requestUrl<-paste("http://maps.googleapis.com/maps/api/geocode
    /xml?address=",city,"&sensor=false", sep="")
    xmlResult<-xmlTreeParse(requestUrl,isURL=TRUE)
    root <- xmlRoot(xmlResult)
    lat <-xmlValue(root[['result']][['geometry']][['location']][['lat']])
    lon <-xmlValue(root[['result']][['geometry']][['location']][['lng']])
    # 将经纬度数据输入到wunderground.com的API中获得天气数据,注意你要申请自己的key放到your_key处
    url <- 'http://api.wunderground.com/api/your_key/conditions/forecast/lang:CN/q/'
    finalurl <- paste(url,as.character(lat),',',as.character(lon),'.json',sep='')
    # 将经纬度数据输入到wunderground.com的API中获得天气数据
    # 注意你要申请自己的key放到your_key处
    url <- 'http://api.wunderground.com/api/your_key/conditions/
    forecast/lang:CN/q/'
    finalurl <- paste(url,as.character(lat),',',as.character(lon),
    '.json',sep='')
    web <- getURL(finalurl)
    raw <-fromJSON(web)
    high <- raw$forecast$simpleforecast$forecastday[[2]]$high['celsius']
    low <- raw$forecast$simpleforecast$forecastday[[2]]$low['celsius']
    condition <- raw$forecast$simpleforecast$forecastday[[2]]$conditions
    currenttemp <- raw$current_observation$temp_c
    currentweather <- raw$current_observation$weather
    result <-list(current=paste(currenttemp,'°C ',currentweather,sep=''),tomorrow=paste(high,'°C','-',low,'°C ',condition,sep=''))
    result <-list(current=paste(currenttemp,'°C ',currentweather,sep=''),
    tomorrow=paste(high,'°C','-',low,'°C ',condition,sep=''))
    names(result) <-c('当前', '明天')
    return(result)
    }
  5. xccds revised this gist Apr 29, 2012. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions weather.R
    Original file line number Diff line number Diff line change
    @@ -10,8 +10,8 @@ getweather <- function() {
    root <- xmlRoot(xmlResult)
    lat <-xmlValue(root[['result']][['geometry']][['location']][['lat']])
    lon <-xmlValue(root[['result']][['geometry']][['location']][['lng']])
    # 将经纬度数据输入到wunderground.com的API中获得天气数据
    url <- 'http://api.wunderground.com/api/a98d04ac43156c84/conditions/forecast/lang:CN/q/'
    # 将经纬度数据输入到wunderground.com的API中获得天气数据,注意你要申请自己的key放到your_key处
    url <- 'http://api.wunderground.com/api/your_key/conditions/forecast/lang:CN/q/'
    finalurl <- paste(url,as.character(lat),',',as.character(lon),'.json',sep='')
    web <- getURL(finalurl)
    raw <-fromJSON(web)
  6. xccds revised this gist Apr 29, 2012. 1 changed file with 3 additions and 0 deletions.
    3 changes: 3 additions & 0 deletions weather.R
    Original file line number Diff line number Diff line change
    @@ -2,12 +2,15 @@ getweather <- function() {
    library(RCurl)
    library(RJSONIO)
    library(XML)
    # 输入需要查询的城市名称,若中国城市则输入拼音即可,如:beijin
    city <- as.character(readline('please input the city name:'))
    # 从google API处获得经纬度数据
    requestUrl<-paste("http://maps.googleapis.com/maps/api/geocode/xml?address=",city,"&sensor=false", sep="")
    xmlResult<-xmlTreeParse(requestUrl,isURL=TRUE)
    root <- xmlRoot(xmlResult)
    lat <-xmlValue(root[['result']][['geometry']][['location']][['lat']])
    lon <-xmlValue(root[['result']][['geometry']][['location']][['lng']])
    # 将经纬度数据输入到wunderground.com的API中获得天气数据
    url <- 'http://api.wunderground.com/api/a98d04ac43156c84/conditions/forecast/lang:CN/q/'
    finalurl <- paste(url,as.character(lat),',',as.character(lon),'.json',sep='')
    web <- getURL(finalurl)
  7. xccds created this gist Apr 29, 2012.
    23 changes: 23 additions & 0 deletions weather.R
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,23 @@
    getweather <- function() {
    library(RCurl)
    library(RJSONIO)
    library(XML)
    city <- as.character(readline('please input the city name:'))
    requestUrl<-paste("http://maps.googleapis.com/maps/api/geocode/xml?address=",city,"&sensor=false", sep="")
    xmlResult<-xmlTreeParse(requestUrl,isURL=TRUE)
    root <- xmlRoot(xmlResult)
    lat <-xmlValue(root[['result']][['geometry']][['location']][['lat']])
    lon <-xmlValue(root[['result']][['geometry']][['location']][['lng']])
    url <- 'http://api.wunderground.com/api/a98d04ac43156c84/conditions/forecast/lang:CN/q/'
    finalurl <- paste(url,as.character(lat),',',as.character(lon),'.json',sep='')
    web <- getURL(finalurl)
    raw <-fromJSON(web)
    high <- raw$forecast$simpleforecast$forecastday[[2]]$high['celsius']
    low <- raw$forecast$simpleforecast$forecastday[[2]]$low['celsius']
    condition <- raw$forecast$simpleforecast$forecastday[[2]]$conditions
    currenttemp <- raw$current_observation$temp_c
    currentweather <- raw$current_observation$weather
    result <-list(current=paste(currenttemp,'°C ',currentweather,sep=''),tomorrow=paste(high,'°C','-',low,'°C ',condition,sep=''))
    names(result) <-c('当前', '明天')
    return(result)
    }