dfreelon / pyktok

A simple module to collect video, text, and metadata from Tiktok.
BSD 3-Clause "New" or "Revised" License
315 stars 44 forks source link

Example code does not work #56

Closed SmartestDolphin closed 4 weeks ago

SmartestDolphin commented 1 month ago

Hello, I'm have issues trying to use this package. After a fresh install with the pip command and after installing each package separately, I'm trying to run one of the examples. I tried to run both with the app.py and from a python script. If I try to run this code

import pyktok as pyk
pyk.specify_browser('firefox')

pyk.save_tiktok('https://www.tiktok.com/@tiktok/video/7106594312292453675?is_copy_url=1&is_from_webapp=v1',
            True,
                'video_data.csv',
        'firefox')

I get this error:

File ".....\.venv\Lib\site-packages\pyktok\pyktok.py", line 237, in save_tiktok
    video_id = list(tt_json['ItemModule'].keys())[0]
                    ~~~~~~~^^^^^^^^^^^^^^
KeyError: 'ItemModule'

If I open firefox on my machine and go to tiktok.com I am already logged in.

Any help would be appreciated, thanks!

dfreelon commented 1 month ago

I ran your code and it worked for me. I'm assuming you're running ver 0.0.26. If so, please provide the output of the following two statements:

pyk.get_tiktok_json('https://www.tiktok.com/@tiktok/video/7106594312292453675?is_copy_url=1&is_from_webapp=v1')

pyk.alt_get_tiktok_json('https://www.tiktok.com/@tiktok/video/7106594312292453675?is_copy_url=1&is_from_webapp=v1')

SmartestDolphin commented 1 month ago

I am running version 0.0.26 installed via pip. The output of pyk.get_tiktok_json('https://www.tiktok.com/@tiktok/video/7106594312292453675?is_copy_url=1&is_from_webapp=v1', browser_name='firefox') is a dictionary with this structure (if you need values nested more deeply let me know)

{
    "AppContext": {
        "appContext": "...",
        "initialized": "...",
        "lang": "...",
        "sideNavActive": "..."
    },
    "BizContext": {
        "bizContext": "...",
        "initialized": "..."
    },
    "LoginContextModule": {
        "initialized": "...",
        "phoenix": "...",
        "platformConfig": "...",
        "emailSuffixConfig": "...",
        "previousModalType": "...",
        "loginTitle": "...",
        "loginSlogan": "...",
        "signUpTitle": "...",
        "signUpSlogan": "...",
        "aid": "...",
        "disableSignup": "...",
        "isSecurityVerification": "...",
        "hideHomePage": "...",
        "loginType": "...",
        "redirectUrl": "...",
        "isModal": "...",
        "hideCloseButton": "...",
        "hideToggleLoginSign": "...",
        "targetAid": "...",
        "noCtaPopup": "...",
        "standaloneAid": "...",
        "signupConfig": "...",
        "forceLogin": "...",
        "emailConsent": "...",
        "ttpConfig": "...",
        "preventSignupText": "...",
        "lastLoginMethod": "..."
    }
}

The ouput of pyk.alt_get_tiktok_json('https://www.tiktok.com/@tiktok/video/7106594312292453675?is_copy_url=1&is_from_webapp=v1', browser_name='firefox') is:

{
    "__DEFAULT_SCOPE__": {
        "webapp.app-context": "...",
        "webapp.i18n-translation": "..."
    }
}

I should mention that I'm running on Windows 11, Python 3.11

dfreelon commented 1 month ago

Yeah, I'm gonna need the complete unabridged output if you want help. Also, please go into your local version of the package and reproduce line 237 of pyktok.py for me, as what you posted in the error does not match line 237 in the current version.

SmartestDolphin commented 1 month ago

Hi, to be sure I just did pip uninstall pyktok followed by pip install pyktok (version 0.0.26)

The code that I get from pypy still gives me the same error

here is a screenshot of the portion you asked for:

image

I see that in the repository source code on line 237 there is no code at all so maybe the version on pypy is not up to date?

dfreelon commented 1 month ago

Yes, I recently merged an external PR but didn't update the PyPI version to match, so am doing that now. Thanks for pointing that out, but it won't fix your problem. Based on the output you provided, it looks like TikTok is giving you something different than it's giving me. The next step is to browse to a TikTok video page, "View Source," and send me the entire source HTML document. I suggest posting a cloud link here, or looking up my email and sending it to me that way.

dfreelon commented 4 weeks ago

Closed due to lack of activity.