roju / tiktok-live-recorder

A Python script for recording TikTok live streams
MIT License
16 stars 2 forks source link

The script no longer works (ERROR - room_id not found) #32

Open CyberbobGR opened 1 month ago

CyberbobGR commented 1 month ago

It looks like TikTok changed something on its side and the script no longer works with username. It just shows "ERROR - room_id not found".

Marksmanship256 commented 1 month ago

try it

    def get_room_id_from_user(self) -> str:
        """Given a username, get the room_id"""
        try:
            response = self.req.get(
                f'https://www.tiktok.com/@{self.user}/live',
                allow_redirects=False, headers=bot_utils.headers, cookies=self.cookies)
            # logging.info(f'get_room_id_from_user response: {response.text}')
            if response.status_code == StatusCode.REDIRECT:
                raise errors.Blacklisted('Redirect')

            match = re.search(r'room_id=(\d+)', response.text)
            if match:
                return match.group(1)

            match = re.search(r'"roomId":"(\d+)"', response.text)
            if match:
                return match.group(1)

            raise ValueError('room_id not found')

        except (req.HTTPError, errors.Blacklisted) as e:
            raise errors.Blacklisted(e)
        except AttributeError as e:
            raise errors.UserNotFound(f'{ErrorMsg.USERNAME_ERROR}\n{e}')
        except ValueError as e:
            raise e
        except Exception as ex:
            raise errors.GenericReq(ex)

If your cookies aren't loaded from a file but from headers in bot_utils.py, then this is:

    def get_room_id_from_user(self) -> str:
        """Given a username, get the room_id"""
        try:
            response = self.req.get(
                f'https://www.tiktok.com/@{self.user}/live',
                allow_redirects=False, headers=bot_utils.headers)
            # logging.info(f'get_room_id_from_user response: {response.text}')
            if response.status_code == StatusCode.REDIRECT:
                raise errors.Blacklisted('Redirect')

            match = re.search(r'room_id=(\d+)', response.text)
            if match:
                return match.group(1)

            match = re.search(r'"roomId":"(\d+)"', response.text)
            if match:
                return match.group(1)

            raise ValueError('room_id not found')

        except (req.HTTPError, errors.Blacklisted) as e:
            raise errors.Blacklisted(e)
        except AttributeError as e:
            raise errors.UserNotFound(f'{ErrorMsg.USERNAME_ERROR}\n{e}')
        except ValueError as e:
            raise e
        except Exception as ex:
            raise errors.GenericReq(ex)
broklin48 commented 1 month ago

try it

thanks bro, it works

Naderrnawar commented 1 month ago

try it How can I make this work? iam using phone

jean75000 commented 1 month ago

@Marksmanship256 I would like to load my cookie.json but I don't know what to modify in the script. Have you modified the script to load your authentication cookie ? Thanks

Marksmanship256 commented 1 month ago

@Marksmanship256 I would like to load my cookie.json but I don't know what to modify in the script. Have you modified the script to load your authentication cookie ? Thanks

@jean75000 I use the Chrome extension Get cookies.txt LOCALLY to export cookies in Netscape format. Then you gotta make these edits to the tiktok_bot.py file.

Make sure to back up your tiktok_bot.py file before making these changes.

  1. Add this line in __init__ func: (absolute path to the cookies.txt file)

    self.cookies = self.load_cookies_from_netscape_file('С:/tiktok-live-recorder/www.tiktok.com_cookies.txt')

    2024-07-10_125502

  2. Go through the whole file, search for headers=bot_utils.headers, and in the arguments, right after headers=bot_utils.headers, add cookies=self.cookies, separated by commas, in each request (in 4 places).

1

2

3

4

  1. Then, at the end of the file, right after the get_user_from_room_id function, add this load_cookies_from_netscape_file function to read your cookie file.
    def load_cookies_from_netscape_file(self, filepath):
        """Load cookies from a Netscape format cookie file."""
        cookies = {}
        try:
            with open(filepath, 'r') as file:
                for line in file:
                    if not line.startswith('#') and line.strip():
                        parts = line.strip().split('\t')
                        if len(parts) == 7:
                            domain, flag, path, secure, expiration, name, value = parts
                            cookies[name] = value
        except FileNotFoundError as e:
            logging.error(f"Cookie file not found: {e}")
        except Exception as e:
            logging.error(f"Error reading cookie file: {e}")
        return cookies

2024-07-10_125049

How can I make this work? iam using phone

@Naderrnawar Someone needs to update the repo with these changes, or you gotta figure out how to make these changes yourself in your phone.

broklin48 commented 1 month ago

@jean75000 I use the Chrome extension Get cookies.txt LOCALLY to export cookies in Netscape format. Then you gotta make these edits to the tiktok_bot.py file.

Make sure to back up your tiktok_bot.py file before making these changes.

@Marksmanship256 i got some error

image

Marksmanship256 commented 1 month ago

@jean75000 I use the Chrome extension Get cookies.txt LOCALLY to export cookies in Netscape format. Then you gotta make these edits to the tiktok_bot.py file. Make sure to back up your tiktok_bot.py file before making these changes.

@Marksmanship256 i got some error

image

The TabError: inconsistent use of tabs and spaces in indentation error indicates that your code uses both tabs and spaces for indentation, which Python does not allow. All indentation in Python code must be consistent, using either tabs or spaces (spaces are recommended).

Method 1: Using a Text Editor or IDE

  1. VSCode: Open the file in VSCode. Press Ctrl+Shift+P to open the command palette. Type Convert Indentation to Spaces and select the command

  2. PyCharm: Open the file in PyCharm. From the menu, choose Code -> Reformat Code or press Ctrl+Alt+L.

  3. Sublime Text: Open the file in Sublime Text. Select View -> Indentation -> Convert Indentation to Spaces.

Method 2: Manual Fix

If you want to fix the issue manually, open your script in a text editor and ensure that all indentations are done in a consistent manner. For example, use only spaces (usually 4 spaces per indentation level).

TBH, the console shows you everything you need to fix the error; line 36 probably has the wrong indent.

jean75000 commented 1 month ago

@Marksmanship256 Thanks 👍

broklin48 commented 1 month ago

The TabError: inconsistent use of tabs and spaces in indentation error indicates that your code uses both tabs and spaces for indentation, which Python does not allow. All indentation in Python code must be consistent, using either tabs or spaces (spaces are recommended).

TBH, the console shows you everything you need to fix the error; line 36 probably has the wrong indent.

Ok, thanks. Fixed.

Naderrnawar commented 1 month ago

@roju can you please update your script with those changes

emefbiemef commented 1 month ago

@Marksmanship256 I added your code but the room_id error still persists. Is my tiktok id banned? I can view the live on the mobile app and web just fine though