import xmlrpc.client
from cmath import sin
from email import header
from webbrowser import get
from wsgiref.headers import Headers
from bs4 import BeautifulSoup
import requests
import urllib3
import urllib
import pandas as pd
import os
import re
import time
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver import ActionChains
from selenium.webdriver.chrome.options import Options
from zipfile import *
import zipfile
from os.path import basename
import gspread
from selenium.common.exceptions import TimeoutException


sa = gspread.service_account(filename="C:/Users/viral/Documents/sheetsapi.json")
sh = sa.open("only74kotc")
sheet = sh.worksheet("pricedrop")
http = urllib3.PoolManager()

headers = {
    'Accept-Encoding': 'gzip, deflate, sdch',
    'Accept-Language': 'en-US,en;q=0.8',
    'Upgrade-Insecure-Requests': '1',
    'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36',
    'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
    'Cache-Control': 'max-age=0',
    'Connection': 'keep-alive',
}

options = webdriver.ChromeOptions()
options.add_argument("--log-level=3")
options.add_argument("user-data-dir=C:\\Users\\viral\\AppData\\Local\\Google\\Chrome Beta\\User Data\\")
options.add_argument("--start-maximized")  # use --start-maximized instead
options.binary_location = "C:\\Program Files\\Google\\Chrome Beta\\Application\\chrome.exe"

driverlocation = "C:/Users/viral/Documents/chromedriver.exe"
mypassword = 'your password'

start_row = 2
end_row = start_row + 1

for i in range(start_row, end_row):
    mytitle = sheet.cell(i, 1).value

    # Replace these with your WordPress credentials and site URL
    username = 'AppPoster'
    password = mypassword
    site_url = 'https://hawstok.com/xmlrpc.php'  # Make sure to use the correct XML-RPC endpoint

    # Create a connection to the XML-RPC endpoint
    client = xmlrpc.client.ServerProxy(site_url)

    # Image data
    image_path = "C:/Users/viral/Documents/play store apps batch 1/"+mytitle+".jpg"  # Replace with the actual image path
    image_data = xmlrpc.client.Binary(open(image_path, 'rb').read())
    image_filename = 'my thumbnail.jpg'  # Replace with the desired filename for the image

    # Prepare the attachment data
    attachment = {
        'name': image_filename,
        'type': 'image/jpeg',
        'bits': image_data,
        'overwrite': True  # Set to True to overwrite if an image with the same name exists
    }

    # Authenticate and upload the image
    try:
        attachment_info = client.wp.uploadFile(0, username, password, attachment)
        attachment_id = attachment_info['id']  # Get the attachment ID
        image_url = attachment_info['url']  # Get the attachment URL
        print(f'Image uploaded successfully. Image URL: {image_url}, Attachment ID: {attachment_id}')
    except Exception as e:
        print(f'Failed to upload the image. Error: {e}')




    post_title = "Top 10 Best "+mytitle+" Apps For Android"
    # Define the path to the text file
    text_file_path = "C:/Users/viral/Documents/play store apps batch 1/" + mytitle + " article full heading.txt"

    # Read content from the text file
    with open(text_file_path, 'r', encoding='utf-8') as file:
        post_content = file.read()

    # Apply modifications to the content
    post_content = post_content.replace('etc. Below is the list of top 10 best '+mytitle+' apps for Android.', '', 1)

    second_heading_occurrence = 2  # Set the occurrence after which you want to add the image
    original_heading = "<h2 style='text-align: center;'>10 Best " + mytitle + " Apps For Android</h2>"
    image_html = '<img src="'+image_url+'" alt="Best ' + mytitle + ' Apps For Android">'

    # Find the index of the second occurrence of the original heading
    index = post_content.find(original_heading)
    for _ in range(second_heading_occurrence - 1):
        index = post_content.find(original_heading, index + 1)

    if index != -1:
        # Insert the image HTML right after the second occurrence of the heading
        post_content = post_content[:index + len(original_heading)] + image_html + post_content[index + len(original_heading):]

    # Create a new post
    post = {
        'title': post_title,
        'description': post_content,
        'post_type': 'post',
        'post_status': 'publish',
        'wp_post_thumbnail': attachment_id,  # Use the attachment ID obtained from uploading the image
        'categories': ['android']
    }

    # Authenticate and create the post
    try:
        post_id = client.metaWeblog.newPost('', username, password, post)
       
        # Construct the post URL using the post's slug
        post_slug = post_title.lower().replace(' ', '-')  # Assuming your slug format is lowercase with hyphens
        post_url = f'https://hawstok.com/{post_slug}/'
       
        print(f'Post created successfully. Post URL: {post_url}')
    except Exception as e:
        print(f'Failed to create the post. Error: {e}')

0 comments:

Post a Comment

 
Top