From 239602e9227122118fc0f3991c691d6e8a6da4c9 Mon Sep 17 00:00:00 2001 From: MAb Date: Tue, 3 Aug 2021 12:16:56 +0500 Subject: [PATCH] Added try/catcher for catching wrong credentials error box and prompts the message on colsole --- githubBot.py | 99 ++++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 73 insertions(+), 26 deletions(-) diff --git a/githubBot.py b/githubBot.py index 394bf8a..0330ae0 100644 --- a/githubBot.py +++ b/githubBot.py @@ -1,55 +1,80 @@ - from selenium import webdriver +from selenium.common.exceptions import NoSuchElementException import time import os import sys -# Directory path where your projects are, which you want to upload on Github as Seperate Repos -uploading_folder_path = "G:\\Netbeans Project\\Uploaded On Github" + # Initializing options for selenium web driver option = webdriver.ChromeOptions() option.add_experimental_option('excludeSwitches', ['enable-logging']) - # Give path to you chromedriver.exe here, to use selenium automation +# just put chromedriver.exe in same folder as your script driver = webdriver.Chrome(executable_path=os.path.join(sys.path[0],'chromedriver.exe'), options=option) + +## also can add path like this +# PATH = r'D:\\Code Programmin\\python\\Scraping\\Github-Bot\chromedriver.exe' +# driver = webdriver.Chrome(executable_path = PATH) + # Method used to login once at start of the automation def login(user_name,pass_word): # Open github login page driver.get('https://github.com/login') - # Username - username = driver.find_element_by_xpath('//*[@id="login_field"]') + + # Let the browser load page completely by waiting 3 seconds + time.sleep(2) + # Input username + username = driver.find_element_by_xpath('//*[@id="login_field"]') username.send_keys(user_name) - # Password - password = driver.find_element_by_xpath('//*[@id="password"]') # Input password + password = driver.find_element_by_xpath('//*[@id="password"]') password.send_keys(pass_word) - # Let the browser load page completely by waiting 3 seconds - time.sleep(3) + # Click on sigin button - sigin = driver.find_element_by_xpath( - '//*[@id="login"]/div[4]/form/div/input[12]') + sigin = driver.find_element_by_xpath('//*[@id="login"]/div[4]/form/div/input[12]') sigin.click() - - # Wait for home page to get loaded completely - time.sleep(5) + + + time.sleep(1) + + ## checking for invalid credentials alert box + ## if it find alert box it means one of user provided credentials is wrong + ErrorDict = {'error':None, 'message':None} + + try: + error = driver.find_element_by_xpath('//*[@id="js-flash-container"]/div/div').text + ErrorDict['error'] = True + ErrorDict['message'] = error + + except NoSuchElementException: + ErrorDict['error'] = False + ErrorDict['message'] = None + + + return ErrorDict + + + # Method to take repository name to create, description to add, private flag, and readme flag -def github_repo( repository_name, descriptions=False, +# added extra parameter(uploading_folder_path) to avoid manually adding folder path in line no: 116 +def github_repo( repository_name, uploading_folder_path,descriptions=False, private=False, readme=False): - + github_homepage_url = 'https://github.com/MyTest54/' + # Create new repo. new_repo = driver.find_element_by_xpath('//*[@id="repos-container"]/h2/a') new_repo.click() @@ -88,14 +113,18 @@ def github_repo( repository_name, descriptions=False, # we need to upload content from our local machine folder to remote repo # Using OS module, we are executing git commands to initialize local repo, add content, # do commit and finally push the content to remote repo - os.chdir(uploading_folder_path+"\\"+repository_name) + + + print(os.chdir(uploading_folder_path+"\\"+repository_name)) print(os.system('echo "# This repository is Uploaded with GITHUB Bot created by Muzamil Nawaz " >> README.md')) print(os.system('git init')) print(os.system('git add .')) print(os.system('git commit -m \"first commit\"')) print(os.system('git branch -M main')) + # Here we are converting general repo name into repo specific format ('-' instead of spaces) - print(os.system('git remote add origin https://github.com/Muzamil-Nawaz/'+generateRepoName(repository_name)+'.git')) + + print(os.system(f'git remote add origin https://github.com/Muzamil-Nawaz/'+generateRepoName(repository_name)+'.git')) print(os.system('git push -u origin main')) print(repository_name+" uploaded successfully.") @@ -104,20 +133,38 @@ def github_repo( repository_name, descriptions=False, # Wait for homepage to load completely time.sleep(3) + # Method used for converting general string to github_repo_format_string def generateRepoName(str): # Replace all spaces in name with '-' return str.replace(" ","-") return str; + + # Method to get all project directory names from the given path folder -def getDirs(path): - os.chdir(path) +def main(uploading_folder_path,github_username,github_password): + + os.chdir(uploading_folder_path) dirs = os.listdir() + # Login with username password at start of automation - login("your-github-username-here", "your-github-password-here") - # Loop through direcotories one by one by creating their remote repos - for dir in dirs: - github_repo(dir) + # and checking error, if error is found the message will be printed + ErrorDict = login(github_username,github_password) + if ErrorDict.get('error'): + print(ErrorDict.get('message')) + return + + + # Loop through direcotories one by one by creating their remote repos + time.sleep(2) + for dir_name in dirs: + github_repo(dir_name,uploading_folder_path) -getDirs(uploading_folder_path) \ No newline at end of file +if __name__ == '__main__': + + # Directory path where your projects are, which you want to upload on Github + uploading_folder_path = 'D:\\Code Programmin\\python\\Scraping\\tttt' + github_username = 'awana4872@gmail.com' + github_password = '@bleomessi' + main(uploading_folder_path,github_username, github_password) \ No newline at end of file