Skip to content

Commit

Permalink
Added monitoring system.
Browse files Browse the repository at this point in the history
Added monitoring system which allows you to watch the amount of rice of another user/group live.
This can be enabled with `-m` or `--monitor` for a user, and `-M` or `--monitor-group` for a group.
Some other arguments like `-i` and `--interval`, `-u` and `--user` apply to monitoring mode.
  • Loading branch information
lafkpages authored Jul 8, 2021
1 parent d09f016 commit c482ed1
Show file tree
Hide file tree
Showing 2 changed files with 163 additions and 27 deletions.
109 changes: 104 additions & 5 deletions Freerice.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@ def __init__(self):

self.game = ''

self.name = ''
self.rank = ''
self.avtr = ''

self.members = []

self.rice_total = 0
self.streak = 0

Expand All @@ -47,10 +53,6 @@ def __init__(self, user_id):
}

# ============== URLS ==============
self.user_url = 'https://accounts.freerice.com/users/'
self.user_url2 = '?_format=json'
self.user_mth = 'GET'

self.new_game_url = 'https://engine.freerice.com/games?lang=en'
self.new_game_mth = 'POST'

Expand Down Expand Up @@ -81,6 +83,28 @@ def __init__(self, user_id):

self.manifest_url = 'https://freerice.com/manifest.json'
self.manifest_mth = 'GET'

self.user_url = 'https://engine.freerice.com/users/'
self.user_mth = 'GET'

self.group_url = 'https://engine.freerice.com/groups/'
self.group_mth = 'GET'

self.ldbd_usrs_url = 'https://engine.freerice.com/users?current=' # page number
self.ldbd_usrs_url2 = '&limit=50&_format=json'
self.ldbd_usrs_mthd = 'GET'

self.ldbd_grps_url = 'https://engine.freerice.com/groups?current=' # page number
self.ldbd_grps_url2 = '&limit=50&_format=json'
self.ldbd_grps_mthd = 'GET'

self.prfl_usrs_url = 'https://accounts.freerice.com/public/users?uuids=' # comma-sepparated user IDs
self.prfl_usrs_url2 = '&_format=json'
self.prfl_usrs_mthd = 'GET'

self.prfl_grps_url = 'https://accounts.freerice.com/public/groups?uuids=' # comma-sepparated user IDs
self.prfl_grps_url2 = '&_format=json'
self.prfl_grps_mthd = 'GET'
# ============ END URLS ============


Expand Down Expand Up @@ -219,4 +243,79 @@ def submitAnswer(self, qId, A):

self.last_ret_v = ret

return ret
return ret

def getUserStats(self, user=None, group=False):
if user is None:
user = self.user

URL = ''
if group:
URL = self.group_url + user
else:
URL = self.user_url + user

req = r.request(self.group_mth if group else self.user_mth, URL)

data = Data()
json = {}

try:
json = req.json()
except:
data.error = True

self.last_ret_v = data

return data

data.rice_total = json['data']['attributes']['rice']
data.rank = json['data']['attributes']['rank']
if group:
data.members = json['data']['attributes']['members']

return data

def getUserProfile(self, user=None, group=False):
if user is None:
user = self.user

URL = ''
if group:
URL = self.prfl_grps_url + user + self.prfl_grps_url2
else:
URL = self.prfl_usrs_url + user + self.prfl_usrs_url2

req = r.request(self.prfl_grps_mthd if group else self.prfl_usrs_mthd, URL)
json = {}
data = Data()

'''
{
"...user-id...":{
"uuid": "...user-id...",
"name": "...user-name...",
"avatar": "avatar-..."
}
}
'''

try:
json = req.json()
except:
data.error = True

self.last_ret_v = data

return data

fk = user

try:
data.name = json[fk]['name']
except KeyError:
fk = self.user + self.prfl_usrs_url2
data.name = json[fk]['name']
data.avtr = json[fk]['avatar']

return data
81 changes: 59 additions & 22 deletions Requester.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Hacks
from Freerice import Freerice, ConnectTimeout, FetchDescriptorError
from Freerice import Freerice, ConnectTimeout
try:
from Freerice import FetchDescriptorError
except:
pass

# Timing
from time import sleep
Expand All @@ -22,6 +26,8 @@
# ============= CONFIG =============
# User
user = '14cd5145-e13c-40fe-aad0-7a2bfb31b2f1' # user ID (can be found in LocalStorage > user > uuid)
monitor = False
mntr_gp = False

# Logs
log = True # logging to terminal enabled/disabled
Expand Down Expand Up @@ -56,7 +62,7 @@
logging.critical("\rNo arguments passed.")
else:
try:
_opts, _args = getopt.getopt(sys.argv[1:], "Tt:hu:i:", ["use-tor", "threads", "no-log", "help", "user=", 'interval='])
_opts, _args = getopt.getopt(sys.argv[1:], "Tt:hu:i:mM", ["use-tor", "threads", "no-log", "help", "user=", 'interval=', 'monitor', 'monitor-group'])
except getopt.GetoptError:
logging.debug(sys.argv[1:])
logging.critical("\rArgument parsing error.")
Expand Down Expand Up @@ -98,6 +104,13 @@
secs = int(float(arg))

print('Interval:', secs)
elif opt in {'-m', '-M', '--monitor', '--monitor-group'}:
mntr_gp = not opt in {'-m', '--monitor'}
msg = 'group' if mntr_gp else 'user'

print('Monitor mode: on, for', msg)
print('This will log', msg, 'rice, but will not increment it.')
monitor = True

# Define the hack class with your user id
freerice = Freerice(user) # Pass your user ID
Expand Down Expand Up @@ -131,7 +144,7 @@ def get_network_ip():
def FSUV():
global fsuv

logging.critical('\rFreerice servers are unavailable (HTTP error 429).\nTry changing your IP (via VPN) or enabling Tor (--use-tor or -T).')
logging.critical('\rFreerice servers are unavailable.\nTry changing your IP (via VPN) or enabling Tor (--use-tor or -T).')

try:
ips = [get_local_ip(), get_external_ip(), get_network_ip()]
Expand Down Expand Up @@ -160,15 +173,26 @@ def USRC():

def TC(log_=log):
if log_:
logging.critical('\r User | Total rice | Streak |Games created ')
logging.critical('\r--------------+--------------+--------------+--------------')
logging.critical('\r User | Total rice | Streak | Rank |Games created ')
logging.critical('\r--------------+--------------+--------------+--------------+--------------')

def LogFormatted(*log_data):
global log_sbd, log_tcs

print('\r' + '|'.join(str((' ' * log_sbd) + x).ljust(log_tcs) for x in log_data), end='')

def doSleep():
global secs

if not secs == False:
sleep(secs)

def MainHack(log=False, i=0):
try:
global freerice

# Create a new game if none created
last = False
#last = False
'''
if freerice.game in [[], {}, '', 0, None, False]:
last = freerice.newGame()
Expand All @@ -184,10 +208,7 @@ def MainHack(log=False, i=0):
while True:
# Logs
if log and not (last.rice_total == 0 or last.rice_total == ''):
log_data = ['You', str(last.rice_total), str(last.streak), str(freerice.n_games)]
log_data_formatted = '|'.join(str((' ' * log_sbd) + x).ljust(log_tcs) for x in log_data)

print('\r' + log_data_formatted, end='')
LogFormatted('You', str(last.rice_total), str(last.streak), '', str(freerice.n_games))

if last.error or len(last.question_txt) < 2:
# If error is 'JSON decode error' => Freerice servers unavailable
Expand Down Expand Up @@ -215,8 +236,7 @@ def MainHack(log=False, i=0):
except FetchDescriptorError:
TRER()

if type(secs) in {type(0), type(0.1)}:
sleep(secs)
doSleep()

print('\r', end='')
except KeyboardInterrupt:
Expand All @@ -230,19 +250,36 @@ def MainHack(log=False, i=0):
if freerice.last_ret_v.error:
FSUV()

if threads > 1:
if monitor:
try:
for i in range(threads - 1):
#START THREAD
print('Monitoring ID:', user, '\n')

#logging.critical('Threads started: ' + str(i + 1), end='\r')
TC()

sleep(0.5)

#logging.critical('\nStarting thread %s in main program.' % threads)

MainHack(log, threads - 1)
profile = freerice.getUserProfile(group=mntr_gp)

while True:
data = freerice.getUserStats(group=mntr_gp)

LogFormatted(profile.name, str(data.rice_total), '', str(data.rank), '')

doSleep()
except KeyboardInterrupt:
USRC()
else:
MainHack(log)
if threads > 1:
try:
for i in range(threads - 1):
#START THREAD

#logging.critical('Threads started: ' + str(i + 1), end='\r')

sleep(0.5)

#logging.critical('\nStarting thread %s in main program.' % threads)

MainHack(log, threads - 1)
except KeyboardInterrupt:
USRC()
else:
MainHack(log)

0 comments on commit c482ed1

Please sign in to comment.