generated from oracle-devrel/repo-template
-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathlive_client_producer.py
76 lines (61 loc) · 2.13 KB
/
live_client_producer.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
# Copyright (c) 2021 Oracle and/or its affiliates.
'''
@author jasperan
This file uses RabbitMQ message queues to communicate Live Client API data from one endpoint to the other. You can virtually put your consumer
wherever you want, and let it process the incoming data from the producer.
'''
import yaml
import os
from pathlib import Path
import requests
import pandas as pd
import time
import datetime
import pika
import argparse
from pika.credentials import PlainCredentials
import json
import ast
cli_parser = argparse.ArgumentParser()
cli_parser.add_argument('-i', '--ip', type=str, help='IP address to make requests to', required=True)
args = cli_parser.parse_args()
_MQ_NAME = 'live_client'
credentials = PlainCredentials('league', 'league')
connection = pika.BlockingConnection(
pika.ConnectionParameters(
'{}'.format(args.ip),
5672,
'/',
credentials,
heartbeat=600, blocked_connection_timeout=300))
channel = connection.channel()
channel.queue_declare(queue=_MQ_NAME)
def send_message(queue_name, message):
channel.basic_publish(exchange='', routing_key=queue_name, body='{}'.format(message))
print('{} | MQ {} OK'.format(datetime.datetime.now(), message))
def build_object(content):
# We convert to JSON format
content = response.json()
for x in content['allPlayers']:
del x['items'] # delete items to avoid quotation marks
built_obj = {
'activePlayer': content['activePlayer'],
'allPlayers': content['allPlayers']
}
content = json.dumps(content)
content = content.replace("'", "\"")
print(content)
return content
while True:
try:
response = requests.get('https://127.0.0.1:2999/liveclientdata/allgamedata', verify=False)
except requests.exceptions.ConnectionError:
# Try again every 5 seconds
print('{} | Currently not in game'.format(datetime.datetime.now()))
time.sleep(5)
continue
# Send to RabbitMQ queue.
if response.status_code != 404:
to_send = build_object(response.content)
send_message('live_client', to_send)
time.sleep(30) # wait 30 seconds before making another request