-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpublishPicksTelegram.py
143 lines (123 loc) · 5.88 KB
/
publishPicksTelegram.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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
import asyncio
import re
from datetime import datetime, timedelta
import requests
import json
from bs4 import BeautifulSoup
import telegram
from models import db, objects
from credentials import *
# Database connection
dbConnection = db.Database()
breaksDB = dbConnection.connect()
picksTelegramObj = objects.PicksTelegram(breaksDB)
tournamentsObj = objects.Tournaments(breaksDB)
# Credentials
bot = telegram.Bot(token=TELEGRAM_TOKEN)
# Constants and variables
emojisNumbers = ('0️⃣', '1️⃣', '2️⃣', '3️⃣', '4️⃣', '5️⃣', '6️⃣', '7️⃣', '8️⃣', '9️⃣')
weekdays = ('Lunes', 'Martes', 'Miércoles', 'Jueves', 'Viernes', 'Sábado', 'Domingo')
SURFACES = {'I': 'Pista dura indoor', 'D': 'Pista dura outdoor', 'T': 'Tierra batida', 'H': 'Hierba', 'M': 'Moqueta'}
FLAGS = {
'australia': '🇦🇺',
'china': '🇨🇳',
'dominican-republic': '🇩🇴',
'egypt': '🇪🇬',
'france': '🇫🇷',
'germany': '🇩🇪',
'great-britain': '🇬🇧',
'greece': '🇬🇷',
'italy': '🇮🇹',
'mexico': '🇲🇽',
'qatar': '🇶🇦',
'rwanda': '🇷🇼',
'south-korea': '🇰🇷',
'spain': '🇪🇸',
'switzerland': '🇨🇭',
'tunisia': '🇹🇳',
'uae': '🇦🇪',
'usa': '🇺🇸'
}
async def send(imageURL, message):
await bot.send_photo(chat_id=TELEGRAM_CHANNEL_ID, photo=imageURL, caption=message, parse_mode=telegram.constants.ParseMode.MARKDOWN_V2)
# Get Tipsterland picks
picksTelegramDB = picksTelegramObj.find_all()
numPublishedPicks = len(list(picksTelegramDB))
url = 'https://www.tipsterland.com/api/picks/cards?tipster_id={}&month=5&year=2024&pending=false&requestId=1&page=1'.format(TIPSTERLAND_ID)
jsonContent = json.loads(requests.get(url).text)
soup = BeautifulSoup(jsonContent['data'], 'lxml')
picks = soup.select('div[class*=x-pick-card]')
today = datetime.now()
numPicks = 0
numPick = numPublishedPicks + 1
for pick in picks:
pickDate = pick.select_one('div[class=pick-card-event-date] span').text
pickDateParts = pickDate.split(' ')
pickDateParts[0] = pickDateParts[0].zfill(2)
pickDateParts[1] = pickDateParts[1].title()
pickDate = ' '.join(pickDateParts).replace('29 Feb', '1 Mar').replace('Abr', 'Apr')
pickDate = datetime.strptime(pickDate, '%d %b. %H:%M') + timedelta(hours=2)
pickDate = pickDate.replace(year=today.year)
#if pickDate > today or numPicks < 1:
if pickDate > today:
# Future pick
pickDB = {}
numPickString = str(numPick)
numPickEmoji = ''
for numPickChar in numPickString:
numPickEmoji += emojisNumbers[int(numPickChar)]
pickDB['pick'] = pick.select_one('div[class^="x-pick-alert"]').text.strip()
pickDB['competition'] = pick.select_one('div[class=pick-card-competition]').text.strip()
tournamentParts = pickDB['competition'].split(' ')
category = tournamentParts[0]
tournamentName = ' '.join(tournamentParts[1:])
print(tournamentName)
if category == 'ITF':
tournament = tournamentsObj.find([{'category': 'ITF'}, {'name': {'$regex': tournamentName}}])
elif category[:3] == 'ATP':
categoryParts = category.split('-')
subcategory = categoryParts[1]
tournament = tournamentsObj.find([{'category': 'ATP'}, {'subcategory': subcategory}, {'name': {'$regex': tournamentName}}])
elif category == 'Challenger':
tournament = tournamentsObj.find([{'category': 'CH'}, {"$or": [{'name': {'$regex': '{} challenger'.format(tournamentName)}}, {'name': {'$regex': '{} chall.'.format(tournamentName)}}]}])
elif category == 'Masters':
tournamentName = ' '.join(tournamentParts[2:])
print(tournamentName)
tournament = tournamentsObj.find([{'category': 'ATP'}, {'subcategory': '1000'}, {'name': {'$regex': tournamentName}}])
country = tournament['country']
if country in FLAGS:
flag = FLAGS[country]
else:
flag = ''
pickDB['event'] = pick.select_one('div[class=pick-card-event]').text.strip()
weekday = weekdays[pickDate.weekday()]
pickDB['date'] = '{} {}, a las {} h.'.format(weekday, pickDate.strftime('%d/%m'), pickDate.strftime('%H:%M'))
pickDB['odd'] = pick.select_one('div[class*="odds-rounded-label"]').text.strip()
print(pickDB)
foundPick = picksTelegramObj.find([{'date': pickDate.strftime('%Y-%m-%d %H:%M')}, {'pick': pickDB['pick']}])
if foundPick is None:
# Get picture
pickID = pick.select_one('button[data-pick-id]')['data-pick-id']
pickURL = 'https://www.tipsterland.com/api/dlg-pick-view/{}?dlgId=dlg_pick_card_details&show_tipster=false'.format(pickID)
r = requests.get(pickURL)
pickSoup = BeautifulSoup(r.text, 'lxml')
imageURL = pickSoup.select_one('img[class=pick-image]')['src']
if 'http' not in imageURL:
imageURL = 'https://www.tipsterland.com{}'.format(imageURL)
print('Image URL: {}'.format(imageURL))
# Send message to Telegram
message = '{} *{}*\n\n🏆 {} {}\n📌 {}\n🎾 {}\n⏰ {}\n💰 @{}'.format(numPickEmoji, re.escape(pickDB['pick']), re.escape(pickDB['competition']), flag, SURFACES[tournament['surface']], re.escape(pickDB['event']), re.escape(pickDB['date']), pickDB['odd'])
print(message)
asyncio.run(send(imageURL, message))
# Insert into DB
pickTelegram = {
'_id': numPick,
'date': pickDate.strftime('%Y-%m-%d %H:%M'),
'competition': pickDB['competition'],
'pick': pickDB['pick'],
'odd': float(pickDB['odd'].replace(',', '.'))
}
picksTelegramObj.create(pickTelegram)
numPick += 1
numPicks += 1
print("The processed have been completed successfully")