Permalink
Browse files

Set to automatically find python source

  • Loading branch information...
emhart committed Oct 4, 2012
1 parent 869451d commit 82f5e919cf308f464e35b72e15682782a79e7ffe
View
@@ -9,19 +9,26 @@
#' @author Edmund Hart \email{edmund.m.hart@@gmail.com}
#' @export
#' @examples \dontrun{
-#' my_search <- rGrends("Bieber")
+#' my_search <- rGtrends("Bieber")
#' plot(my_search[,2],my_search[,1],type='l')
#' }
#'
-rGtrends <- function(keywords,src_path = paste(getwd(),"/src",sep=""), date=c("all","all")){
+rGtrends <- function(keywords,src_path = NA, date=c("all","all")){
#require(rJava)
#require(rJython)
#require(stringr)
## Exception handling
if(!is.character(keywords)) stop("Keywords must be strings")
if(length(date)!=2) stop("Date must be a vector of length 2, see documentation")
+####Find the installed path of the package for the python if not specified ###
+if(is.na(src_path)){
+ libs_path <- installed.packages()
+ libs_path <- libs_path[grep("rGtrends",libs_path)[1],2]
+ src_path <- paste(libs_path,"/rGtrends/src",sep="")
+}
+
pg_path <- paste("'",src_path,"/pyGTrends.py'",sep="")
View
@@ -0,0 +1,19 @@
+rGtrends tutorial
+========================================================
+
+rGtrends is an R wrapper for making calls to [Google Trends](http://www.google.com/trends) it automatically calls a python api which downloads a temporary CSV. As of 9/27/2012 google changed it's interface for trends and the python component needs to be retooled for finer parsing. As it stands now only raw data is downloaded with the ability to sort by date. But once the python api is fixed individual sections can be downloaded.
+
+Step 1.
+----
+
+ensure
+```{r}
+summary(cars)
+```
+
+You can also embed plots, for example:
+
+```{r fig.width=7, fig.height=6}
+plot(cars)
+```
+
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
View
@@ -3,9 +3,7 @@
\title{Search google trends (http://trends.google.com)
using a python API.}
\usage{
- rGtrends(keywords,
- src_path = paste(getwd(), "/src", sep = ""),
- date = c("all", "all"))
+ rGtrends(keywords, src_path = NA, date = c("all", "all"))
}
\arguments{
\item{keywords}{A vector of search strings, each term
@@ -30,7 +28,7 @@ using a python API.}
}
\examples{
\dontrun{
-my_search <- rGrends("Bieber")
+my_search <- rGtrends("Bieber")
plot(my_search[,2],my_search[,1],type='l')
}
}
View
@@ -1,106 +0,0 @@
-import httplib
-import urllib
-import urllib2
-import re
-import csv
-
-from cookielib import CookieJar
-
-class pyGTrends(object):
- """
- Google Trends API
-
- Recommended usage:
-
- from csv import DictReader
- r = pyGTrends(username, password)
- r.download_report(('pants', 'skirt'))
- d = DictReader(r.csv().split('\n'))
- """
- def __init__(self, username, password):
- """
- provide login and password to be used to connect to Google Analytics
- all immutable system variables are also defined here
- website_id is the ID of the specific site on google analytics
- """
- self.login_params = {
- "continue": 'http://www.google.com/trends',
- "PersistentCookie": "yes",
- "Email": username,
- "Passwd": password,
- }
- self.headers = [("Referrer", "https://www.google.com/accounts/ServiceLoginBoxAuth"),
- ("Content-type", "application/x-www-form-urlencoded"),
- ('User-Agent', 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/535.21 (KHTML, like Gecko) Chrome/19.0.1042.0 Safari/535.21'),
- ("Accept", "text/plain")]
- self.url_ServiceLoginBoxAuth = 'https://accounts.google.com/ServiceLoginBoxAuth'
- self.url_Export = 'http://www.google.com/trends/viz'
- self.url_CookieCheck = 'https://www.google.com/accounts/CheckCookie?chtml=LoginDoneHtml'
- self.header_dictionary = {}
- self._connect()
-
- def _connect(self):
- """
- connect to Google Trends
- """
-
- self.cj = CookieJar()
- self.opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(self.cj))
- self.opener.addheaders = self.headers
-
- galx = re.compile('<input type="hidden" name="GALX" value="(?P<galx>[a-zA-Z0-9_-]+)">')
-
- resp = self.opener.open(self.url_ServiceLoginBoxAuth).read()
- resp = re.sub(r'\s\s+', ' ', resp)
-
- m = galx.search(resp)
- if not m:
- raise Exception("Cannot parse GALX out of login page")
- self.login_params['GALX'] = m.group('galx')
- params = urllib.urlencode(self.login_params)
- self.opener.open(self.url_ServiceLoginBoxAuth, params)
- self.opener.open(self.url_CookieCheck)
-
- def download_report(self, keywords, date='all', geo='all', geor='all', graph = 'all_csv', sort=0, scale=0, sa='N'):
- """
- download a specific report
- date, geo, geor, graph, sort, scale and sa
- are all Google Trends specific ways to slice the data
- """
- if type(keywords) not in (type([]), type(('tuple',))):
- keywords = [keywords]
-
- params = urllib.urlencode({
- 'q': ",".join(keywords),
- 'date': date,
- 'graph': graph,
- 'geo': geo,
- 'geor': geor,
- 'sort': str(sort),
- 'scale': str(scale),
- 'sa': sa
- })
- self.raw_data = self.opener.open('http://www.google.com/trends/viz?' + params).read()
- if self.raw_data in ['You must be signed in to export data from Google Trends']:
- raise Exception(self.raw_data)
-
- def csv(self, section="main", as_list=False):
- """
- Returns a CSV of a specific segment of the data.
- Available segments include Main, Language, City and Region.
- """
- if section == "main":
- section = ("Week","Year","Day","Month")
- else:
- section = (section,)
-
- segments = self.raw_data.split('\n\n\n')
- for s in segments:
- if s.partition(',')[0] in section:
- if as_list:
- return [line for line in csv.reader(s.split('\n'))]
- else:
- return s
-
- raise Exception("Could not find requested section")
-
View
@@ -1,12 +0,0 @@
-def pyGparse(pyGtrendobj):
- my_lines = pyGtrendobj.splitlines()
- p_num = ((len(my_lines[0].split(",")) - 1)/2)
- output = [[0 for x in xrange(0)] for x in xrange(p_num + 1)]
-
- for x in my_lines:
- t_string = x.split(",")
- my_dat = map(lambda i: t_string[i],filter(lambda i: i%2 == 1,range(len(t_string))))
- my_dat.insert(0,t_string[0])
- map(lambda i: output[i].append(my_dat[i]),range(len(my_dat)))
-
- return output

0 comments on commit 82f5e91

Please sign in to comment.