-
-
Save Juice805/5603e03e0162de61217af59354c25376 to your computer and use it in GitHub Desktop.
#! /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) |
added a fix for long file names
Thank you, this worked perfectly! All my Public vids are downloaded now. all 1247 :)
You know if there is a way to get unlisted videos some way?
Hey, thanks so much for this!!
Though I get this error :/
Traceback (most recent call last):
File "C:\Plays\123.py", line 40, in <module>
get_playstv_videos("<playstv user guid>")
File "C:\Plays\123.py", line 19, in get_playstv_videos
batch = load(urlopen(f"https://plays.tv/playsapi/feedsys/v1/userfeed/{user_id}/uploaded?limit=200&filter=&lastId={last_id}"))
File "C:\Users\kagst\AppData\Local\Programs\Python\Python38\lib\urllib\request.py", line 222, in urlopen
return opener.open(url, data, timeout)
File "C:\Users\kagst\AppData\Local\Programs\Python\Python38\lib\urllib\request.py", line 525, in open
response = self._open(req, data)
File "C:\Users\kagst\AppData\Local\Programs\Python\Python38\lib\urllib\request.py", line 542, in _open
result = self._call_chain(self.handle_open, protocol, protocol +
File "C:\Users\kagst\AppData\Local\Programs\Python\Python38\lib\urllib\request.py", line 502, in _call_chain
result = func(*args)
File "C:\Users\kagst\AppData\Local\Programs\Python\Python38\lib\urllib\request.py", line 1360, in https_open
return self.do_open(http.client.HTTPSConnection, req,
File "C:\Users\kagst\AppData\Local\Programs\Python\Python38\lib\urllib\request.py", line 1317, in do_open
h.request(req.get_method(), req.selector, req.data, headers,
File "C:\Users\kagst\AppData\Local\Programs\Python\Python38\lib\http\client.py", line 1230, in request
self._send_request(method, url, body, headers, encode_chunked)
File "C:\Users\kagst\AppData\Local\Programs\Python\Python38\lib\http\client.py", line 1241, in _send_request
self.putrequest(method, url, **skips)
File "C:\Users\kagst\AppData\Local\Programs\Python\Python38\lib\http\client.py", line 1092, in putrequest
self._validate_path(url)
File "C:\Users\kagst\AppData\Local\Programs\Python\Python38\lib\http\client.py", line 1183, in _validate_path
raise InvalidURL(f"URL can't contain control characters. {url!r} "
http.client.InvalidURL: URL can't contain control characters. '/playsapi/feedsys/v1/userfeed/<playstv user guid>/uploaded?limit=200&filter=&lastId=9ccc3c4ba7a053cd626cd0c33b7d7f3c' (found at least ' ')
>>>
@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
@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!!
Inherited the instructions, but I'll update them soon
Inherited the instructions, but I'll update them soon
Great!
Also is there a way to add the upload date to the title?
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.
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.
Love this. I already have my files but I'm downloading them all again to get the metadata :) Excellent work
Sorry guys but can somebody describe it how can I open this .py file on my windows?
Thanks in advance
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.
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.
added second file that will properly set the creation time on Mac for downloaded videos