Tuesday, October 13, 2020

Posting on wordpress

 def is_content_insufficient(posts_count, content_count):

    if posts_count != content_count:
return 1
else:
return 0


def get_quotes(keyword, page_no):
import requests
from bs4 import BeautifulSoup as bs
QuoteArray = []
AuthorArray = []
BookArray = []
base_url = 'https://www.goodreads.com/quotes/tag/'
url = base_url + keyword + '?page=' + str(page_no)
response_data = requests.get(url).text[:]
soup = bs(response_data, features='html.parser')
for item in soup.find_all("div", class_="quoteText"):
temp = item.find(text=True)
QuoteArray.append(temp.rstrip())
for item in soup.find_all("span", class_="authorOrTitle"):
AuthorArray.append(item.get_text().rstrip())
for item in soup.find_all("a", class_="authorOrTitle"):
BookArray.append(item.get_text().rstrip())
all_quotes = ''
for i in range(20):

if len(BookArray) >= 20:
all_quotes = all_quotes + QuoteArray[i] + AuthorArray[i] + ' ' + BookArray[i] + '\n'
else:
all_quotes = all_quotes + QuoteArray[i] + AuthorArray[i] + '\n'

return all_quotes


def send_keys(element, keys):
cc = 0
while 1:
cc += 1
if cc > waiting_period:
return 'fail'
try:
e = wb.find_elements_by_css_selector(element)
for x in e:
try:
if x.get_attribute('value').strip() != '':
continue
x.click()
except:
time.sleep(0.1)
x.send_keys(keys)
return 'ok'
except:
time.sleep(1)


def click(element: object) -> object:
cc = 0
while 1:
cc += 1
if cc > waiting_period:
return 'fail'
try:
e = wb.find_element_by_css_selector(element)
e.click()
return 'ok'
except:
time.sleep(1)


def login_to_site():
wb.get('wp-admin')
send_keys("#user_login", "")
send_keys("#user_pass", "")
click("#wp-submit")


def wait_for_text(text):
cc = 0
print("waiting for text: " + str(text))
while 1:
try:
cc += 1
if cc > waiting_period or "This site can’t be reached" in wb.page_source:
return 'fail'
if text.lower() in str(wb.page_source).lower():
return 'ok'
else:
time.sleep(1)
except:
time.sleep(1)


def scroll_window_to_top():
try:
wb.execute_script("window.scrollBy(-10000, -10000)")
except:
time.sleep(0)


def scroll_window_to_bottom():
try:
wb.execute_script("window.scrollBy(50, 50)")
except:
time.sleep(0)


def modify_quiz_settings(question, option):
click(question)
time.sleep(1)
click(option)
time.sleep(1)


def return_location(element: object) -> object:
cc = 0
while 1:
cc += 1
if cc > waiting_period:
return 'fail'
try:
e = wb.find_element_by_css_selector(element)
location = e.location
return location
except:
time.sleep(1)


from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.action_chains import ActionChains
import time
import sys
import os
import subprocess

total_posts = int(input('Enter the total number of posts\n'))
posts_links = []
for i in range(0, total_posts):
ele = input()
posts_links.append(ele)
try:
article_file = open("C:/Users/USER/Desktop/articles.txt", "r")
content = article_file.read()
except:
print("There is some problem with the Article file. Please check")
sys.exit()
Articles = content.split("$%")
article_file.close()
if is_content_insufficient(int(total_posts), len(Articles)):
print('Insufficient number of articles in articles.txt file')
sys.exit()

try:
Quotes_Keywords = open("C:/Users/USER/Desktop/keywords.txt", "r")
Quotes_content = Quotes_Keywords.read()
except:
print("There is some problem with the Quotes file. Please check")
sys.exit()
keywords = Quotes_content.split("\n")
Quotes_Keywords.close()

if is_content_insufficient(int(total_posts), len(keywords)):
print('Insufficient number of keywords in keywords.txt file')
sys.exit()

try:
Images_Paths_obj = open('C:/Users/USER/Desktop/images.txt', 'r')
Image_File_content = Images_Paths_obj.read()
except:
print("There is some problem with the images.txt file")
sys.exit()
Images_Paths = Image_File_content.split('\n')
Images_Paths_obj.close()


main_posts_content = []
button_shortcode = ' \n\n[su_button url="#section1" style="soft" background="#ff1c22" size="16" center="yes" radius="5"]Click here to play the Quiz[/su_button]\n\n '
start_quiz = '<a id="section1"></a>"Start the Quiz"'


for i in range(0, total_posts):
main_posts_content.append(button_shortcode + Articles[i] + get_quotes(keywords[i], 2))

for i in range(0, total_posts):
waiting_period = 50
wb = ''
driver_path = os.path.join(sys.path[0], 'data', 'C:/Users/USER/Desktop/chromedriver.exe')
send_enter_script = os.path.join(sys.path[0], 'data', 'send_enter.exe')

# creating browser instance
try:
wb = webdriver.Chrome(executable_path=driver_path)
wb.set_window_size(1100, 700)
except Exception as Error:
print('Error Occurred while creating a browser instance...')
print('Error: ' + str(Error))
print('Line: ' + str(Error.__traceback__.tb_lineno))
input('')
sys.exit()

# logging in to the site.
print("logging in to the site...")
login_to_site()

time.sleep(1)

# navigating to the post.
print("navigating to the post...")
wb.get(posts_links[i])
wait_for_text("Edit Quiz")

time.sleep(1)

click('#content-html')
send_keys('#content', main_posts_content[i])
time.sleep(2)
click('#content-tmce')
time.sleep(2)
scroll_window_to_bottom()
click('#quizzard > p > a:nth-child(3)')
time.sleep(1)
modify_quiz_settings('#snax_one_question_per_page', '#snax_one_question_per_page > option:nth-child(1)')
modify_quiz_settings('#snax_start_quiz', '#snax_start_quiz > option:nth-child(2)')
modify_quiz_settings('#snax_play_again', '#snax_play_again > option:nth-child(2)')
click('#snax_pinterest_limit')
time.sleep(1)
wb.find_element_by_id('snax_pinterest_limit').clear()
time.sleep(0.5)
wb.find_element_by_css_selector('#snax_pinterest_limit').send_keys('2000')
time.sleep(0.5)
scroll_window_to_top()
click('#insert-media-button')
time.sleep(2)
click('#menu-item-upload')
time.sleep(2)
input_file = "//input[starts-with(@id,'html5_')]"
wb.find_element_by_xpath(input_file).send_keys(Images_Paths[i])
time.sleep(50)
click('#__wp-uploader-id-0 > div.media-frame-toolbar > div > div.media-toolbar-primary.search-form > button')
time.sleep(2)
click('#content-html')
time.sleep(1)
for i in range(3):
wb.find_element_by_css_selector("#content").send_keys(Keys.DOWN)
time.sleep(0.5)
wb.find_element_by_css_selector('#content').send_keys(Keys.ENTER)
time.sleep(0.5)
wb.find_element_by_css_selector("#content").send_keys(start_quiz)
time.sleep(1.5)
scroll_window_to_top()
time.sleep(0.5)
click('#content-tmce')
time.sleep(0.5)
wb.switch_to.default_content()
scroll_window_to_top()

time.sleep(2)
click('#publish')
time.sleep(5)
wb.quit()