LondoN eXtream
Regular Member
- May 14, 2020
- 294
- 194
Python:
import json
import google.auth
from googleapiclient.discovery import build
from google.oauth2.service_account import Credentials
import xml.etree.ElementTree as ET
import requests
# Configurarea căilor pentru fișierele JSON de autentificare
SERVICE_ACCOUNT_FILES = [
'path_to_service_account_1.json',
'path_to_service_account_2.json',
'path_to_service_account_3.json',
# Adaugă mai multe fișiere de autentificare pentru fiecare cont
]
SITEMAP_URL = 'https://exemplu.com/sitemap.xml'
SENT_URLS_FILE = 'sent_urls.json'
# Preluarea URL-urilor din sitemap
def get_urls_from_sitemap(sitemap_url):
response = requests.get(sitemap_url)
tree = ET.ElementTree(ET.fromstring(response.text))
root = tree.getroot()
urls = [url.find('loc').text for url in root.findall('url')]
return urls
# Funcție pentru a încărca URL-urile deja trimise
def load_sent_urls():
try:
with open(SENT_URLS_FILE, 'r') as f:
return json.load(f)
except FileNotFoundError:
return {}
# Funcție pentru a salva URL-urile trimise
def save_sent_urls(sent_urls):
with open(SENT_URLS_FILE, 'w') as f:
json.dump(sent_urls, f)
# Funcție pentru a trimite un URL către Google Indexing API
def send_url_to_indexing(url, service):
try:
body = {
'url': url,
'type': 'URL_UPDATED'
}
service.urlNotifications().publish(body=body).execute()
print(f"Sent URL: {url}")
return True
except Exception as e:
print(f"Failed to send URL: {url}, Error: {e}")
return False
# Funcție principală pentru a trimite URL-urile
def send_urls(service_account_files):
urls = get_urls_from_sitemap(SITEMAP_URL)
sent_urls = load_sent_urls()
sent_count = 0
for service_account_file in service_account_files:
# Autentificare cu un alt Service Account
credentials = Credentials.from_service_account_file(service_account_file, scopes=['https://www.googleapis.com/auth/indexing'])
service = build('indexing', 'v3', credentials=credentials)
for url in urls:
# Verificăm dacă URL-ul a fost deja trimis (înainte de a-l trimite)
if url not in sent_urls:
if send_url_to_indexing(url, service):
sent_urls[url] = True
sent_count += 1
# Oprește trimiterea după ce am trimis 200 URL-uri pentru acest Service Account
if sent_count % 200 == 0:
break
# Dacă am trimis 200 URL-uri, oprește și treci la următorul Service Account
if sent_count % 200 == 0:
break
# Salvează URL-urile trimise pentru a nu le trimite din nou
save_sent_urls(sent_urls)
return sent_count
if __name__ == '__main__':
sent_count = send_urls(SERVICE_ACCOUNT_FILES)
print(f"Total sent URLs: {sent_count}")
Basically, create multiple service account and store them in a folder. After, update the URLs in the script. Modify the sitemap URL file with your sitemap. Run and go!
Also, apply this command before running
Code:
pip install --upgrade google-api-python-client google-auth-httplib2 google-auth-oauthlib
Tested it and managed to send 1000 URLs. I do not have more service account and i am lazy to create them. Happy new year!