The main objective of this bot to make fast and reliable automatic subscriptions to email newsletter for people, who filled the website form(submissions go to google sheet)
I had too many requests to google docs.(rate limit error) -> so i added sleep 5 after every subscription. Also added break into email-check loop.(to avoid subscribing too many in 1 loop)
# pylint: disable=missing-module-docstring
import quickemailverification
import json
import sentry_sdk
import re
import requests
import traceback
from bs4 import BeautifulSoup
from mod import *
import gspread
import urllib3
from os import system
import time
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
client = quickemailverification.Client(
'eed14e5a') # key blurred
sentry_sdk.init("https://322.ingest.sentry.io/334", # key blurred
traces_sample_rate=1.0)
def regex(regex, text):
'''If find this text-return True'''
return bool(re.search(regex, text))
def checkemail(email):
'''Api for email deliverability True-means many metrics are fine'''
quickemailverification = client.quickemailverification()
response = quickemailverification.verify(email)
if response.body["safe_to_send"] == "true":
return True
else:
return False
# pylint: disable=missing-module-docstring
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
# For clarity in opened windows cmd(i just run .py file and they all ahve same title)
system("Ben EMail")
r = requests
def subscribe(email, name="", proxy="", debug=False):
if debug:
proxy = "http://127.0.0.1:8080"
proxies = {
"https": proxy
}
burp0_url = "https://api.benchmarkemail.com"
burp0_cookies = {
"Key": "M1oqxE5kauGh55V12W2J" # key blurred
}
burp0_headers = {}
burp0_data = {
"doubleoptin": "1",
"fldEmail": email,
"fldfirstname": name,
"fldlastname": ''
}
r = requests.post(burp0_url,
headers=burp0_headers,
cookies=burp0_cookies,
data=burp0_data,
verify=False,
proxies=proxies,
timeout=10)
if regex("Check your inbox", r.text):
# check response html text
print("Subscribed", name, email, r.status_code)
success = True
else:
print("Error", name, email, "status code", r.status_code)
success = False
return success, r.text
credentials = {
"type": "service_account",
# key blurred
}
gc = gspread.service_account_from_dict(credentials)
sh = gc.open("email_form")
ws = sh.get_worksheet(0)
def parse_bmresponse(response):
soup = BeautifulSoup(response, 'html.parser')
result = soup.find("div", {"class": "content-section"})
# apart from parsing response for "success phrase" i save api responses for further investigations
return result.get_text()
def dotable(): # go through the whole table and find who is not subscribed yet
all_values = ws.get_all_values()
rownum = 1
for row in all_values[1:]:
rownum += 1
if row[2] == "subscribed" or row[2] == "error":
pass
elif row[2] == "":
name = row[0]
email = row[1]
print(fr"Empty status {name},{email} on {rownum} ")
success, response = subscribe(email, name)
if success:
# Change status if subbed
ws.update("C" + str(rownum), 'moded_ok')
# response from email delivery api
check_result = "Can send "+str(checkemail(email))
# raw result from subsription func
ws.update("F" + str(rownum), check_result)
# response from email delivery api
ws.update("G" + str(rownum), parse_bmresponse(response))
break
else:
ws.update("C" + str(rownum), 'moded_noluck')
ws.update("F" + str(rownum), check_result)
ws.update("G" + str(rownum), parse_bmresponse(response))
break
else:
pass
while 1 > 0:
try:
dotable()
time.sleep(5)
except requests.exceptions.ReadTimeout:
print("Timeout server, i sleep 15")
time.sleep(15)
except Exception:
traceback.print_exc()
sentry_sdk.capture_exception()