Skip to content

Instantly share code, notes, and snippets.

@Juice805
Forked from st0le/PlaysTvDownloader.ps1
Last active December 7, 2019 23:29
Show Gist options
  • Save Juice805/5603e03e0162de61217af59354c25376 to your computer and use it in GitHub Desktop.
Save Juice805/5603e03e0162de61217af59354c25376 to your computer and use it in GitHub Desktop.
Downloads all public videos of a user
#! /usr/bin/python3
# download all PlaysTv videos of a user
# To find the user id, navigate to the your profile while logged in (IMPORTANT!)
# View source of the page, In the <html> tag there's data-conf attribute.
# The json in there will have the user id under [login_user.id]
user_id = "<playstv user guid>"
# Replace the <playstv user guid> above with your ID
from re import sub
from json import load
from urllib.request import urlretrieve, urlopen
import time
try:
from mutagen.mp4 import MP4
except ImportError:
print("mutagen not installed. Ignoring metadata.")
MP4 = None
def is_windows():
import platform
return platform.system() == 'Windows'
if is_windows():
try:
from win32_setctime import setctime
except ImportError:
print("win32-setctime is not installed. Not setting creation time.")
setctime = None
def set_creation_time(filename, ms_epoch):
created = ms_epoch / 1000
if is_windows():
if setctime:
setctime(filename, created)
else:
import os
stamp = time.strftime('%m/%d/%Y %H:%M:%S', time.localtime(created))
command = 'SetFile -d \"' + stamp + '\" \"' + filename + '\"'
os.system(command)
def update_metadata(filename, title, game):
if MP4:
mp4 = MP4(filename)
mp4["\xa9nam"] = title
mp4["desc"] = game
mp4.save()
def safe_title(index, title):
only_chars = sub(r'[^\w]+', '_', title).strip("_")
return f"{index} - {only_chars}.mp4"[:255]
def get_playstv_videos(user_id):
last_id = ""
items = []
while last_id != None:
batch = load(urlopen(f"https://plays.tv/playsapi/feedsys/v1/userfeed/{user_id}/uploaded?limit=200&filter=&lastId={last_id}"))
items.extend(batch["items"])
last_id = batch["lastId"]
print(len(items))
for index,item in enumerate(items, start=1):
description = item["description"]
filename = safe_title(index,description)
if "gameTitle" in item:
game = item["gameTitle"]
else:
game = ""
if "downloadUrl" in item:
url = item["downloadUrl"]
else:
url = item["src"]
print(filename, url)
try:
urlretrieve(url, filename)
except Exception as e:
print("Failed to download.")
print(e)
continue
try:
set_creation_time(filename, item["created"])
update_metadata(filename, description, game)
except Exception as e:
print("Failed to update metadata.")
print(e)
continue
if __name__ == "__main__":
get_playstv_videos(user_id)
@kagstrom2100
Copy link

@kagstrom2100 You'll need to replace <playstv user guid> with your user id. Instructions to find that ID are in a comment near the top of the script

Ahh that worked better. As the instructions where a bit unclear I though you needed to put in in the last_id = "" variable. Maybe update the instructions to avoid misunderstandings?

Anyways, many many thanks for this script!!

@Juice805
Copy link
Author

Inherited the instructions, but I'll update them soon

@kagstrom2100
Copy link

Inherited the instructions, but I'll update them soon

Great!

Also is there a way to add the upload date to the title?

@Juice805
Copy link
Author

Juice805 commented Nov 21, 2019

Updated the instructions. Also now adds the original title and game name into the metadata of the MP4 file if mutagen in installed.

pip3 install mutagen

The Mac version sets the creation date of the file, I don't run this on a windows machine so if someone else has the equivalent for windows I can add it.

@Juice805
Copy link
Author

Juice805 commented Nov 22, 2019

Updated to condense it into 1 cross platform script for Windows and Mac. Unfortunately windows requires another dependency win32-setctime.

pip3 install win32-setctime

If you don't care about metadata the dependencies can be ignored.

@cainxxx
Copy link

cainxxx commented Nov 22, 2019

Love this. I already have my files but I'm downloading them all again to get the metadata :) Excellent work

@partzan
Copy link

partzan commented Nov 26, 2019

Sorry guys but can somebody describe it how can I open this .py file on my windows?
Thanks in advance

@partzan
Copy link

partzan commented Nov 26, 2019

Sorry guys but can somebody describe it how can I open this .py file on my windows?
Thanks in advance

Holy Jesus! that's a miracle, I went and studied all tutorials needed for a python newbie and finally it worked smoothly!
wonderful job, God Bless.

@jeremiahfallin
Copy link

Do you know of a way to download videos that other people have uploaded with you in them? They don't show up through that API call but are available on your profile.

I'm not sure where I would find plays.tv API endpoints. Their developer page doesn't have any listed as far as I can tell unless you have a registered app with them.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment