Yahoo Gemini API Python获取报告的示例?

时间:2016-08-18 05:46:23

标签: yahoo-api

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

我需要下载雅虎双子座广告报道。但雅虎文档只有PHP代码,没有python。有人可以分享任何意见吗?

我以前做过oauth,但它有一些基本的文档

1 个答案:

答案 0 :(得分:7)

使用下面的示例

尝试此lib https://github.com/josuebrunel/yahoo-oauth
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