kevinzg / facebook-scraper

Scrape Facebook public pages without an API key
MIT License
2.42k stars 630 forks source link

Set a new User_Agent everytime TemporarilyBanned occurs #386

Open LightMoon opened 3 years ago

LightMoon commented 3 years ago

Hi,

I have an array of different User Agents, I want to reset facebook scrpper User-Agent every time an exception of TemporarilyBanned occurs. I am keen to know if I have it implemented correctly? In other words, I want the User-Agent to remain to what has been changed for the other functions until the next TemporarilyBanned happens.

P.S: I'm new to python and still a lot to learn. You reckon what area I should read up to fully comprehend where my question is coming from. Is it related to python scoping/namespace topics?

from facebook_scraper import get_profile, get_friends, enable_logging, set_proxy, set_user_agent, set_cookies
from random import randint, uniform

def FacebookBanChecker (A_user):
    checker = None
    sleep_counter = 1
    while checker is None:
        try:
            logger.warning(f'Facebook Ban Checker has been called.')
            sleep_timer = randint(4000, 10000)
            sleep_timer = sleep_counter * sleep_timer
            sleep_counter += 0.2
            sleep_timer_value = '{0:.2g}'.format(sleep_timer / 3600)
            logger.warning(f'Temporarily banned on IP address of *** {get_current_proxy_ip()} ***, sleeping for {sleep_timer_value} Hours!')
            time.sleep(sleep_timer)
            logger.info('Sleep Timer is finished, It is the time to get new IP Address.')
            change_proxy_ip_address()
            logger.warning(f'The IP address has changed to *** {get_current_ip()} ***!')
            random.seed()
            New_User_AG = random.choice(User_Ag)
            set_user_agent(New_User_AG)
            logger.info('New User Agent has been set')
            # for user in Friend list:
            checker = get_profile(A_user, cookies="cookies.json")
            if checker is not None:
                return
        except facebook_scraper.exceptions.TemporarilyBanned:
            logger.warning('The Account is still Temporarily banned.')
            pass

def getProfileInfo(userId):
    profile = None
    while profile is None:
        try:
            # Get Profile info.
            profile = get_profile(userId, cookies="cookies.json")
            . 
            .
            .
            .
            return profile

        except facebook_scraper.exceptions.NotFound as e:
            logger.critical(f'{e} happened @ getProfileInfo')
            logger.debug(f'<<{userId}>> has no content in his/her profile @ getProfileInfo')
            profile = None
            return profile
            pass

        except IndexError as index_error:
            logger.critical(f'{index_error} happened @ getProfileInfo')
            logger.debug(f'<<{userId}>> has no info in his/her profile @ getProfileInfo')
            profile = None
            return profile
            pass

        except Exception as e:
            logger.critical(f'{e} happened @ getProfileInfo')
            pass

User_Ag = ['Mozilla/5.0 (Linux; Android 9; SM-N975U) AppleWebKit/'
           '537.36 (KHTML, like Gecko) Chrome/91.0.4472.120 Mobile Safari/537.36',
           'Mozilla / 5.0(Linux; Android 11; SM - G525F) AppleWebKit /'
           '537.36(KHTML, like Gecko) Chrome /91.0.4472.120 Mobile Safari /537.36',]
LightMoon commented 3 years ago

hey @neon-ninja ! Can you please tell me how many times I should call set_user_agent function? Should I set it at the beginning of each function which I wrote? or if I reset it whenever the TemporaryBan happens will do?

neon-ninja commented 3 years ago

I don't think changing user agent is a good idea, as it'll likely cause more problems for you than it'll solve