gpt4 book ai didi

python-2.7 - 提取时自动日期范围

转载 作者:行者123 更新时间:2023-12-03 16:52:09 27 4
gpt4 key购买 nike

我正在使用以下脚本从Google Analytics(分析)提取数据。在这里,我正在提取上一周的数据。我想自动化日期范围,这样我就不必每周更改date_range了。
我也想避免GA采样数据。请指导我正确执行详细信息的正确方法。

作者='test@gmail.com(测试)'

import argparse
import sys
import csv
import string
import datetime
import json
import time

from apiclient.errors import HttpError
from apiclient import sample_tools
from oauth2client.client import AccessTokenRefreshError

cam_name = sys.argv[1:]

class SampledDataError(Exception): pass

def main(argv):
# Authenticate and construct service.
service, flags = sample_tools.init(
argv[0], 'analytics', 'v3', __doc__, __file__,
scope='https://www.googleapis.com/analytics.readonly')

# Try to make a request to the API. Print the results or handle errors.
try:
profile_id = profile_ids[profile]
if not profile_id:
print ('Could not find a valid profile for this user.')
else:
metrics = argv[1]
dimensions = argv[2]
reportName = argv[3]
sort = argv[4]
filters = argv[5]

for start_date, end_date in date_ranges:
limit = ga_query(service, profile_id, 0,
start_date, end_date, metrics, dimensions, sort, filters).get('totalResults')
for pag_index in range(0, limit, 10000):
results = ga_query(service, profile_id, pag_index,
start_date, end_date, metrics, dimensions, sort, filters)
# if results.get('containsSampledData'):

# raise SampledDataError
print_results(results, pag_index, start_date, end_date, reportName)

except TypeError as error:
# Handle errors in constructing a query.
print ('There was an error in constructing your query : %s' % error)

except HttpError as error:
# Handle API errors.
print ('Arg, there was an API error : %s : %s' %
(error.resp.status, error._get_reason()))

except AccessTokenRefreshError:
# Handle Auth errors.
print ('The credentials have been revoked or expired, please re-run '
'the application to re-authorize')

except SampledDataError:
# force an error if ever a query returns data that is sampled!
print ('Error: Query contains sampled data!')


def ga_query(service, profile_id, pag_index, start_date, end_date, metrics, dimensions, sort, filters):

return service.data().ga().get(
ids='ga:' + profile_id,
start_date=start_date,
end_date=end_date,
metrics=metrics,
dimensions=dimensions,
sort=sort,
filters=filters,
samplingLevel='HIGHER_PRECISION',
start_index=str(pag_index+1),
max_results=str(pag_index+10000)).execute()


def print_results(results, pag_index, start_date, end_date, reportName):
"""Prints out the results.

This prints out the profile name, the column headers, and all the rows of
data.

Args:
results: The response returned from the Core Reporting API.
"""

# New write header
if pag_index == 0:
if (start_date, end_date) == date_ranges[0]:
print ('Profile Name: %s' % results.get('profileInfo').get('profileName'))
columnHeaders = results.get('columnHeaders')
cleanHeaders = [str(h['name']) for h in columnHeaders]
writer.writerow(cleanHeaders)
print (reportName,'Now pulling data from %s to %s.' %(start_date, end_date))


# Print data table.
if results.get('rows', []):
for row in results.get('rows'):
for i in range(len(row)):
old, new = row[i], str()
for s in old:
new += s if s in string.printable else ''
row[i] = new
writer.writerow(row)

else:
print ('No Rows Found')

limit = results.get('totalResults')
print (pag_index, 'of about', int(round(limit, -4)), 'rows.')
return None

# Uncomment this line & replace with 'profile name': 'id' to query a single profile
# Delete or comment out this line to loop over multiple profiles.

#Brands

profile_ids = {'abc-Mobile': '12345',
'abc-Desktop': '23456',
'pqr-Mobile': '34567',
'pqr-Desktop': '45678',
'xyz-Mobile': '56789',
'xyz-Desktop': '67890'}

date_ranges = [
('2017-01-24','2017-01-24'),
('2017-01-25','2017-01-25'),
('2017-01-26','2017-01-26'),
('2017-01-27','2017-01-27'),
('2017-01-28','2017-01-28'),
('2017-01-29','2017-01-29'),
('2017-01-30','2017-01-30')
]

for profile in sorted(profile_ids):
print("Sequence 1",profile)
with open('qwerty.json') as json_data:
d = json.load(json_data)
for getThisReport in d["Reports"]:
print("Sequence 2",getThisReport["ReportName"])
reportName = getThisReport["ReportName"]
metrics = getThisReport["Metrics"]
dimensions = getThisReport["Dimensions"]
sort = getThisReport["sort"]
filters = getThisReport["filter"]

path = 'C:\\Projects\\DataExport\\test\\' #replace with path to your folder where csv file with data will be written

today = time.strftime('%Y%m%d')

filename = profile+'_'+reportName+'_'+today+'.csv' #replace with your filename. Note %s is a placeholder variable and the profile name you specified on row 162 will be written here
with open(path + filename, 'wt') as f:
writer = csv.writer(f,delimiter = '|', lineterminator='\n', quoting=csv.QUOTE_MINIMAL)
args = [sys.argv,metrics,dimensions,reportName,sort,filters]
if __name__ == '__main__': main(args)
print ( "Profile done. Next profile...")

print ("All profiles done.")

最佳答案

就日期而言,Core Reporting API支持一些有趣的事情。


所有Google Analytics(分析)数据请求都必须指定一个日期范围。如果请求中未包含开始日期和结束日期参数,则服务器将返回错误。使用YYYY-MM-DD模式可以将日期值指定为特定日期,而使用今天,昨天或NdaysAgo模式则可以将日期值指定为相对日期。值必须匹配[0-9] {4}-[0-9] {2}-[0-9] {2} |今天|昨天| [0-9] +(daysAgo)。


所以做类似的事情

start_date = '7daysAgo' 
end_date = 'today'


请记住,数据没有在24-48小时内完成处理,因此您今天,昨天和前一天的数据可能不是100%准确的。

关于python-2.7 - 提取时自动日期范围,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42478655/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com