Youtube Reports API 教学 - 频道中出报表

「鲑鱼均,因为一场鲑鱼之乱被主管称为鲑鱼世代,广义来说以年龄和脸蛋分类的话这应该算是一种 KNN 的机器学习,不正经的数据分析师,毕业後把人生暂停了半年,在 Google 和 AWS 办过几场演讲,缓下脚步的同时找了份跨领域工作。偶而慢跑、爱跟小动物玩耍。曾立过很多志,最近是希望当一个有细节的人。」


Youtube Reports API 教学 - 频道中出报表

YouTube Reports API 能够使开发人员安排报告的排程,并且批量下载生成报告。对於 YouTube Reports API 而言, API 支持预先所以定义好的报告内容,并且每个报告都包含一组针对频道使用者或内容管理员 YouTube 资讯。灵活的追踪每 Youtube 影片的影片资讯,也有了更加弹性的资料运用方式。这篇是 Python - 数位行销的 Youtube 分析教学系列文章的第 28 篇,也是我参加 2021 iThome 铁人赛中系列文章的第 28 天。

系列文章:Python — 数位行销分析与 Youtube API 教学
昨日回顾:Youtube Reports API 教学 - 最後一次做 OAuth2.0 授权

关於 YouTube Reports API

Reports

YouTube Reports API 的目的在於,让开发人员与使用者可以快速生成报告,并且对於报告进行取用和分析,对於 YouTube Reports API 的使用来说我们大致上分为几个步骤。

  • 呼叫 reportTypes.list() 方法以搜寻频道或内容管理员可以检索的报告列表。
  • 呼叫 jobs.create() 方法来确定应该为频道或内容管理员生成报告。随後使用 API 的 jobs.list() 和 jobs.delete() 来检索或更改正在生成的报告列表。
  • 呼叫 jobs.reports.list() 方法为特定作业生成的报告列表。响应中的每个资源都包含一个 downloadUrl 属性,该属性指定可以从中下载报告的 URL。
  • 发送授权的 GET 请求下载 URL 检索报告。

呼叫 reportTypes.list() 方法以搜寻频道

def list_report_types(youtube_reporting, **kwargs):
    kwargs = remove_empty_kwargs(**kwargs)
    results = youtube_reporting.reportTypes().list(**kwargs).execute()
    reportTypes = results['reportTypes']

    if 'reportTypes' in results and results['reportTypes']:
        reportTypes = results['reportTypes']
        for reportType in reportTypes:
            print ('Report type id: %s\n name: %s\n' % (reportType['id'], reportType['name']))
    else:
        print ('No report types found')
        return False
    return True

True

呼叫 jobs.create() 方法来确定内容管理员。

def create_reporting_job(youtube_reporting, report_type_id, **kwargs):
    kwargs = remove_empty_kwargs(**kwargs)

    reporting_job = youtube_reporting.jobs().create(
        body=dict(
            reportTypeId=args.report_type,
            name=args.name
        ),
        **kwargs
    ).execute()

    print('Reporting job "%s" created for reporting type "%s" at "%s"'
          % (reporting_job['name'], reporting_job['reportTypeId'],
             reporting_job['createTime']))

create

呼叫 jobs.reports.list() 方法为特定工作生成报告列表

def retrieve_reports(youtube_reporting, **kwargs):
    kwargs = remove_empty_kwargs(**kwargs)
    results = youtube_reporting.jobs().reports().list(
        **kwargs
    ).execute()
    if 'reports' in results and results['reports']:
        reports = results['reports']
        for report in reports:
            print('Report dates: %s \n       download URL: %s\n'
                  % (report['startTime'], report['downloadUrl']))

startTime

发送授权的 GET 请求下载 URL

def download_report(youtube_reporting, report_url, local_file):
    request = youtube_reporting.media().download(
        resourceName=' '
    )
    request.uri = report_url
    fh = FileIO(local_file, mode='wb')
    downloader = MediaIoBaseDownload(fh, request, chunksize=-1)
    done = False
    while done is False:
        status, done = downloader.next_chunk()
        if status:
            print('Download %d%%.' % int(status.progress() * 100))
    print('Download Complete!')

Download

执行档案做测试

在介绍完上方的步骤後,我们完成 OAuth2.0 验证,并且完成身分验证後,回传得到 Report dates 的资料列表,资料列表虽非按照日期排序,办事可以得到该有的日期都拥有,完成本次测试

OAuth2

今天说明了 Reporting API 的 OAuth2.0 开放授权

终於迈入 Reporting API 了,等於我们的挑战赛到达了赛末点,还蛮高兴的,如果有时间也欢迎看看我的夥伴们的文章

lu23770127 — SASS 基础初学三十天
10u1 — 糟了!是世界奇观!
juck30808 — Python — 数位行销分析与 Youtube API 教学
HLD — 浅谈物件导向与Design Pattern介绍
SiQing47 — 前端?後端?你早晚都要全端的,何不从现在开始?

Jerry Chien

【鲑鱼均】 现职是 200 多万订阅 Youtuber 的数据分析师,专长在 Python 的开发与使用,大学虽然是资讯背景但总是斜杠跑到商管行销领域,以工作角度来说的话,待过 FMCG、通讯软件、社群影音产业,也算是个数位行销体系出生的资讯人。这 30 天铁人挑战赛会从数位行销角度去重新切入数据分析这件事情,期待这个社会中,每个人能在各个角力间不断冲突而渐能找到一个平衡点回归最初的统计建立最终的初心。

下一篇:Youtube Reports API 教学 - 告一个段落


<<:  day28 等一下啦,会坏掉的/// Coroutine并发操作的重复请求

>>:  後端说修改时只需要送「有修改的栏位」过来

Flutter体验 Day 5-Widget 乐高积木

Widget乐高积木 在开始实作App之前,我们需要认识 Flutter 程序架构的基本概念。 回顾...

用 Python 畅玩 Line bot - 08:Audio message part2

既然可以将音档转为文字回传了,接下来我们可以尝试看看将收到的 Text message 转为音档去进...

Flutter学习Day3 Widget 观念 StatelessWidget (下)

大家安安,小弟弟要来接续上一篇的结尾 上一篇的进度是列出了宠物的清单 而现在我们想实现在点击清单的时...

Day5给你一个漂漂的导览列大家说好不好!

延续昨天,今天来完成一个导览列吧!! 首先先在components创一个navbar.vue 像昨天...

第3车厢-学会切版保证班?CSS的宝藏图

本篇公开自身学会切版的秘密武器是...,及分享学习CSS当中最重要的css盒模型概念 昨天文章中我...