Skip to content

if service fails than use next service #36

@TrajanWJ

Description

@TrajanWJ

I have no idea if this is necessary or not

from tiktok_downloader import Tikmate, snaptik, tikdown, VideoInfo, ttdownloader, tikwm, mdown


class TikScrape:
    def __init__(self):
        self.downloadtypes = [Tikmate, snaptik, tikdown, VideoInfo, ttdownloader, tikwm, mdown]

    def downloadTok(self, link, path, type=0):

        if type >= len(self.downloadtypes): # if the type is not in the list of download types, raise an error
            # raise Exception("Invalid service selected OR every service failed, exiting.")
            return False
        
        try:
        
            loader = self.downloadtypes[type]() # load the service selected in type, initally "Tikmate", then the rest of the services in self.downloadtypes, which are listed in order of reliability
            vidInfo = loader.get_media(link)

            if vidInfo != []: # if the service returned a video, download the video and return True

                vidInfo[0].download(path)
                return True
            
            else:
                raise Exception(f"Service {self.downloadtypes[type].__name__} returned nothing, possibly due to rate limiting or malformated link.")
                # if the service returned nothing, raise an error to try the next one

        except Exception as e: # if the service failed, try the next one

            print(f"Service {self.downloadtypes[type].__name__} failed, automatically retrying with service {self.downloadtypes[type+1].__name__}. \nReason error: {e}")
            # say it failed, then try the next one
            self.downloadTok(link, path, type=type+1)

        
x = TikScrape()
x.downloadTok("LINK", "tiktok.mp4")

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions