Watch2Gether API Documentation

Hey… ‘Accept’ and 'Content-Type" are headers and should not be part of the body object. Check out this post, it includes a working Python example: Watch2Gether API Documentation - #103 by user_3687e5658d466be

but that wont be the problem here, because it works in the first example

Hi @user_95f53b7185f71fb
I want to expand my sync project for listening to music together a bit.
Is it possible to change the playlist I am adding to in the API? Afaik I can only add to the current playlist, for which I don’t have a way of knowing whích playlist it currently is.
So basically instead of only having current as an option for playlists I can have use a key or name for the playlist I want to add a video to. Would be awesome to have that feature!

"https://w2g.tv/rooms/{W2G_STREAM_LINK}/playlists/{PLAYLIST_NAME_OR_KEY}/playlist_items/sync_update"

Thanks for your input. This is not yet officially supported. You can use a playlist key instead of “current” in the request but you would have to figure out this key by yourself in the browser console for example… and i can’t really guarantee that this will always work in the future.

Hello, im having some trouble with adding videos into a rooms playlist. Heres my code (python):

headers = {
            "Accept": "application/json",
            "Content-Type": "application/json",
}
url = f"https://w2g.tv/rooms/{streamkey}/playlists/current/playlist_items/sync_update"
items = []
params = {"w2g_api_key": "<api_key>"}
for video in vids[1:]:
    items.append({"url": video.watch_url, "title": video.title, "thumb": video.thumbnail_url})
params["add_items"] = items
print(params)
res = requests.post(url, headers=headers, params=params)
print(res)

The two prints return:

{
   'w2g_api_key': '<my_api_key>',
   'add_items': [
      {'url': 'https://www.youtube.com/watch?v=dQw4w9WgXcQ', 
       'title': 'Rick Astley - Never Gonna Give You Up (Official Music Video)', 
       'thumb': 'https://i.ytimg.com/vi/dQw4w9WgXcQ/hq720.jpg'}, 
      {'url': 'https://www.youtube.com/watch?v=34Ig3X59_qA', 
       'title': 'Never Gonna Give You Up - Rick Astley (Lyrics) 🎵', 
       'thumb': 'https://i.ytimg.com/vi/34Ig3X59_qA/hq720.jpg'}
   ]
}

and

<Response [500]>

is there something wrong with my request?

Hmm… i’m not a Python expert but i think the body of the post request is set with “data” and not with “params”. Does that help?

Hey Watch2Gether developers. Please update your code to the new api hostname api.w2g.tv. Just using “w2g.tv” will stop working soon!

For example:

https://w2g.tv/rooms/create.json

should become

https://api.w2g.tv/rooms/create.json

Thanks!

Hello, is there a way to get information about currently existing items in playlists?

At the moment there is no official API method that supports this. But thanks for the input. it would be a good addition!

1 Like

@user_95f53b7185f71fb @user_387276a85c2f21d
I have a python script that parses the current playlist and puts all the data into a usable CSV file. You could modify this into a pandas dataframe instead of a CSV file

from bs4 import BeautifulSoup
import re
import csv

date = "10_12_21"


def main():
    with open(f"w2g_html_files/w2g_{date}.html", "r", encoding="utf-8") as w2g_html:
        soup = BeautifulSoup(w2g_html, "html.parser")
        videos_dict = {}
        for video_hash in soup.find_all("img", attrs={"data-w2g": "['$parent.playClick', ['event', 'click']], ['thumb', ['attr', 'src']]"}):
            new_video_key = re.search("(.*)/vi/(.*)/(.*)", video_hash["src"]).group(2)
            video_title = video_hash.find_next('div', attrs={"class": "w2g-list-item-title mod-player"}).text
            videos_dict[f"https://www.youtube.com/watch?v={new_video_key}"] = (video_title, video_hash["src"])
        # each key in dict is youtube_url : Tuple( video title, video icon url )
        create_csv_file_from_dict(videos_dict)


def create_csv_file_from_dict(videos_dict):
    csv_file = f"w2g_csv_files/w2g_{date}.csv"
    try:
        with open(csv_file, "w", encoding="utf-8", newline='') as csv_file:
            writer = csv.writer(csv_file)
            writer.writerow(["URL", "Youtube Title", "Youtube Icon Url", "Artist"])
            for key, value in videos_dict.items():
                writer.writerow([key, value[0], value[1]])
    except IOError:
        print("I/O Error")


if __name__ == "__main__":
    main()
Footer
© 2022 GitHub, Inc.
Footer navigation
Terms
  1. Copy and paste the HTML code from w2g website into a new file in the format w2g_<date>.html in the w2g_html_files directory
  2. In the file html_to_csv.py change the date at the top to correspond to step 1
  3. Run python html_to_csv.py. This creates a csv file of all the w2g videos

Hey there,
even though its a late reply. For anyone struggling to implement a playlist with Python.
The key is that a Python list is not equal to the JavaScript list expected from the server. That’s why you get a 500 response.
To fix that you can import json and convert the Python list:

> import json
headers = {
            "Accept": "application/json",
            "Content-Type": "application/json",
}
url = f"https://w2g.tv/rooms/{streamkey}/playlists/current/playlist_items/sync_update"
items = []
params = {"w2g_api_key": "<api_key>"}
for video in vids[1:]:
    items.append({"url": video.watch_url, "title": video.title, "thumb": video.thumbnail_url})
items=json.dumps(items)
params["add_items"] = items
print(params)
res = requests.post(url, headers=headers, params=params)
print(res)
1 Like

Thanks for your input!

It would be very nice, to have these pause play delete API calls for consumers as well :pray:
Please think about :innocent:

You mean you would like to control the current video playback of a room?

I’m building a telegram bot for a watch party style of game that some friends invented, and I was wondering if it’s possible to set things like Additional Settings > Turn off suggested videos via the API when creating a room? I don’t see that in the documentation at the top of this post, but I thought I would ask if perhaps there are additional body parameters that have been added/updated for PRO users?

Thanks a lot for your feedback. I agree, this would be a nice addition. Can you explain a bit more in detail what you are going to build?

Sure! So we have this party game called YouTube Roulette where basically we randomly generate youtube search terms and choose from the first results and watch whatever we get. then we vote on the best video of the round, and the process repeats until someone gets three points. Usually we play in person but the telegram bot will be designed to be able to play remotely and I’m planning to use the W2G API to create and add videos to the room with the telegram bot. Since I have a premium account, I’d like to be able to set some of the premium room features when creating the room with the API :slight_smile:

Thanks for the info. That sounds like a cool concept and a fun game to play. I look into the API this month and check what options in can include additionally!

Awesome! I figure any of the room option toggles and such would be good ones to include in the API :slight_smile: Looking forward to seeing what you come up with!