Add paging to videos command

This commit is contained in:
Ivan Habunek 2020-05-17 13:35:51 +02:00
parent 2118cd8825
commit ea01ef3d99
No known key found for this signature in database
GPG Key ID: CDBD63C43A30BB95
4 changed files with 52 additions and 17 deletions

View File

@ -7,6 +7,7 @@ Twitch Downloader change log
* Fix videos command (#18) * Fix videos command (#18)
* **Breaking**: `videos` command no longer takes the `--offset` parameter due to * **Breaking**: `videos` command no longer takes the `--offset` parameter due to
API changes API changes
* Add paging to `videos` command to replace offset
1.7.0 (2020-04-25) 1.7.0 (2020-04-25)
------------------ ------------------

View File

@ -16,23 +16,44 @@ from twitchdl.exceptions import ConsoleError
from twitchdl.output import print_out, print_video from twitchdl.output import print_out, print_video
def _continue():
print_out(
"\nThere are more videos. "
"Press <green><b>Enter</green> to continue, "
"<yellow><b>Ctrl+C</yellow> to break."
)
try:
input()
except KeyboardInterrupt:
return False
return True
def videos(channel_name, limit, sort, type, **kwargs): def videos(channel_name, limit, sort, type, **kwargs):
print_out("Loading videos...") print_out("Loading videos...")
videos = twitch.get_channel_videos(channel_name, limit, sort, type) generator = twitch.channel_videos_generator(channel_name, limit, sort, type)
count = len(videos["edges"])
total = videos["totalCount"]
if not count:
print_out("No videos found")
return
# TODO: paging
first = 1 first = 1
last = count
print_out("<yellow>Showing videos {}-{} of {}</yellow>".format(first, last, total))
for video in videos["edges"]: for videos, has_more in generator:
print_video(video["node"]) if "edges" not in videos:
break
count = len(videos["edges"]) if "edges" in videos else 0
total = videos["totalCount"]
last = first + count - 1
print_out("-" * 80)
print_out("<yellow>Showing videos {}-{} of {}</yellow>".format(first, last, total))
for video in videos["edges"]:
print_video(video["node"])
if not has_more or not _continue():
break
first += count
def _select_quality(playlists): def _select_quality(playlists):

View File

@ -7,7 +7,7 @@ from twitchdl import utils
START_CODES = { START_CODES = {
'bold': '\033[1m', 'b': '\033[1m',
'i': '\033[3m', 'i': '\033[3m',
'u': '\033[4m', 'u': '\033[4m',
'red': '\033[91m', 'red': '\033[91m',
@ -65,7 +65,7 @@ def print_video(video):
# Can't find URL in video object, strange # Can't find URL in video object, strange
url = "https://twitch.tv/{}".format(video["id"]) url = "https://twitch.tv/{}".format(video["id"])
print_out("\n<bold>{}</bold>".format(video["id"])) print_out("\n<b>{}</b>".format(video["id"]))
print_out("<green>{}</green>".format(video["title"])) print_out("<green>{}</green>".format(video["title"]))
print_out("<cyan>{}</cyan> playing <cyan>{}</cyan>".format(channel, game)) print_out("<cyan>{}</cyan> playing <cyan>{}</cyan>".format(channel, game))
print_out("Published <cyan>{}</cyan> Length: <cyan>{}</cyan> ".format(published_at, length)) print_out("Published <cyan>{}</cyan> Length: <cyan>{}</cyan> ".format(published_at, length))

View File

@ -81,13 +81,13 @@ def get_clip(slug):
return data["data"]["clip"] return data["data"]["clip"]
def get_channel_videos(channel_id, limit, sort, type="archive"): def get_channel_videos(channel_id, limit, sort, type="archive", after=None):
url = "https://gql.twitch.tv/gql" url = "https://gql.twitch.tv/gql"
query = """ query = """
{{ {{
user(login: "{channel_id}") {{ user(login: "{channel_id}") {{
videos(options: {{ }}, first: {limit}, type: {type}, sort: {sort}, after: "opaqueCursor") {{ videos(options: {{ }}, first: {limit}, type: {type}, sort: {sort}, after: "{after}") {{
totalCount totalCount
edges {{ edges {{
cursor cursor
@ -114,15 +114,28 @@ def get_channel_videos(channel_id, limit, sort, type="archive"):
query = query.format(**{ query = query.format(**{
"channel_id": channel_id, "channel_id": channel_id,
"after": after,
"limit": limit, "limit": limit,
"type": type.upper(),
"sort": sort.upper(), "sort": sort.upper(),
"type": type.upper(),
}) })
response = authenticated_post(url, json={"query": query}).json() response = authenticated_post(url, json={"query": query}).json()
return response["data"]["user"]["videos"] return response["data"]["user"]["videos"]
def channel_videos_generator(channel_id, limit, sort, type):
cursor = None
while True:
videos = get_channel_videos(channel_id, limit, sort, type, after=cursor)
cursor = videos["edges"][-1]["cursor"]
yield videos, cursor is not None
if not cursor:
break
def get_access_token(video_id): def get_access_token(video_id):
url = "https://api.twitch.tv/api/vods/{}/access_token".format(video_id) url = "https://api.twitch.tv/api/vods/{}/access_token".format(video_id)