wkaisertexas / tiktok-uploader

Automatically ⬆️ upload TikTok videos
https://pypi.org/project/tiktok-uploader/
352 stars 84 forks source link

_clear(desc) Not Clearing Whole Caption Text Box If Title To To Long #131

Closed Jamadoo closed 4 months ago

Jamadoo commented 4 months ago

This is a issue relating to @callum-fortune 's new Pull-Request

On Lines

    # desc populates with filename before clearing
    WebDriverWait(driver, config['explicit_wait']).until(lambda driver: desc.text != '')

    _clear(desc)

    WebDriverWait(driver, config['explicit_wait']).until(lambda driver: desc.text == '')

If the title of the file is longer then the half of the text box, only the text before the half of the text box is removed. Considor sending "Keys.End" before calling clear

    # desc populates with filename before clearing
    WebDriverWait(driver, config['explicit_wait']).until(lambda driver: desc.text != '')

    desc.send_keys(Keys.END)
    _clear(desc)

    WebDriverWait(driver, config['explicit_wait']).until(lambda driver: desc.text == '')

Works great in my experience.

I also noticed a log is send in the console of the browser ({isSameSearchValue: false}) each time the dialog pops up showcasing the available hashtags and mentions. We could use a wait until this log gets written instead of

if word[0] == "#":
                desc.send_keys(word)
                desc.send_keys(' ' + Keys.BACKSPACE)
                time.sleep(config['implicit_wait']) # Here we would add the wait until the log gets written, aka the menu gets opened. Which will speed up the hashtag adding progress a ton, because the menu pops up between 0.5-2 seconds in my experience. Thus, here we wont wait any  unnecessary time 
                desc.send_keys(Keys.ENTER)
Jamadoo commented 4 months ago

@Jamadoo I have bad news, the problem seems to have persisted. I passed in @bbc and it uploaded with something totally wrong

You can see the username that it ended up uploading:

image

what seems strange is the first user in the list of user which tiktok gives is anthony... How the hell does tiktok get that?? The username string is the username of the first div child, aka the top user in the list then the search string is the user we are looking for.

And as we can see in your screenshot, tiktok put "anthony" as the top user... That seems like a tiktok problem not a code problem?

callum-fortune commented 4 months ago

@Jamadoo I know right I think his username has TikTok in it and they match both when searching

Jamadoo commented 4 months ago

@callum-fortune but thats suppose to be a issue on tiktok's side? What we can do is, we loop through all the children of the div. Check the username. If the user-ids match, we click the element. And after 5 seconds, we just press enter to select the first element

What happened in your test with the new code?

callum-fortune commented 4 months ago

@Jamadoo This time I got @cooplive returned lol. Not sure whats going on, I'm debugging it now

Jamadoo commented 4 months ago

@callum-fortune are you running on normal mod (not headless)? What does tiktok give in the list of usernames when the bot enters bbc?

callum-fortune commented 4 months ago

@Jamadoo Yeh I'm not running headless at the moment so I can see whats happening. Initially it was giving random users that I follow so i added a time.sleep between sending the space and backspace. Then it started returning bbc accounts but still selected @bbcnews instead of @bbc. Weird...

Jamadoo commented 4 months ago

try increasing the timeout to like 30 secs and remove your sleep. See if the users get updated

Jamadoo commented 4 months ago

i created the code which loops through all the shown users. Im testing it rn

callum-fortune commented 4 months ago

@Jamadoo I was literally just coming to say that, you were only checking the first user

Jamadoo commented 4 months ago

i think i got it wokring. Still debugging @callum-fortune

callum-fortune commented 4 months ago

@Jamadoo I think the reason you couldn't reproduce is because the top recommendations are people that you follow, I assume you are using a tiktok account that follows nobody but I follow many people.

A also have a solution that appears to work, will be interesting to see how they compare πŸ‘€

Jamadoo commented 4 months ago

@callum-fortune yes, exactly. Im using a account with no followers. I could quickly test with my main

What your solution?

callum-fortune commented 4 months ago

@Jamadoo My solution counts the iterations in the loop and when it finds the correct match, it sends the down key that many times to navigate down the dropdown and select the correct user

Jamadoo commented 4 months ago

@callum-fortune Im simply getting all the user-id classes and looping through them. Heres my code

            elif word[0] == "@":
                logger.debug(green('- Adding Mention: ' + word))
                desc.send_keys(word)
                desc.send_keys(' ' + Keys.BACKSPACE)
                # Wait For Popup
                Popup = driverWait.until(EC.presence_of_element_located(
                    (By.XPATH, config['selectors']['upload']['mention_box'])
                ))
                # Wait For Div With Users
                Div = driverWait.until(
                    lambda driver: Popup.find_element(By.XPATH, "./div[not(@*)]")
                )
                # Wait until it has at least two children
                driverWait.until(
                    lambda driver: len(Div.find_elements(By.XPATH, "./*")) >= 2
                )
                ## Wait For Correct User To Appear
                # Variable indicating whether the loop should continue
                found = False
                waiting_interval = 0.5
                timeout = 5
                start_time = time.time()
                # Loop until the desired condition is met
                while not found and (time.time() - start_time < timeout):
                    # Get all children with 'user-id' class
                    user_id_elements = Div.find_elements(By.CLASS_NAME, "user-id")

                    for user_id_element in user_id_elements:
                        if user_id_element and user_id_element.is_enabled:
                            # If elements are found, check the first one
                            first_user_id = user_id_elements[0]

                            # Get the inner text
                            first_user_id_text = first_user_id.text
                            username = first_user_id_text.split(" Β· ")[0]

                            # Compare with the target word
                            if username.lower() == word[1:].lower():
                                found = True
                                user_id_element.click()
                                print("Matching User found : Clicking User")
                                break
                    if not found:
                        # If not matching, wait and repeat
                        print(f"No match. Waiting for {waiting_interval} seconds...")
                        time.sleep(waiting_interval)  # Pause for the specified interval
                if not found:
                    logger.debug(green('- Selecting First User'))
                    desc.send_keys(Keys.ENTER)
Jamadoo commented 4 months ago

@Jamadoo My solution counts the iterations in the loop and when it finds the correct match, it sends the down key that many times to navigate down the dropdown and select the correct user

so you loop through the user-id and count how many down is the needed user and the press down arrow that many times? Im geussing you dont know you can just click that element, right? πŸ˜‚

callum-fortune commented 4 months ago

@Jamadoo Its getting late ok lmao. Nah I didn't realise the actual element object was being passed down so didn't think to click it

Jamadoo commented 4 months ago

@callum-fortune dont worry, its also 10pm for me

Jamadoo commented 4 months ago

im starting to loose all my focus as well πŸ˜‚

Jamadoo commented 4 months ago

last test run, then i need to do some work for my classes tmr. And also summarize the last couple pages of science chapter for tmr πŸ˜‚

callum-fortune commented 4 months ago

@Jamadoo Where are you actually located? its 9pm here. I'm just implementing your code on my side and I'll get it all cleaned up and pushed to that PR

Jamadoo commented 4 months ago

@callum-fortune Im from South Africa. and u?

Great, so im guessing the script works for situation?

callum-fortune commented 4 months ago

@Jamadoo I'm in the UK. Yeh it pretty much works now just a few touches, I'll make sure to co-author you in the PR

Jamadoo commented 4 months ago

i just tested the script on my main, works like excepted as well. I did notice my US account and SA account's UI was different. That could also be a factor for why random users popup for you. @callum-fortune

Jamadoo commented 4 months ago

@Jamadoo I'm in the UK. Yeh it pretty much works now just a few touches, I'll make sure to co-author you in the PR

alright, sounds good. Thanks for your help. Ima leave the rest to you

callum-fortune commented 4 months ago

@Jamadoo Cheers, have a good one!

Jamadoo commented 4 months ago

@callum-fortune Thanks, you to 🍻