Advertisement
Guest User

Untitled

a guest
Jul 3rd, 2024
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 26.59 KB | None | 0 0
  1. -----------------------------
  2. # celery.py
  3. ---------------------------
  4. import os
  5. from celery import Celery
  6. from celery.schedules import crontab # scheduler
  7. from dotenv import load_dotenv
  8.  
  9. load_dotenv()
  10. os.environ.setdefault(
  11. "DJANGO_SETTINGS_MODULE", f"core.settings.{os.getenv('PROJ_ENVIROMENT')}"
  12. )
  13. app = Celery("core")
  14. app.config_from_object("django.conf:settings", namespace="CELERY")
  15.  
  16. app.autodiscover_tasks(["jobsSchedule"])
  17.  
  18.  
  19. # scheduled task execution
  20. app.conf.beat_schedule = {
  21. "check-tips": {
  22. "task": "jobsSchedule.tasks.check_tips",
  23. "schedule": crontab(minute=0),
  24. },
  25. "scraping-task-daily-selenium": {
  26. "task": "jobsSchedule.tasks.getDailyMatches",
  27. "schedule": crontab(minute=0, hour=10),
  28. },
  29. }
  30.  
  31.  
  32. @app.task(bind=True)
  33. def debug_task(self):
  34. print("Request: {0!r}".format(self.request))
  35.  
  36.  
  37.  
  38. ---------------------------
  39. #jobsSchedule/tasks.py
  40. ------------------------------
  41. from celery import shared_task
  42. from .dailyGetMatchOdds import GetDailyMatchData
  43.  
  44. @shared_task
  45. def getDailyMatches():
  46. GetDailyMatchData()
  47.  
  48. @shared_task
  49. def check_tips():
  50. from jobsSchedule import check_tip
  51.  
  52. check_tip.run_task()
  53.  
  54. ----------------------------------
  55. #jobsSchedule/check_tip.py
  56. -----------------------------------
  57.  
  58. from datetime import date
  59.  
  60. import requests
  61. from django.db.models import Count, Prefetch
  62.  
  63.  
  64. from flash.models.Tips import BtsTip, DcTip, HtftTip, MatchTip, OnextwoTip, OuTip
  65. from utils import send_check_msg
  66.  
  67.  
  68.  
  69. today = date.today()
  70. fixture_url = "https://api-football-v1.p.rapidapi.com/v3/fixtures?date=" + str(today)
  71.  
  72.  
  73. # Her bir fixture_id ucun is_active=True olan
  74. all_is_active_tips = (
  75. MatchTip.objects.filter(is_active=True)
  76. .values("fixture_id")
  77. .distinct()
  78. .annotate(count=Count("fixture_id"))
  79. )
  80.  
  81. # fixture_id listesi
  82. all_tips_fixture_list = [tip["fixture_id"] for tip in all_is_active_tips]
  83.  
  84. # is_active=True count
  85. all_is_active_tips_count = MatchTip.objects.filter(is_active=True).count()
  86.  
  87.  
  88. headers = {
  89. "X-RapidAPI-Host": "",
  90. "X-RapidAPI-Key": "",
  91. }
  92.  
  93. all_fixtures_response = requests.get(fixture_url, headers=headers).json()["response"]
  94.  
  95.  
  96. def check_match(all_fixtures_response):
  97. for fixture in all_fixtures_response:
  98. fixture_status = fixture["fixture"]["status"]["short"]
  99.  
  100. if fixture_status == "FT":
  101. fixture_id = fixture["fixture"]["id"]
  102.  
  103. match_score = fixture["score"]
  104.  
  105. home_full_time_score = None
  106. away_full_time_score = None
  107. home_first_time_score = None
  108. away_first_time_score = None
  109. home_second_time_score = None
  110. away_second_time_score = None
  111. # full time
  112. match_fulltime = match_score["fulltime"]
  113. if match_fulltime["home"] is not None:
  114. home_full_time_score = int(match_fulltime["home"])
  115. if match_fulltime["away"] is not None:
  116. away_full_time_score = int(match_fulltime["away"])
  117. # first time
  118. match_halftime = match_score["halftime"]
  119.  
  120. if match_halftime["home"] is not None:
  121. home_first_time_score = int(match_halftime["home"])
  122.  
  123. if match_halftime["away"] is not None:
  124. away_first_time_score = int(match_halftime["away"])
  125.  
  126. # second time
  127. if home_full_time_score is not None and home_first_time_score is not None:
  128. home_second_time_score = home_full_time_score - home_first_time_score
  129.  
  130. if away_full_time_score is not None and away_first_time_score is not None:
  131. away_second_time_score = away_full_time_score - away_first_time_score
  132.  
  133. if str(fixture_id) in all_tips_fixture_list:
  134. match_tips = MatchTip.objects.filter(fixture_id=fixture_id)
  135. match_tips = match_tips.prefetch_related(
  136. Prefetch(
  137. "tip_bts_tip",
  138. queryset=BtsTip.objects.filter(
  139. match_id__in=match_tips.values_list("id", flat=True)
  140. ).defer("created_at", "updated_at"),
  141. ),
  142. Prefetch(
  143. "tip_dc_tip",
  144. queryset=DcTip.objects.filter(
  145. match_id__in=match_tips.values_list("id", flat=True)
  146. ).defer("created_at", "updated_at"),
  147. ),
  148. Prefetch(
  149. "tip_htft_tip",
  150. queryset=HtftTip.objects.filter(
  151. match_id__in=match_tips.values_list("id", flat=True)
  152. ).defer("created_at", "updated_at"),
  153. ),
  154. Prefetch(
  155. "tip_onextwo_tip",
  156. queryset=OnextwoTip.objects.filter(
  157. match_id__in=match_tips.values_list("id", flat=True)
  158. ).defer("created_at", "updated_at"),
  159. ),
  160. Prefetch(
  161. "tip_ou_tip",
  162. queryset=OuTip.objects.filter(
  163. match_id__in=match_tips.values_list("id", flat=True)
  164. ).defer("created_at", "updated_at"),
  165. ),
  166. )
  167.  
  168. check_odds(
  169. match_tips,
  170. home_first_time_score,
  171. away_first_time_score,
  172. home_second_time_score,
  173. away_second_time_score,
  174. home_full_time_score,
  175. away_full_time_score,
  176. )
  177.  
  178.  
  179. def check_odds(
  180. match_tips,
  181. home_first_time_score,
  182. away_first_time_score,
  183. home_second_time_score,
  184. away_second_time_score,
  185. home_full_time_score,
  186. away_full_time_score,
  187. ):
  188. for match_tip in match_tips:
  189. match_tip.home_first_time_score = home_first_time_score
  190. match_tip.away_first_time_score = away_first_time_score
  191. match_tip.home_second_time_score = home_second_time_score
  192. match_tip.away_second_time_score = away_second_time_score
  193. match_tip.home_full_time_score = home_full_time_score
  194. match_tip.away_full_time_score = away_full_time_score
  195.  
  196. match_tip.save()
  197.  
  198. if match_tip.tip_bts_tip.exists():
  199. for tip in match_tip.tip_bts_tip.all():
  200. # Full time
  201. if tip.half == "allmatch":
  202. # column - 1
  203. if (tip.column == "Yes") and (
  204. (home_full_time_score > 1) and (away_full_time_score > 1)
  205. ):
  206. match_tip.is_correct = True
  207. match_tip.is_active = False
  208. match_tip.save()
  209. elif (tip.column == "No") and (
  210. home_full_time_score == 0 and away_full_time_score == 0
  211. ):
  212. match_tip.is_correct = True
  213. match_tip.is_active = False
  214. match_tip.save()
  215. else:
  216. match_tip.is_correct = False
  217. match_tip.is_active = False
  218. match_tip.save()
  219. elif tip.half == "firsthalf":
  220. # column - 1
  221. if (tip.column == "Yes") and (
  222. (home_full_time_score > 1) and (away_full_time_score > 1)
  223. ):
  224. match_tip.is_correct = True
  225. match_tip.is_active = False
  226. match_tip.save()
  227. elif (tip.column == "No") and (
  228. home_full_time_score == 0 and away_full_time_score == 0
  229. ):
  230. match_tip.is_correct = True
  231. match_tip.is_active = False
  232. match_tip.save()
  233. else:
  234. match_tip.is_correct = False
  235. match_tip.is_active = False
  236. match_tip.save()
  237. elif tip.half == "secondhalf":
  238. # column - 1
  239. if (tip.column == "Yes") and (
  240. (home_full_time_score > 1) and (away_full_time_score > 1)
  241. ):
  242. match_tip.is_correct = True
  243. match_tip.is_active = False
  244. match_tip.save()
  245. elif (tip.column == "No") and (
  246. home_full_time_score == 0 and away_full_time_score == 0
  247. ):
  248. match_tip.is_correct = True
  249. match_tip.is_active = False
  250. match_tip.save()
  251. else:
  252. match_tip.is_correct = False
  253. match_tip.is_active = False
  254. match_tip.save()
  255.  
  256. if match_tip.tip_dc_tip.exists():
  257. for tip in match_tip.tip_dc_tip.all():
  258. # Full time
  259. if tip.half == "allmatch":
  260. # column - 1X
  261. if (tip.column == "1X") and (
  262. (home_full_time_score > away_full_time_score)
  263. or (home_full_time_score == away_full_time_score)
  264. ):
  265. match_tip.is_correct = True
  266. match_tip.is_active = False
  267. match_tip.save()
  268. # column -XX
  269. elif (tip.column == "XX") and (
  270. (home_full_time_score > away_full_time_score)
  271. or (away_full_time_score > home_full_time_score)
  272. ):
  273. match_tip.is_correct = True
  274. match_tip.is_active = False
  275. match_tip.save()
  276. # column -2X
  277. elif (tip.column == "2X") and (
  278. (away_full_time_score > home_full_time_score)
  279. or (home_full_time_score == away_full_time_score)
  280. ):
  281. match_tip.is_correct = True
  282. match_tip.is_active = False
  283. match_tip.save()
  284. else:
  285. match_tip.is_correct = False
  286. match_tip.is_active = False
  287. match_tip.save()
  288. elif tip.half == "firsthalf":
  289. # column - 1X
  290. if (tip.column == "1X") and (
  291. (home_full_time_score > away_full_time_score)
  292. or (home_full_time_score == away_full_time_score)
  293. ):
  294. match_tip.is_correct = True
  295. match_tip.is_active = False
  296. match_tip.save()
  297. # column -XX
  298. elif (tip.column == "XX") and (
  299. (home_full_time_score > away_full_time_score)
  300. or (away_full_time_score > home_full_time_score)
  301. ):
  302. match_tip.is_correct = True
  303. match_tip.is_active = False
  304. match_tip.save()
  305. # column -2X
  306. elif (tip.column == "2X") and (
  307. (away_full_time_score > home_full_time_score)
  308. or (home_full_time_score == away_full_time_score)
  309. ):
  310. match_tip.is_correct = True
  311. match_tip.is_active = False
  312. match_tip.save()
  313. else:
  314. match_tip.is_correct = False
  315. match_tip.is_active = False
  316. match_tip.save()
  317. elif tip.half == "secondhalf":
  318. # column - 1X
  319. if (tip.column == "1X") and (
  320. (home_full_time_score > away_full_time_score)
  321. or (home_full_time_score == away_full_time_score)
  322. ):
  323. match_tip.is_correct = True
  324. match_tip.is_active = False
  325. match_tip.save()
  326. # column -XX
  327. elif (tip.column == "XX") and (
  328. (home_full_time_score > away_full_time_score)
  329. or (away_full_time_score > home_full_time_score)
  330. ):
  331. match_tip.is_correct = True
  332. match_tip.is_active = False
  333. match_tip.save()
  334. # column -2X
  335. elif (tip.column == "2X") and (
  336. (away_full_time_score > home_full_time_score)
  337. or (home_full_time_score == away_full_time_score)
  338. ):
  339. match_tip.is_correct = True
  340. match_tip.is_active = False
  341. match_tip.save()
  342. else:
  343. match_tip.is_correct = False
  344. match_tip.is_active = False
  345. match_tip.save()
  346.  
  347. if match_tip.tip_onextwo_tip.exists():
  348. for tip in match_tip.tip_onextwo_tip.all():
  349. # Full time
  350. if tip.half == "allmatch":
  351. # column - 1
  352. if (tip.column == "1") and (
  353. home_full_time_score > away_full_time_score
  354. ):
  355. match_tip.is_correct = True
  356. match_tip.is_active = False
  357. match_tip.save()
  358.  
  359. # column - X
  360. elif (tip.column == "X") and (
  361. home_full_time_score == away_full_time_score
  362. ):
  363. match_tip.is_correct = True
  364. match_tip.is_active = False
  365. match_tip.save()
  366.  
  367. # column -2
  368. elif (tip.column == "2") and (
  369. home_full_time_score < away_full_time_score
  370. ):
  371. match_tip.is_correct = True
  372. match_tip.is_active = False
  373. match_tip.save()
  374. else:
  375. match_tip.is_correct = False
  376. match_tip.is_active = False
  377. match_tip.save()
  378.  
  379. # 1st half
  380. elif tip.half == "firsthalf":
  381. # column - 1
  382. if (tip.column == "1") and (
  383. home_full_time_score > away_full_time_score
  384. ):
  385. match_tip.is_correct = True
  386. match_tip.is_active = False
  387. match_tip.save()
  388.  
  389. # column - X
  390. elif (tip.column == "X") and (
  391. home_full_time_score == away_full_time_score
  392. ):
  393. match_tip.is_correct = True
  394. match_tip.is_active = False
  395. match_tip.save()
  396.  
  397. # column -2
  398. elif (tip.column == "2") and (
  399. home_full_time_score < away_full_time_score
  400. ):
  401. match_tip.is_correct = True
  402. match_tip.is_active = False
  403. match_tip.save()
  404. else:
  405. match_tip.is_correct = False
  406. match_tip.is_active = False
  407. match_tip.save()
  408.  
  409. # 2nd half
  410. elif tip.half == "secondhalf":
  411. # column - 1
  412. if (tip.column == "1") and (
  413. home_full_time_score > away_full_time_score
  414. ):
  415. match_tip.is_correct = True
  416. match_tip.is_active = False
  417. match_tip.save()
  418.  
  419. # column - X
  420. elif (tip.column == "X") and (
  421. home_full_time_score == away_full_time_score
  422. ):
  423. match_tip.is_correct = True
  424. match_tip.is_active = False
  425. match_tip.save()
  426.  
  427. # column -2
  428. elif (tip.column == "2") and (
  429. home_full_time_score < away_full_time_score
  430. ):
  431. match_tip.is_correct = True
  432. match_tip.is_active = False
  433. match_tip.save()
  434. else:
  435. match_tip.is_correct = False
  436. match_tip.is_active = False
  437. match_tip.save()
  438.  
  439. if match_tip.tip_ou_tip.exists():
  440. for tip in match_tip.tip_ou_tip.all():
  441. if tip.half == "allmatch":
  442. if "Over" in tip.column:
  443. if ("1.5" in tip.column) and (
  444. (home_full_time_score + away_full_time_score) >= 2
  445. ):
  446. match_tip.is_correct = True
  447. match_tip.is_active = False
  448. match_tip.save()
  449. elif ("2.5" in tip.column) and (
  450. (home_full_time_score + away_full_time_score) >= 2
  451. ):
  452. match_tip.is_correct = True
  453. match_tip.is_active = False
  454. match_tip.save()
  455. elif ("3.5" in tip.column) and (
  456. (home_full_time_score + away_full_time_score) >= 2
  457. ):
  458. match_tip.is_correct = True
  459. match_tip.is_active = False
  460. match_tip.save()
  461. elif ("4.5" in tip.column) and (
  462. (home_full_time_score + away_full_time_score) >= 2
  463. ):
  464. match_tip.is_correct = True
  465. match_tip.is_active = False
  466. match_tip.save()
  467. else:
  468. match_tip.is_correct = False
  469. match_tip.is_active = False
  470. match_tip.save()
  471. elif "Under" in tip.column:
  472. if ("1.5" in tip.column) and (
  473. (home_full_time_score + away_full_time_score) <= 2
  474. ):
  475. match_tip.is_correct = True
  476. match_tip.is_active = False
  477. match_tip.save()
  478. elif ("2.5" in tip.column) and (
  479. (home_full_time_score + away_full_time_score) >= 2
  480. ):
  481. match_tip.is_correct = True
  482. match_tip.is_active = False
  483. match_tip.save()
  484. elif ("3.5" in tip.column) and (
  485. (home_full_time_score + away_full_time_score) >= 2
  486. ):
  487. match_tip.is_correct = True
  488. match_tip.is_active = False
  489. match_tip.save()
  490. elif ("4.5" in tip.column) and (
  491. (home_full_time_score + away_full_time_score) >= 2
  492. ):
  493. match_tip.is_correct = True
  494. match_tip.is_active = False
  495. match_tip.save()
  496. else:
  497. match_tip.is_correct = False
  498. match_tip.is_active = False
  499. match_tip.save()
  500. elif tip.half == "firsthalf":
  501. if "Over" in tip.column:
  502. if ("1.5" in tip.column) and (
  503. (home_full_time_score + away_full_time_score) >= 2
  504. ):
  505. match_tip.is_correct = True
  506. match_tip.is_active = False
  507. match_tip.save()
  508. elif ("2.5" in tip.column) and (
  509. (home_full_time_score + away_full_time_score) >= 2
  510. ):
  511. match_tip.is_correct = True
  512. match_tip.is_active = False
  513. match_tip.save()
  514. elif ("3.5" in tip.column) and (
  515. (home_full_time_score + away_full_time_score) >= 2
  516. ):
  517. match_tip.is_correct = True
  518. match_tip.is_active = False
  519. match_tip.save()
  520. elif ("4.5" in tip.column) and (
  521. (home_full_time_score + away_full_time_score) >= 2
  522. ):
  523. match_tip.is_correct = True
  524. match_tip.is_active = False
  525. match_tip.save()
  526. else:
  527. match_tip.is_correct = False
  528. match_tip.is_active = False
  529. match_tip.save()
  530. elif "Under" in tip.column:
  531. if ("1.5" in tip.column) and (
  532. (home_full_time_score + away_full_time_score) <= 2
  533. ):
  534. match_tip.is_correct = True
  535. match_tip.is_active = False
  536. match_tip.save()
  537. elif ("2.5" in tip.column) and (
  538. (home_full_time_score + away_full_time_score) >= 2
  539. ):
  540. match_tip.is_correct = True
  541. match_tip.is_active = False
  542. match_tip.save()
  543. elif ("3.5" in tip.column) and (
  544. (home_full_time_score + away_full_time_score) >= 2
  545. ):
  546. match_tip.is_correct = True
  547. match_tip.is_active = False
  548. match_tip.save()
  549. elif ("4.5" in tip.column) and (
  550. (home_full_time_score + away_full_time_score) >= 2
  551. ):
  552. match_tip.is_correct = True
  553. match_tip.is_active = False
  554. match_tip.save()
  555. else:
  556. match_tip.is_correct = False
  557. match_tip.is_active = False
  558. match_tip.save()
  559. elif tip.half == "secondhalf":
  560. if "Over" in tip.column:
  561. if ("1.5" in tip.column) and (
  562. (home_full_time_score + away_full_time_score) >= 2
  563. ):
  564. match_tip.is_correct = True
  565. match_tip.is_active = False
  566. match_tip.save()
  567. elif ("2.5" in tip.column) and (
  568. (home_full_time_score + away_full_time_score) >= 2
  569. ):
  570. match_tip.is_correct = True
  571. match_tip.is_active = False
  572. match_tip.save()
  573. elif ("3.5" in tip.column) and (
  574. (home_full_time_score + away_full_time_score) >= 2
  575. ):
  576. match_tip.is_correct = True
  577. match_tip.is_active = False
  578. match_tip.save()
  579. elif ("4.5" in tip.column) and (
  580. (home_full_time_score + away_full_time_score) >= 2
  581. ):
  582. match_tip.is_correct = True
  583. match_tip.is_active = False
  584. match_tip.save()
  585. else:
  586. match_tip.is_correct = False
  587. match_tip.is_active = False
  588. match_tip.save()
  589. elif "Under" in tip.column:
  590. if ("1.5" in tip.column) and (
  591. (home_full_time_score + away_full_time_score) <= 2
  592. ):
  593. match_tip.is_correct = True
  594. match_tip.is_active = False
  595. match_tip.save()
  596. elif ("2.5" in tip.column) and (
  597. (home_full_time_score + away_full_time_score) >= 2
  598. ):
  599. match_tip.is_correct = True
  600. match_tip.is_active = False
  601. match_tip.save()
  602. elif ("3.5" in tip.column) and (
  603. (home_full_time_score + away_full_time_score) >= 2
  604. ):
  605. match_tip.is_correct = True
  606. match_tip.is_active = False
  607. match_tip.save()
  608. elif ("4.5" in tip.column) and (
  609. (home_full_time_score + away_full_time_score) >= 2
  610. ):
  611. match_tip.is_correct = True
  612. match_tip.is_active = False
  613. match_tip.save()
  614. else:
  615. match_tip.is_correct = False
  616. match_tip.is_active = False
  617. match_tip.save()
  618.  
  619.  
  620. def run_task():
  621. check_match(all_fixtures_response)
  622.  
  623. try:
  624. print("mail gonderiliri....")
  625. send_check_msg(all_is_active_tips_count)
  626. print("mail gonderildi")
  627.  
  628. except Exception as e:
  629. print("xeta oldu", e)
  630.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement