Shri
Shri

Reputation: 11

Yahoo Gemini API Python Example to fetch reports?

https://developer.yahoo.com/gemini/

I need to download yahoo gemini ad reports. But yahoo documentation only has php code and no python. can someone please share any inputs ?

I have done oauth before but it had some basic documentation

Upvotes: 1

Views: 1749

Answers (1)

Kubber
Kubber

Reputation: 397

Try this lib https://github.com/josuebrunel/yahoo-oauth with examples below

import urllib
import json
import time
from yahoo_oauth import OAuth2

oauth = OAuth2(None, None, from_file='credentials.json')

if not oauth.token_is_valid():
    oauth.refresh_access_token()

 # get all accounts
response = oauth.session.get("https://api.admanager.yahoo.com/v1/rest/advertiser/")
data = response.content
print data
jdata = json.loads(data)
for j in jdata['response']:
    print "{} {}".format(j['id'], j['advertiserName'])

# get advertiser data
advertiser_id = 12345678
report_date_from = "2016-08-28"
report_date_to = "2016-08-28"
payload = {"cube": "performance_stats",
           "fields": [
               {"field": "Day"},
               {"field": "Impressions"},
               {"field": "Conversions"},
               {"field": "Spend"},
               {"field": "Campaign ID"}
           ],
           "filters": [
               {"field": "Advertiser ID", "operator": "=", "value": advertiser_id},
               {"field": "Day", "operator": "between", "from": report_date_from, "to": report_date_to}
           ]}

response = oauth.session.post("https://api.admanager.yahoo.com/v1/rest/reports/custom?reportFormat=json", json=payload)
print response.content

jdata = json.loads(response.content)
job_id = jdata['response']['jobId']

# you will need to add some loop and waits before the report is ready
time.sleep(60)

url = "https://api.admanager.yahoo.com/v1/rest/reports/custom/{}?advertiserId={}".format(job_id, advertiser_id)
response = oauth.session.get(url)
print response.content

# report will be returned as url
rdata = json.loads(response.content)
if 'status' in rdata['response'] and rdata['response']['status'] == 'completed':
    report = urllib.urlopen(rdata['response']['jobResponse']).read()
    print report

Upvotes: 7

Related Questions