Help needed with Python API

This is the place for queries that don't fit in any of the other categories.

Help needed with Python API

Postby gachladd » Sat May 11, 2013 11:22 pm

Hi Guys

I use Python 2.7.4 in conjunction with notepad++ . I downloaded the source code for jdmunro / unofficial-google-trends-api which enables users to download CSV reports from Google Trends. The problem is noting happens when i run it. About me, I am not a programmer, I downloaded a youtube video on how to run an API so I suspect I did not put the correct code in. Can some one please try and run it looking for two terms and output code if it works? I really hope someone can help me it would be fantastic, thanks so much,


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")
gachladd
 
Posts: 2
Joined: Sat May 11, 2013 11:07 pm

Re: Help needed with Python API

Postby hansn » Sun May 12, 2013 12:57 am

Please use code tags when pasting code.

See here for instructions viewtopic.php?f=6&t=145
hansn
 
Posts: 87
Joined: Thu Feb 21, 2013 8:46 pm

Re: Help needed with Python API

Postby micseydel » Sun May 12, 2013 5:14 am

Did you run this exact code, as is? It just defines functionality, doesn't do anything. It's an "Application programming interface". It's something that someone skilled in programming can make use of, though laypeople would have to learn programming in order to use.
Join the #python-forum IRC channel on irc.freenode.net!
User avatar
micseydel
 
Posts: 1179
Joined: Tue Feb 12, 2013 2:18 am
Location: Mountain View, CA

Re: Help needed with Python API

Postby gachladd » Sun May 12, 2013 8:12 am

thanks for reply micseydel, that makes sense
gachladd
 
Posts: 2
Joined: Sat May 11, 2013 11:07 pm

Re: Help needed with Python API

Postby micseydel » Sun May 12, 2013 8:21 am

If you need help making use of it, feel free to post back, making sure to follow the rules here, or you can post requesting a job here (not sure how often you get responses there though).
Join the #python-forum IRC channel on irc.freenode.net!
User avatar
micseydel
 
Posts: 1179
Joined: Tue Feb 12, 2013 2:18 am
Location: Mountain View, CA


Return to General Coding Help

Who is online

Users browsing this forum: patzelt and 3 guests