Skip to content

Commit

Permalink
Added try/catcher for catching wrong credentials error box and prompt…
Browse files Browse the repository at this point in the history
…s the message on colsole
  • Loading branch information
MAb540 committed Aug 3, 2021
1 parent 437b7da commit 239602e
Showing 1 changed file with 73 additions and 26 deletions.
99 changes: 73 additions & 26 deletions githubBot.py
Original file line number Diff line number Diff line change
@@ -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()
Expand Down Expand Up @@ -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.")

Expand All @@ -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)
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 = '[email protected]'
github_password = '@bleomessi'
main(uploading_folder_path,github_username, github_password)

0 comments on commit 239602e

Please sign in to comment.