I use this code to scrape odds data. It sends me an email every time there is a discrepancy in odds between bookmakers. I run the code multiple times everyday manually through the terminal. As I run the code multiple times a day, I get many duplicate emails of the same instances of odds discrepancies. The content of the emails is exactly the same, except for the subject line which displays the time when the emails were sent. Is there anyway to log the content of emails so the the code prevents emails being sent which are the same? I would like to prevent the emails being sent not just filter them out once they've been sent through Gmail. The code would also have to refresh each day so the log of the content is emptied. Sorry for the complex question.
request1 = requests.get('https://www.odds.com.au/api/web/public/Meetings/getDataByRangeCacheable/?filter=events,regions,meetings&APIKey=65d5a3e79fcd603b3845f0dc7c2437f0&sportId=1®ionId[]=1®ionId[]=22®ionId[]=24®ionId[]=25®ionId[]=26®ionId[]=27®ionId[]=28®ionId[]=29®ionId[]=30&rangeStart=2020-03-03T16:00:00.657Z&rangeEnd=2020-03-04T15:59:59.657Z')
eventid = []
json1 = request1.json()
for id in json1['events']:
eventid.append(id['id'])
data1 = []
for event in eventid:
request2 = requests.get(f'https://www.punters.com.au/api/web/public/Odds/getOddsComparisonCacheable/?allowGet=true&APIKey=65d5a3e79fcd603b3845f0dc7c2437f0&eventId={event}&betType=FixedWin',
headers={'User-Agent': 'Mozilla/5.0'})
json2 = request2.json()
eventname = json2['eventName']
inttracks =
for selection in json2['selections']:
for price in selection['prices']:
if price['bookmaker'] in ['BetEasy', 'Neds', 'Sportsbet'] and price['hasOdds'] and not any(inttrack in eventname for inttrack in inttracks):
data1.append((eventname, selection['name'], price.get('bookmaker'), price.get('odds', 0)))
cols1 = ['Race', 'Horse', 'Bookmaker', 'AvgOdds']
df1 = pd.DataFrame(data=data1, columns=cols1)
df2 = df1.groupby(by='Horse', sort=False).mean()
df2 = df2.reset_index()
df2 = df2.reset_index()
df3 = round(df2,2)
data2 = []
for event in eventid:
request2 = requests.get(f'https://www.punters.com.au/api/web/public/Odds/getOddsComparisonCacheable/?allowGet=true&APIKey=65d5a3e79fcd603b3845f0dc7c2437f0&eventId={event}&betType=FixedWin',
headers={'User-Agent': 'Mozilla/5.0'})
json2 = request2.json()
eventname = json2['eventName']
for selection in json2['selections']:
for price in selection['prices']:
if price['bookmaker'] in ['Bet365'] and price['hasOdds']:
data2.append((eventname, selection['name'], price.get('bookmaker'), price.get('odds', 0)))
cols2 = ['Race', 'Horse', 'Bookmaker', 'Odds']
df4 = pd.DataFrame(data=data2, columns=cols2)
dfmerge1 = pd.merge(df3, df4, on = "Horse")
dfmerge1["ComparedOdds"] = np.where(dfmerge1["AvgOdds"] < ((dfmerge1["Odds"]-1)*0.8+1), 1, 0)
dfmerge2 = dfmerge1.loc[dfmerge1['ComparedOdds'] == 1, 'Horse'].values
dfmerge3 = dfmerge1.loc[dfmerge1['ComparedOdds'] == 1, 'Race'].values
dfmerge4 = dfmerge1.loc[dfmerge1['ComparedOdds'] == 1, 'Odds'].values
dfmerge5 = dfmerge1.loc[dfmerge1['ComparedOdds'] == 1, 'AvgOdds'].values
for dfmerge2[0], dfmerge3[0], dfmerge4[0], dfmerge5[0] in zip(dfmerge2, dfmerge3, dfmerge4, dfmerge5):
import datetime
datetime = datetime.datetime.now()
subject = f'Overlay - {datetime}'
body = f'Hi Harrison,\n\nYou have one overlay:\n\nFor {dfmerge2[0]} in {dfmerge3[0]}, Bet365 are displaying odds of ${dfmerge4[0]}, where as the average odds are ${dfmerge5[0]}.\n\nBest of luck.'
message = f'Subject: {subject}\n\n{body}'
mail = smtplib.SMTP('smtp.gmail.com', 587)
mail.ehlo()
mail.starttls()
mail.login
mail.sendmail(message)
mail.close()