def google_trends(query_terms=['<STR_LIT>', '<STR_LIT>', '<STR_LIT>'], data_set='<STR_LIT>', refresh_data=False):
query_terms.sort()<EOL>import pandas<EOL>dir_path = os.path.join(data_path,'<STR_LIT>')<EOL>if not os.path.isdir(dir_path):<EOL><INDENT>os.makedirs(dir_path)<EOL><DEDENT>dir_name = '<STR_LIT:->'.join(query_terms)<EOL>dir_name = dir_name.replace('<STR_LIT:U+0020>', '<STR_LIT:_>')<EOL>dir_path = os.path.join(dir_path,dir_name)<EOL>file = '<STR_LIT>'<EOL>file_name = os.path.join(dir_path,file)<EOL>if not os.path.exists(file_name) or refresh_data:<EOL><INDENT>print("<STR_LIT>")<EOL>quoted_terms = []<EOL>for term in query_terms:<EOL><INDENT>quoted_terms.append(urllib2.quote(term))<EOL><DEDENT>print("<STR_LIT>", '<STR_LIT:U+002CU+0020>'.join(query_terms))<EOL>print("<STR_LIT>")<EOL>query = '<STR_LIT>' % "<STR_LIT:U+002C>".join(quoted_terms)<EOL>data = urlopen(query).read()<EOL>print("<STR_LIT>")<EOL>header = """<STR_LIT>"""<EOL>data = data[len(header):-<NUM_LIT:2>]<EOL>data = re.sub('<STR_LIT>', (lambda m: '<STR_LIT>' % (m.group(<NUM_LIT:1>).strip(), <NUM_LIT:1>+int(m.group(<NUM_LIT:2>)), int(m.group(<NUM_LIT:3>)))), data)<EOL>timeseries = json.loads(data)<EOL>columns = [k['<STR_LIT:label>'] for k in timeseries['<STR_LIT>']['<STR_LIT>']]<EOL>rows = map(lambda x: [k['<STR_LIT:v>'] for k in x['<STR_LIT:c>']], timeseries['<STR_LIT>']['<STR_LIT>'])<EOL>df = pandas.DataFrame(rows, columns=columns)<EOL>if not os.path.isdir(dir_path):<EOL><INDENT>os.makedirs(dir_path)<EOL><DEDENT>df.to_csv(file_name)<EOL><DEDENT>else:<EOL><INDENT>print("<STR_LIT>")<EOL>print("<STR_LIT>", '<STR_LIT:U+002CU+0020>'.join(query_terms))<EOL>df = pandas.read_csv(file_name, parse_dates=[<NUM_LIT:0>])<EOL><DEDENT>columns = df.columns<EOL>terms = len(query_terms)<EOL>import datetime<EOL>X = np.asarray([(row, i) for i in range(terms) for row in df.index])<EOL>Y = np.asarray([[df.ix[row][query_terms[i]]] for i in range(terms) for row in df.index ])<EOL>output_info = columns[<NUM_LIT:1>:]<EOL>return data_details_return({'<STR_LIT>' : df, '<STR_LIT:X>': X, '<STR_LIT:Y>': Y, '<STR_LIT>': output_info, '<STR_LIT:info>': "<STR_LIT>" + '<STR_LIT:U+002CU+0020>'.join(output_info) + '<STR_LIT:.>'}, data_set)<EOL>
Data downloaded from Google trends for given query terms. Warning, if you use this function multiple times in a row you get blocked due to terms of service violations. The function will cache the result of your query, if you wish to refresh an old query set refresh_data to True. The function is inspired by this notebook: http://nbviewer.ipython.org/github/sahuguet/notebooks/blob/master/GoogleTrends%20meet%20Notebook.ipynb