使用Python从特定频道获取Twitch剪辑URL

时间:2018-10-31 13:07:07

标签: python json python-3.x twitch

我想使用Python从特定的Twitch频道中获取剪辑URL列表。

与本文提供的答案大致相同: How would I get all links, or clips, from a specific channel on Twitch in Python?

我已经尝试通过阅读并比较JSON响应(来自答案的来源和我想做的事情)来修改上述文章中的代码,但是我对JSON一点都不熟悉,并且它没有检索网址。

这是我讨厌的查询:

json_req = """[{"query":"query ClipsCards__User($displayName: String!, 
$limit: Int, $criteria: GameClipsInput) { user(login: $displayName) { id 
clips(first: $limit, criteria: $criteria) { pageInfo { hasNextPage 
__typename } edges { cursor node { id slug url embedURL title viewCount 
language curator { id login displayName __typename } game { id name 
boxArtURL(width: 52, height: 72) __typename } broadcaster { id login 
displayName __typename } thumbnailURL createdAt durationSeconds __typename } 
__typename } __typename } __typename } } ","variables": 
{"displayName":"empressyenni","limit":100,"criteria": 
{"filter":"LAST_DAY"}},"operationName":"ClipsCards__User"}]"""

r = requests.post(url, data=json_req, headers={"client- 
id":"kimne78kx3ncx6brgo4mv6wki5h1ko"})
r_json = r.json()

edges = r_json[0]['data']['user']['clips']['edges']
urls = [edge['node']['url'] for edge in edges]

for url in urls:
    print(url)

这是有效的代码,但适用于游戏类别,而不是单个频道:

json_req = """[{"query":"query ClipsCards__Game($gameName: String!, $limit: 
Int, $cursor: Cursor, $criteria: GameClipsInput) { game(name: $gameName) { 
id clips(first: $limit, after: $cursor, criteria: $criteria) { pageInfo { 
hasNextPage __typename } edges { cursor node { id slug url embedURL title 
viewCount language curator { id login displayName __typename } game { id 
name boxArtURL(width: 52, height: 72) __typename } broadcaster { id login 
displayName __typename } thumbnailURL createdAt durationSeconds __typename } 
__typename } __typename } __typename } } ","variables":{"gameName":"League 
of Legends","limit":100,"criteria":{"languages":[],"filter":"LAST_DAY"}
,"cursor":"MjA="},"operationName":"ClipsCards__Game"}]"""

r = requests.post(url, data=json_req, headers={"client- 
id":"kimne78kx3ncx6brgo4mv6wki5h1ko"})
r_json = r.json()

edges = r_json[0]['data']['user']['clips']['edges']
urls = [edge['node']['url'] for edge in edges]

for url in urls:
    print(url)

0 个答案:

没有答案