You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

通过流媒体服务获取歌曲播放量并自动更新至Excel表格

解决方案

针对你需要每日自动更新Excel中多平台歌曲播放量的需求,结合当前已实现的Spotify热度获取脚本,以下是具体问题的解决思路和优化方案:

一、获取Spotify原始播放量的可行方案

Spotify公开Web API仅返回popularity热度值,无法直接获取原始播放量,可通过两种途径解决:

1. 官方合规渠道:Spotify for Artists API

若你是歌曲艺人或其授权团队成员,可申请Spotify for Artists的API访问权限。该API提供艺人旗下作品的精确播放量、听众数据等,需通过官方艺术家后台完成账号关联后调用对应接口(如/artists/{artist_id}/stats)获取歌曲级播放数据。

2. 第三方聚合API

无艺人权限时,推荐使用Songstats、Chartmetric等第三方数据平台:

  • 这类平台整合了Spotify、Apple Music、YouTube等多平台数据,仅需一个API密钥即可批量获取播放量;
  • 相比单独订阅Apple Music开发者计划,打包服务更划算,且无需维护多平台授权逻辑。

二、Apple Music与YouTube播放量的高效获取

1. Apple Music

跳过付费开发者订阅的最优选择是使用第三方聚合API(如Songstats):

  • 平台已预先完成Apple Music授权对接,无需自行申请开发者账号;
  • 提供歌曲ID或ISRC编码即可直接获取播放量数据。

2. YouTube

使用YouTube Data API(免费额度足够日常使用):

  • 每日提供10000单位免费请求额度,满足批量更新需求;
  • 通过视频ID调用videos.list接口,可直接获取精确播放量。

三、脚本优化与自动更新实现

1. 多平台数据获取扩展(示例)

在现有脚本基础上,加入YouTube播放量获取逻辑,同时增加请求重试机制提升稳定性:

import openpyxl
import requests
import base64
from requests.adapters import HTTPAdapter
from urllib3.util.retry import Retry

# 配置API密钥
SPOTIFY_CLIENT_ID = "你的Spotify Client ID"
SPOTIFY_CLIENT_SECRET = "你的Spotify Client Secret"
YOUTUBE_API_KEY = "你的YouTube API密钥"

# 创建带重试的请求会话
def create_session():
    session = requests.Session()
    retry_strategy = Retry(
        total=3,
        backoff_factor=1,
        status_forcelist=[429, 500, 502, 503, 504]
    )
    adapter = HTTPAdapter(max_retries=retry_strategy)
    session.mount("https://", adapter)
    return session

session = create_session()

# 获取Spotify访问令牌
def get_spotify_token():
    url = 'https://accounts.spotify.com/api/token'
    credentials = f"{SPOTIFY_CLIENT_ID}:{SPOTIFY_CLIENT_SECRET}"
    encoded_creds = base64.b64encode(credentials.encode('utf-8')).decode('utf-8')
    headers = {'Authorization': f'Basic {encoded_creds}'}
    data = {'grant_type': 'client_credentials'}
    response = session.post(url, data=data, headers=headers)
    return response.json().get('access_token')

# 获取Spotify热度
def get_spotify_popularity(uri, token):
    if not uri:
        return None
    track_id = uri.split(':')[-1]
    url = f'https://api.spotify.com/v1/tracks/{track_id}'
    headers = {'Authorization': f'Bearer {token}'}
    response = session.get(url, headers=headers)
    data = response.json()
    return data.get('popularity')

# 获取YouTube播放量
def get_youtube_views(video_id):
    if not video_id:
        return None
    url = f'https://www.googleapis.com/youtube/v3/videos?part=statistics&id={video_id}&key={YOUTUBE_API_KEY}'
    response = session.get(url)
    data = response.json()
    if data.get('items'):
        return int(data['items'][0]['statistics'].get('viewCount', 0))
    return 0

# 处理Excel文件
def update_excel(file_path):
    wb = openpyxl.load_workbook(file_path)
    ws = wb.active

    # 新增列标题
    ws.cell(row=1, column=4, value='Spotify热度')
    ws.cell(row=1, column=5, value='YouTube播放量')

    spotify_token = get_spotify_token()

    # 遍历数据行(假设列C=Spotify URI,列D=YouTube视频ID)
    for row in range(2, ws.max_row + 1):
        # 更新Spotify热度
        spotify_uri = ws.cell(row=row, column=3).value
        popularity = get_spotify_popularity(spotify_uri, spotify_token)
        if popularity is not None:
            ws.cell(row=row, column=4, value=popularity)
        
        # 更新YouTube播放量
        youtube_id = ws.cell(row=row, column=4).value
        views = get_youtube_views(youtube_id)
        ws.cell(row=row, column=5, value=views)

    wb.save(file_path)

if __name__ == '__main__':
    update_excel('streams.xlsx')

2. 每日自动更新配置

  • Windows:打开「任务计划程序」,创建定时任务,设置每日指定时间运行Python脚本;
  • Linux/macOS:编辑crontab配置,添加0 9 * * * python3 /绝对路径/脚本.py,实现每日9点自动执行;
  • 云函数:将脚本部署到AWS Lambda、腾讯云函数等平台,配置定时触发器,无需本地运行。

内容的提问来源于stack exchange,提问作者ppbb

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.12 09:14:54