-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
41 lines (27 loc) · 1.1 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import re
from getsitemap import get_individual_sitemap
from google.oauth2 import service_account
from googleapiclient.discovery import build
SITEMAP = "https://example.com/sitemap.xml"
MATCHING_REGEXES = [r"/example/"]
REQUEST_TYPE = "URL_UPDATED"
urls = get_individual_sitemap(SITEMAP)[SITEMAP]
credentials = service_account.Credentials.from_service_account_file("secret.json")
service = build("indexing", "v3", credentials=credentials)
batch = service.new_batch_http_request()
eligible_urls = [url for url in urls if any(re.match(regex, url) for regex in MATCHING_REGEXES)]
batches = [eligible_urls[i : i + 100] for i in range(0, len(eligible_urls), 100)]
def callback(_, response, exception):
if exception is not None:
print("Error")
print(exception)
else:
print("Success")
print(response)
for batch_urls in batches:
for url in batch_urls:
request = {"url": url, "type": REQUEST_TYPE}
batch.add(service.urlNotifications().publish(body=request), callback=callback)
batch.execute()
batch = service.new_batch_http_request()
print("Done ✨")