Advertisement
Guest User

Untitled

a guest
Nov 2nd, 2017
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 41.36 KB | None | 0 0
  1. import threading
  2. import constants
  3. import pymysql
  4. import requests
  5. from datetime import datetime
  6. from dateutil.parser import parse
  7. import os
  8. import urllib.request
  9. import shutil
  10. import json
  11.  
  12.  
  13. class CheckStreams(threading.Thread):
  14. def __init__(self, debug):
  15. threading.Thread.__init__(self)
  16. self.debug = debug
  17. self.newGames = False
  18. self.online = 0
  19.  
  20. def get_new_games(self):
  21. return self.newGames
  22.  
  23. def set_new_games(self):
  24. self.newGames = True
  25.  
  26. def get_online(self):
  27. return self.online
  28.  
  29. def set_online(self):
  30. self.online += 1
  31.  
  32. def run(self):
  33.  
  34. # Connect to the database
  35. connect = pymysql.connect(host=constants.HOST,
  36. user=constants.USERNAME,
  37. password=constants.PASSWORD,
  38. db=constants.DATABASE,
  39. charset='utf8mb4',
  40. port=constants.PORT,
  41. cursorclass=pymysql.cursors.DictCursor)
  42. # Get Streamer Table #
  43. try:
  44.  
  45. with connect.cursor() as cursor:
  46.  
  47. # Read all Streamers #
  48. sql = "SELECT * FROM `ls_streamer` s LEFT JOIN `ls_platforms` p ON s.platform = p.id"
  49.  
  50. # Execute Query #
  51. cursor.execute(sql)
  52.  
  53. # Result to streamers Var #
  54. streamers = cursor.fetchall()
  55.  
  56. # Check if we have Streamers at all #
  57. if cursor.rowcount > 0:
  58.  
  59. # Show total Streamers #
  60. if self.debug: print('Total Streamers: %s' % constants.color('green', cursor.rowcount))
  61.  
  62. # Loop through all streamers #
  63. for streamer in streamers:
  64.  
  65. # Output Current Streamer #
  66. if self.debug: print(
  67. 'n%s - %s' % (
  68. streamer["channel_user"], constants.color(streamer["name"], streamer["name"])))
  69.  
  70. # Fetch from Platform #
  71. if streamer["platform"] == 1:
  72.  
  73. # Twitch.tv #
  74. if self.debug: print('Gathering %s' % constants.color('Twitch.tv', 'Twitch.tv'))
  75. # gatherTwitch()
  76. self.gatherTwitch(streamer)
  77. #self.gatherTwitchVods(streamer)
  78.  
  79. elif streamer["platform"] == 2:
  80.  
  81. # Azubu.tv #
  82. if self.debug: print('Gathering %s' % constants.color('Azubu.tv', 'Azubu.tv'))
  83. # self.gatherAzubu(streamer)
  84.  
  85. elif streamer["platform"] == 3:
  86.  
  87. # Beam.pro #
  88. if self.debug: print('Gathering %s' % constants.color('Beam.pro', 'Beam.pro'))
  89. self.gatherBeam(streamer)
  90.  
  91. else:
  92.  
  93. # None #
  94. if self.debug: print(
  95. 'Wrong Platform ID: #%s' % constants.color('red', streamer["platform"]))
  96.  
  97. else:
  98. if self.debug: print('Total Streamers: %s' % constants.color('red', '0'))
  99.  
  100. except Exception:
  101. constants.PrintException()
  102.  
  103. finally:
  104. connect.close()
  105.  
  106.  
  107. def gatherTwitch(self, streamer):
  108.  
  109. # Connect to the database
  110. connect = pymysql.connect(host=constants.HOST,
  111. user=constants.USERNAME,
  112. password=constants.PASSWORD,
  113. db=constants.DATABASE,
  114. charset='utf8mb4',
  115. port=constants.PORT,
  116. cursorclass=pymysql.cursors.DictCursor)
  117.  
  118. # Internal Vars #
  119. isOnline = False
  120. description = None
  121. language = None
  122. thumbnail = None
  123. setOffline = False
  124. banner = "None"
  125. logo = "None"
  126. partner = False
  127.  
  128. # Create Stream Url from Vars #
  129. streamUrl = '%s%s' % (streamer["url"], streamer["channel_id"])
  130.  
  131. # Send Request w/ Twitch Headers #
  132. r = requests.get(
  133. streamUrl,
  134. headers=
  135. {
  136. "Accept": "application/vnd.twitchtv.v5+json",
  137. "Client-ID": constants.TWITCH_CLIENT_ID
  138. }
  139. )
  140.  
  141. # Check Response Code #
  142. if r.status_code == 200:
  143.  
  144. # JSON #
  145. data = r.json()
  146.  
  147. # Check if Stream is valid #
  148. if "stream" in data.keys():
  149.  
  150. # Get Info #
  151. streamData = data["stream"]
  152. if type(streamData) is dict:
  153.  
  154. # Check if Streaming LoL #
  155. if streamData["game"].lower() == "league of legends":
  156.  
  157. # Streaming LoL - YAY #
  158. if self.debug: print('%s' % constants.color('green', 'IS STREAMING'))
  159.  
  160. # Gather Vars #
  161. lastSeen = datetime.now()
  162. if streamer["is_online"] == True:
  163. lastSeen = streamer["modified"]
  164.  
  165. totalStreamed = streamer["total_online"]
  166. totalStreamed += constants.convertTimeDelta((datetime.now() - lastSeen))
  167.  
  168. viewers = streamData["viewers"]
  169. resolution = streamData["video_height"]
  170. fps = streamData["average_fps"]
  171. delay = streamData["delay"]
  172. started = parse(streamData["created_at"], None, ignoretz=True)
  173. isOnline = True
  174.  
  175. # Channel Data #
  176. channelData = streamData["channel"]
  177. if type(channelData) is dict:
  178.  
  179. description = channelData["status"]
  180. language = channelData["language"]
  181. logo = channelData["logo"]
  182. banner = channelData["profile_banner"]
  183. if channelData["partner"] == "true":
  184. partner = True
  185. if channelData["partner"] is True:
  186. partner = True
  187.  
  188. else:
  189. # Output Debug #
  190. if self.debug: print('Channel Data %s' % constants.color('red', 'NOT FOUND'))
  191.  
  192. # Peview Data #
  193. previewData = streamData["preview"]
  194. if type(previewData) is dict:
  195.  
  196. thumbnail = previewData["medium"]
  197.  
  198. else:
  199. # Output Debug #
  200. if self.debug: print('Preview Data %s' % constants.color('red', 'NOT FOUND'))
  201.  
  202. try:
  203. with connect.cursor() as cursorStreamer:
  204.  
  205. # Got all Vars put to DB #
  206. sqlUpdate = """
  207. UPDATE
  208. `ls_streamer`
  209. SET
  210. total_online = %s,
  211. viewers = %s,
  212. resolution = %s,
  213. fps = %s,
  214. delay = %s,
  215. description = %s,
  216. language = %s,
  217. thumbnail = %s,
  218. logo = %s,
  219. banner = %s,
  220. started = %s,
  221. is_online = %s,
  222. modified = %s,
  223. is_partner = %s
  224. WHERE
  225. id = %s
  226. """
  227.  
  228. # Execute Query #
  229. connect.escape(sqlUpdate)
  230. cursorStreamer.execute(sqlUpdate, (
  231. totalStreamed, viewers, resolution, fps, delay, description, language, thumbnail,
  232. logo, banner, started, isOnline, datetime.now(), partner, streamer["id"]))
  233. connect.commit()
  234. self.set_online()
  235.  
  236. # Now crawl the Riot API for Live Games for all connected summoners #
  237. self.getLiveGames(streamer)
  238.  
  239. except Exception:
  240. constants.PrintException()
  241.  
  242. finally:
  243. if self.debug: print('Update ls_streamer: %s' % constants.color('green', 'SUCCESS'))
  244.  
  245. else:
  246.  
  247. # Set Offline #
  248. setOffline = True
  249. # Stream online but not streaming LoL #
  250. if self.debug: print(
  251. 'Stream is %s but %s streaming LoL' % (
  252. constants.color('green', 'online'), constants.color('red', 'NOT')))
  253.  
  254. else:
  255.  
  256. # Set Offline #
  257. setOffline = True
  258. # Output Debug #
  259. if self.debug: print('Streamer is %s' % constants.color('red', 'OFFLINE'))
  260.  
  261. else:
  262.  
  263. # Set Offline #
  264. setOffline = True
  265. # Output Debug #
  266. if self.debug: print('Streamer is %s' % constants.color('red', 'OFFLINE'))
  267.  
  268. else:
  269.  
  270. # Set Offline #
  271. setOffline = True
  272.  
  273. # Output Debug #
  274. if self.debug: print('->%s %s' % ('Response Code', constants.color('red', r.status_code)))
  275.  
  276. # Output Url #
  277. if self.debug: print('Streamer Url: %s' % streamUrl)
  278.  
  279. if setOffline == True:
  280. try:
  281. with connect.cursor() as cursorStreamerOff:
  282.  
  283. # Got all Vars put to DB #
  284. sqlOffline = """
  285. UPDATE
  286. `ls_streamer`
  287. SET
  288. is_online = %s
  289. WHERE
  290. id = %s
  291. """
  292.  
  293. # Execute Query #
  294. connect.escape(sqlOffline)
  295. cursorStreamerOff.execute(sqlOffline, (False, streamer["id"]))
  296. connect.commit()
  297.  
  298. except Exception:
  299. constants.PrintException()
  300.  
  301. # Create Stream Url from Vars #
  302. streamUrlVod = '%s%s/videos?broadcast_type=archive&limit=100' % (
  303. streamer["channelurl"], streamer["channel_id"])
  304.  
  305. # Send Request w/ Twitch Headers #
  306. rVod = requests.get(
  307. streamUrlVod,
  308. headers=
  309. {
  310. "Accept": "application/vnd.twitchtv.v5+json",
  311. "Client-ID": constants.TWITCH_CLIENT_ID
  312. }
  313. )
  314.  
  315. # Check Response Code #
  316. if rVod.status_code == 200:
  317.  
  318. # JSON #
  319. data = rVod.json()
  320.  
  321. # Check if Stream is valid #
  322. if "videos" in data.keys():
  323. # Get Info #
  324. videos = data["videos"]
  325.  
  326. for video in videos:
  327.  
  328. thumb = video["preview"]["medium"]
  329. video_id = video["_id"]
  330. length = video["length"]
  331. created = video["created_at"]
  332. if video["game"] == "League of Legends" and video["viewable"] == "public":
  333.  
  334. # Check if video exists #
  335. try:
  336. with connect.cursor() as cursorVod:
  337. sqlInsert = """
  338. INSERT INTO
  339. `ls_vods`
  340. (streamer_id, thumbnail, video_id, created, `length`, last_check)
  341. VALUES
  342. (%s, %s, %s, %s, %s, %s)
  343. ON DUPLICATE KEY UPDATE
  344. last_check = %s,
  345. thumbnail = %s,
  346. `length` = %s
  347. """
  348.  
  349. cursorVod.execute(sqlInsert,
  350. (streamer["id"], thumb, video_id, created, length, datetime.now(), datetime.now(), thumb, length))
  351. connect.commit()
  352. except Exception:
  353. constants.PrintException()
  354. else:
  355. if self.debug: print('Videos not found in URL: %s' % streamUrl)
  356. else:
  357. # Output Debug #
  358. if self.debug: print('->%s %s' % ('Response Code', constants.color('red', r.status_code)))
  359.  
  360. # Output Url #
  361. if self.debug: print('Streamer Url: %s' % streamUrl)
  362.  
  363.  
  364.  
  365. def gatherAzubu(self, streamer):
  366. # Connect to the database
  367. connect = pymysql.connect(host=constants.HOST,
  368. user=constants.USERNAME,
  369. password=constants.PASSWORD,
  370. db=constants.DATABASE,
  371. charset='utf8mb4',
  372. port=constants.PORT,
  373. cursorclass=pymysql.cursors.DictCursor)
  374.  
  375. description = None
  376. language = None
  377. thumbnail = None
  378. setOffline = False
  379. logo = "NONE"
  380. banner = "NONE"
  381.  
  382. # Create Stream Url from Vars #
  383. streamUrl = '%s%s' % (streamer["url"], streamer["channel_name"])
  384.  
  385. r = requests.get(
  386. streamUrl
  387. )
  388.  
  389. # Check Response Code #
  390. if r.status_code == 200:
  391.  
  392. # JSON #
  393. data = r.json()
  394.  
  395. # Check if Stream is valid #
  396. if "data" in data.keys():
  397.  
  398. # Get Info #
  399. streamData = data["data"]
  400. if type(streamData) is dict:
  401.  
  402. # Check Live #
  403. if data["data"]["is_live"] == True:
  404.  
  405. # Check Game #
  406. if data["data"]["category"]["name"] == "league-of-legends":
  407.  
  408. # Streaming LoL - YAY #
  409. if self.debug: print('%s' % constants.color('green', 'IS STREAMING'))
  410.  
  411. # Gather Vars #
  412. lastSeen = datetime.now()
  413. if streamer["is_online"] == True:
  414. lastSeen = streamer["modified"]
  415.  
  416. totalStreamed = streamer["total_online"]
  417. totalStreamed += constants.convertTimeDelta((datetime.now() - lastSeen))
  418.  
  419. viewers = data["data"]["view_count"]
  420. started = parse(data["data"]["created_at"], None, ignoretz=True)
  421. isOnline = True
  422. description = data["data"]["title"]
  423.  
  424. language = data["data"]["language"]
  425. thumbnail = "https://leaguestreams.net/thumbs/%s_azubu.jpg" % streamer["channel_name"]
  426.  
  427. fileDir = os.path.dirname(os.path.realpath('__file__'))
  428. filename = os.path.join('/var/www/html/ls/',
  429. 'web/thumbs/%s_azubu.jpg' % streamer["channel_name"])
  430.  
  431. # Download the Thumbnail to avoid https error:
  432. with urllib.request.urlopen(data["data"]["url_thumbnail_mobile"]) as response, open(
  433. filename, 'wb') as out_file:
  434. shutil.copyfileobj(response, out_file)
  435.  
  436. # Not in Api #
  437. resolution = None
  438. fps = None
  439. delay = None
  440.  
  441. try:
  442. with connect.cursor() as cursorStreamer:
  443.  
  444. # Got all Vars put to DB #
  445. sqlUpdate = """
  446. UPDATE
  447. `ls_streamer`
  448. SET
  449. total_online = %s,
  450. viewers = %s,
  451. resolution = %s,
  452. fps = %s,
  453. delay = %s,
  454. description = %s,
  455. language = %s,
  456. thumbnail = %s,
  457. logo = %s,
  458. banner = %s,
  459. started = %s,
  460. is_online = %s,
  461. modified = %s
  462. WHERE
  463. id = %s
  464. """
  465.  
  466. # Execute Query #
  467. connect.escape(sqlUpdate)
  468. cursorStreamer.execute(sqlUpdate, (
  469. totalStreamed, viewers, resolution, fps, delay, description, language,
  470. thumbnail,
  471. logo, banner, started, isOnline, datetime.now(), streamer["id"]))
  472. connect.commit()
  473. self.set_online()
  474.  
  475. # Now crawl the Riot API for Live Games for all connected summoners #
  476. self.getLiveGames(streamer)
  477.  
  478. except Exception:
  479. constants.PrintException()
  480.  
  481. finally:
  482. if self.debug: print('Update ls_streamer: %s' % constants.color('green', 'SUCCESS'))
  483.  
  484. else:
  485.  
  486. # Set Offline #
  487. setOffline = True
  488. # Stream online but not streaming LoL #
  489. if self.debug: print(
  490. 'Stream is %s but %s streaming LoL' % (
  491. constants.color('green', 'online'), constants.color('red', 'NOT')))
  492.  
  493. else:
  494.  
  495. # Set Offline #
  496. setOffline = True
  497. # Output Debug #
  498. if self.debug: print('Streamer is %s' % constants.color('red', 'OFFLINE'))
  499.  
  500. else:
  501.  
  502. # Set Offline #
  503. setOffline = True
  504. # Output Debug #
  505. if self.debug: print('Streamer is %s' % constants.color('red', 'OFFLINE'))
  506.  
  507. else:
  508.  
  509. # Set Offline #
  510. setOffline = True
  511. # Output Debug #
  512. if self.debug: print('Streamer is %s' % constants.color('red', 'OFFLINE'))
  513.  
  514. else:
  515.  
  516. # Set Offline #
  517. setOffline = True
  518.  
  519. # Output Debug #
  520. if self.debug: print('->%s %s' % ('Response Code', constants.color('red', r.status_code)))
  521.  
  522. # Output Url #
  523. if self.debug: print('Streamer Url: %s' % streamUrl)
  524.  
  525. if setOffline == True:
  526. try:
  527. with connect.cursor() as cursorStreamerOff:
  528.  
  529. # Got all Vars put to DB #
  530. sqlOffline = """
  531. UPDATE
  532. `ls_streamer`
  533. SET
  534. is_online = %s
  535. WHERE
  536. id = %s
  537. """
  538.  
  539. # Execute Query #
  540. connect.escape(sqlOffline)
  541. cursorStreamerOff.execute(sqlOffline, (False, streamer["id"]))
  542. connect.commit()
  543.  
  544. except Exception:
  545. constants.PrintException()
  546.  
  547. def gatherBeam(self, streamer):
  548. # Connect to the database
  549. connect = pymysql.connect(host=constants.HOST,
  550. user=constants.USERNAME,
  551. password=constants.PASSWORD,
  552. db=constants.DATABASE,
  553. charset='utf8mb4',
  554. port=constants.PORT,
  555. cursorclass=pymysql.cursors.DictCursor)
  556.  
  557. description = None
  558. language = None
  559. thumbnail = None
  560. setOffline = False
  561. logo = "NONE"
  562. banner = "NONE"
  563.  
  564. # Create Stream Url from Vars #
  565. streamUrl = '%s%s' % (streamer["url"], streamer["channel_name"])
  566.  
  567. r = requests.get(
  568. streamUrl
  569. )
  570.  
  571. # Check Response Code #
  572. if r.status_code == 200:
  573.  
  574. # JSON #
  575. data = r.json()
  576.  
  577. # Check if Stream is valid #
  578. if "user" in data.keys():
  579.  
  580. # Get Info #
  581. userData = data["user"]
  582. if type(userData) is dict:
  583.  
  584. # Check Live #
  585. if userData["channel"]["online"] == True:
  586.  
  587. # Check Game #
  588. if data["type"]["name"] == "League of Legends":
  589.  
  590. # Streaming LoL - YAY #
  591. if self.debug: print('%s' % constants.color('green', 'IS STREAMING'))
  592.  
  593. # Gather Vars #
  594. lastSeen = datetime.now()
  595. if streamer["is_online"] == True:
  596. lastSeen = streamer["modified"]
  597.  
  598. totalStreamed = streamer["total_online"]
  599. totalStreamed += constants.convertTimeDelta((datetime.now() - lastSeen))
  600.  
  601. viewers = data["user"]["channel"]["viewersCurrent"]
  602. started = parse(data["user"]["channel"]["updatedAt"], None, ignoretz=True)
  603. isOnline = True
  604. description = data["user"]["channel"]["name"]
  605.  
  606. language = "en"
  607. thumbnail = data["thumbnail"]["url"]
  608.  
  609. # Not in Api #
  610. resolution = None
  611. fps = None
  612. delay = None
  613.  
  614. try:
  615. with connect.cursor() as cursorStreamer:
  616.  
  617. # Got all Vars put to DB #
  618. sqlUpdate = """
  619. UPDATE
  620. `ls_streamer`
  621. SET
  622. total_online = %s,
  623. viewers = %s,
  624. resolution = %s,
  625. fps = %s,
  626. delay = %s,
  627. description = %s,
  628. language = %s,
  629. thumbnail = %s,
  630. logo = %s,
  631. banner = %s,
  632. started = %s,
  633. is_online = %s,
  634. modified = %s
  635. WHERE
  636. id = %s
  637. """
  638.  
  639. # Execute Query #
  640. connect.escape(sqlUpdate)
  641. cursorStreamer.execute(sqlUpdate, (
  642. totalStreamed, viewers, resolution, fps, delay, description, language,
  643. thumbnail,
  644. logo, banner, started, isOnline, datetime.now(), streamer["id"]))
  645. connect.commit()
  646. self.set_online()
  647.  
  648. # Now crawl the Riot API for Live Games for all connected summoners #
  649. self.getLiveGames(streamer)
  650.  
  651. except Exception:
  652. constants.PrintException()
  653.  
  654. finally:
  655. if self.debug: print('Update ls_streamer: %s' % constants.color('green', 'SUCCESS'))
  656.  
  657. else:
  658.  
  659. # Set Offline #
  660. setOffline = True
  661. # Stream online but not streaming LoL #
  662. if self.debug: print(
  663. 'Stream is %s but %s streaming LoL' % (
  664. constants.color('green', 'online'), constants.color('red', 'NOT')))
  665.  
  666. else:
  667.  
  668. # Set Offline #
  669. setOffline = True
  670. # Output Debug #
  671. if self.debug: print('Streamer is %s' % constants.color('red', 'OFFLINE'))
  672.  
  673. else:
  674.  
  675. # Set Offline #
  676. setOffline = True
  677. # Output Debug #
  678. if self.debug: print('Streamer is %s' % constants.color('red', 'OFFLINE'))
  679.  
  680. else:
  681.  
  682. # Set Offline #
  683. setOffline = True
  684. # Output Debug #
  685. if self.debug: print('Streamer is %s' % constants.color('red', 'OFFLINE'))
  686.  
  687. else:
  688.  
  689. # Set Offline #
  690. setOffline = True
  691.  
  692. # Output Debug #
  693. if self.debug: print('->%s %s' % ('Response Code', constants.color('red', r.status_code)))
  694.  
  695. # Output Url #
  696. if self.debug: print('Streamer Url: %s' % streamUrl)
  697.  
  698. if setOffline == True:
  699. try:
  700. with connect.cursor() as cursorStreamerOff:
  701.  
  702. # Got all Vars put to DB #
  703. sqlOffline = """
  704. UPDATE
  705. `ls_streamer`
  706. SET
  707. is_online = %s
  708. WHERE
  709. id = %s
  710. """
  711.  
  712. # Execute Query #
  713. connect.escape(sqlOffline)
  714. cursorStreamerOff.execute(sqlOffline, (False, streamer["id"]))
  715. connect.commit()
  716.  
  717. except Exception:
  718. constants.PrintException()
  719.  
  720. def getLiveGames(self, streamer):
  721.  
  722. # Connect to the database
  723. connect = pymysql.connect(host=constants.HOST,
  724. user=constants.USERNAME,
  725. password=constants.PASSWORD,
  726. db=constants.DATABASE,
  727. charset='utf8mb4',
  728. port=constants.PORT,
  729. cursorclass=pymysql.cursors.DictCursor)
  730.  
  731. # Select all Summoners from Streamer #
  732. try:
  733. with connect.cursor() as cursorSummoner:
  734. sqlSummoner = """
  735. SELECT * FROM
  736. `ls_summoner` s
  737. LEFT JOIN
  738. `ls_regions` r
  739. ON
  740. r.id = s.region_id
  741. WHERE
  742. streamer_id = %s
  743. """
  744.  
  745. # Execute Query #
  746. cursorSummoner.execute(sqlSummoner, (streamer["id"],))
  747. summoners = cursorSummoner.fetchall()
  748.  
  749. if cursorSummoner.rowcount > 0:
  750.  
  751. for summoner in summoners:
  752.  
  753. # Output which Summoner #
  754. if self.debug: print('Checking Live Game for Summoner %s-%s' % (
  755. summoner["short"].upper(), summoner["name"]))
  756.  
  757. urlLive = "https://%s.api.riotgames.com/lol/spectator/v3/active-games/by-summoner/%s?api_key=%s" % (
  758. summoner["long"].lower(), summoner["summoner_id"],
  759. constants.RIOT_API_KEY)
  760.  
  761. r = requests.get(urlLive)
  762.  
  763. if self.debug: print("Checking %s" % urlLive)
  764.  
  765. # Check Response Code #
  766. if r.status_code == 200:
  767.  
  768. # Output #
  769. if self.debug: print('->%s Found Live Game for %s-%s' % (
  770. constants.color('green', 'FOUND'), summoner["short"].upper(), summoner["name"]))
  771.  
  772. # JSON #
  773. game = r.json()
  774.  
  775. # Gather Vars #
  776. map = game["mapId"]
  777. gameId = game["gameId"]
  778. gameMode = game["gameMode"]
  779. gameType = game["gameType"]
  780. queueId = game["gameQueueConfigId"]
  781. played = game["gameLength"]
  782. spell1Id = None
  783. spell2Id = None
  784. teamId = None
  785. champion = None
  786. runes = None
  787. masteries = None
  788.  
  789. pp = game["participants"]
  790. if type(pp) is list:
  791. for part in pp:
  792. if part["summonerId"] == summoner["summoner_id"]:
  793. teamId = part["teamId"]
  794. spell1Id = part["spell1Id"]
  795. spell2Id = part["spell2Id"]
  796. champion = part["championId"]
  797. runes = json.dumps(part["runes"])
  798. masteries = json.dumps(part["masteries"])
  799.  
  800. if self.debug: print('Game %s. Game ID #%s' % (constants.color('green', 'found'), gameId))
  801.  
  802. # MySQL #
  803. try:
  804. with connect.cursor() as cursorUpdateCurrent:
  805.  
  806. # Check Champion in DB #
  807. sqlChampion = "SELECT * FROM `ls_champions` WHERE id = %s"
  808. cursorUpdateCurrent.execute(sqlChampion,
  809. (champion,))
  810. if cursorUpdateCurrent.rowcount == 0:
  811. # Champion not in DB TODO: SEND MAIL #
  812. if self.debug: print("Champion %",
  813. constants.color('red',
  814. 'NOT IN DB'))
  815. # Exit current Game Update #
  816. break
  817.  
  818. # Check Map in DB #
  819. sqlMap = "SELECT * FROM `ls_maps` WHERE id = %s"
  820. cursorUpdateCurrent.execute(sqlMap,
  821. (map,))
  822. if cursorUpdateCurrent.rowcount == 0:
  823. # Map not in DB TODO: SEND MAIL #
  824. if self.debug: print("Map %",
  825. constants.color('red',
  826. 'NOT IN DB'))
  827. # Exit current Game Update #
  828. break
  829.  
  830. # Check Queue in DB #
  831. sqlQueue = "SELECT * FROM `ls_queues` WHERE id = %s"
  832. cursorUpdateCurrent.execute(sqlQueue,
  833. (queueId,))
  834. if cursorUpdateCurrent.rowcount == 0:
  835. # Queue not in DB TODO: SEND MAIL #
  836. if self.debug: print("Queue %",
  837. constants.color('red',
  838. 'NOT IN DB'))
  839. # Exit current Game Update #
  840. break
  841.  
  842. # Check P1Spell1 in DB #
  843. sqlP1Spell1 = "SELECT * FROM `ls_spells` WHERE id = %s"
  844. cursorUpdateCurrent.execute(sqlP1Spell1,
  845. (spell1Id,))
  846. if cursorUpdateCurrent.rowcount == 0:
  847. # P1Spell1 not in DB TODO: SEND MAIL #
  848. if self.debug: print("P1Spell1 %",
  849. constants.color('red',
  850. 'NOT IN DB'))
  851. # Exit current Game Update #
  852. break
  853.  
  854. # Check P1Spell2 in DB #
  855. sqlP1Spell2 = "SELECT * FROM `ls_spells` WHERE id = %s"
  856. cursorUpdateCurrent.execute(
  857. sqlP1Spell2,
  858. (spell2Id,))
  859. if cursorUpdateCurrent.rowcount == 0:
  860. # P1Spell2 not in DB TODO: SEND MAIL #
  861. if self.debug: print("P1Spell2 %",
  862. constants.color('red',
  863. 'NOT IN DB'))
  864. # Exit current Game Update #
  865. break
  866.  
  867. # Select Current Match #
  868. sqlSelectCurrent = " SELECT * FROM `ls_current_match` WHERE summoner_id = %s"
  869.  
  870. cursorUpdateCurrent.execute(sqlSelectCurrent, (summoner["id"]))
  871.  
  872. if cursorUpdateCurrent.rowcount > 0:
  873.  
  874. # Found a Current Match #
  875. sqlUpdateCurrent = """
  876. UPDATE
  877. `ls_current_match`
  878. SET
  879. champion_id = %s,
  880. map_id = %s,
  881. summoner_id = %s,
  882. queue_id = %s,
  883. match_id = %s,
  884. team = %s,
  885. length = %s,
  886. type = %s,
  887. mode = %s,
  888. modified = %s,
  889. p1_spell1_id = %s,
  890. p1_spell2_id = %s,
  891. is_playing = %s,
  892. runes = %s,
  893. masteries = %s
  894. WHERE
  895. summoner_id = %s
  896. """
  897.  
  898. cursorUpdateCurrent.execute(
  899. sqlUpdateCurrent, (
  900. champion,
  901. map,
  902. summoner["id"],
  903. queueId,
  904. gameId,
  905. teamId,
  906. played,
  907. gameType,
  908. gameMode,
  909. datetime.now(),
  910. spell1Id,
  911. spell2Id,
  912. True,
  913. runes,
  914. masteries, summoner["id"]
  915. ))
  916.  
  917. connect.commit()
  918. if self.debug: print(
  919. '%s Current Match in Databasen' % constants.color('yellow',
  920. 'UPDATED'))
  921.  
  922.  
  923.  
  924. else:
  925.  
  926. # No current Match for Summoner Id found #
  927. sqlNewCurrent = """
  928. INSERT INTO
  929. `ls_current_match`
  930. (champion_id, map_id, summoner_id, queue_id, match_id, team, length, type, mode, modified, p1_spell1_id, p1_spell2_id, is_playing, runes, masteries)
  931. VALUES
  932. (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)
  933. """
  934.  
  935. cursorUpdateCurrent.execute(sqlNewCurrent, (
  936. champion,
  937. map,
  938. summoner["id"],
  939. queueId,
  940. gameId,
  941. teamId,
  942. played,
  943. gameType,
  944. gameMode,
  945. datetime.now(),
  946. spell1Id,
  947. spell2Id,
  948. True,
  949. runes,
  950. masteries
  951. ))
  952.  
  953. connect.commit()
  954. if self.debug: print(
  955. '%s Current Match in Databasen' % constants.color(
  956. 'green', 'INSERTED'))
  957.  
  958.  
  959.  
  960.  
  961.  
  962. except Exception:
  963. constants.PrintException()
  964.  
  965. # TODO: ENTER TO MATCHES DB #
  966. # RESONS: If Game Ends and new One Starts before its detected in the While Loop, it gets overwritten #
  967.  
  968. # Exit if One Playing found #
  969. break
  970.  
  971.  
  972. else:
  973. # Response Code not 200 #
  974. if self.debug: print(
  975. '->%s %s' % ('Response Code', constants.color('red', r.status_code)))
  976.  
  977. # Reset Games in Current Matches #
  978. try:
  979. with connect.cursor() as cursorUpdateCurrent:
  980. # Select Current Match #
  981. sqlSelectCurrent = "SELECT * FROM `ls_current_match` WHERE summoner_id = %s"
  982. cursorUpdateCurrent.execute(sqlSelectCurrent, (summoner["id"]))
  983.  
  984. if cursorUpdateCurrent.rowcount == 0:
  985.  
  986. # Output not found in Current Match -> has never played #
  987. if self.debug: print(
  988. 'Not found, Summoner %s' % constants.color('yellow', 'NEVER PLAYED'))
  989.  
  990. else:
  991.  
  992. # Set isPlaying = false #
  993. sqlSelectCurrent = "UPDATE `ls_current_match` SET is_playing = %s WHERE summoner_id = %s"
  994. cursorUpdateCurrent.execute(sqlSelectCurrent, (False, summoner["id"]))
  995. connect.commit()
  996.  
  997. if self.debug: print(
  998. '%s Current Match in Database, set isPlaying = falsen' % constants.color(
  999. 'green', 'UPDATED'))
  1000.  
  1001. # Create new Matchc in MatchHistory #
  1002. sqlSelectCurrentRe = " SELECT * FROM `ls_current_match` WHERE summoner_id = %s"
  1003. cursorUpdateCurrent.execute(sqlSelectCurrentRe, (summoner["id"]))
  1004. cG = cursorUpdateCurrent.fetchone()
  1005.  
  1006. # See if Match in MatchHistory ->should not be the case #
  1007. sqlSelectMatch = "SELECT * FROM `ls_matches` WHERE match_id = %s AND streamer = %s"
  1008. cursorUpdateCurrent.execute(sqlSelectMatch, (cG["match_id"], streamer["id"]))
  1009.  
  1010. if cursorUpdateCurrent.rowcount == 0:
  1011.  
  1012. sqlNewMatch = """
  1013. INSERT INTO
  1014. `ls_matches`
  1015. (streamer, champion, map, match_id, team, lane, role, length, type, win, modified, crawled, summoner, region)
  1016. VALUES
  1017. (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)
  1018. """
  1019.  
  1020. cursorUpdateCurrent.execute(
  1021. sqlNewMatch, (
  1022. streamer["id"],
  1023. cG["champion_id"],
  1024. cG["map_id"],
  1025. cG["match_id"],
  1026. cG["team"],
  1027. 'NONE',
  1028. 'NONE',
  1029. cG["length"],
  1030. cG["type"],
  1031. 1,
  1032. datetime.now(),
  1033. False,
  1034. cG["summoner_id"],
  1035. summoner["region_id"]
  1036.  
  1037. ))
  1038.  
  1039. connect.commit()
  1040.  
  1041. else:
  1042.  
  1043. # Output that Match already exists !?? #
  1044. if self.debug: print('Match-ID %s %s' % (
  1045. cG["match_id"], constants.color('yellow', 'already crawled')))
  1046.  
  1047. self.set_new_games()
  1048.  
  1049. except Exception:
  1050. constants.PrintException()
  1051. else:
  1052. # No Summoners for Streamer #
  1053. if self.debug: print("%s found" % constants.color('red', 'NO SUMMONERS'))
  1054.  
  1055. except Exception:
  1056. constants.PrintException()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement