Forked from gene1wood/get_redpocket_balance.py
Last active
September 30, 2019 05:16
-
-
Save toddq/0c0e132df42ffd7bc5e164b313d823aa to your computer and use it in GitHub Desktop.
Tool to fetch a Red Pocket account's balance of minutes, texts and data and send me a push notification if the balance is low.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import logging | |
from bs4 import BeautifulSoup | |
import requests | |
import yaml | |
import json | |
import os.path | |
import datetime | |
import re | |
import sys | |
# Adapted from https://gist.github.com/gene1wood/236f1dcc082950afb4851a382951820c | |
logging.basicConfig() | |
logger = logging.getLogger() | |
loglevel = logging.DEBUG if '-v' in sys.argv else logging.INFO | |
logger.setLevel(loglevel) | |
WARN_VOICE = 60 | |
WARN_MESSAGING = 100 | |
WARN_DATA = 50 | |
def process_user(user): | |
logger.debug('process user: {}'.format(user)) | |
session = requests.Session() | |
page = session.get('https://www.redpocket.com/login') | |
soup = BeautifulSoup(page.text, 'html.parser') | |
csrf = soup.select( | |
'body div.content div.container div form.form-signin input[name=csrf]' | |
)[0]['value'] | |
logger.debug('CSRF is {}'.format(csrf)) | |
data = { | |
'mdn': user['username'], | |
'password': user['password'], | |
'remember_me': 1, | |
'redirect_url': '', | |
'csrf': csrf | |
} | |
session.post( | |
'https://www.redpocket.com/login', | |
data=data) | |
data = {'csrf_token': csrf} | |
page = session.post( | |
'https://www.redpocket.com/user/get-account-info', | |
data=data, | |
) | |
result = page.json() | |
logger.debug('get-account-info : {}'.format(result)) | |
total = {} | |
user_output = { | |
'datetime': "%s" % datetime.datetime.now(), | |
'plan_name': result['plan'], | |
'expiration_date': result['aed']} | |
logger.debug('Plan : {}'.format(result['plan'])) | |
user_output['voice_balance'] = int(result.get('voice_balance')) | |
user_output['messaging_balance'] = int(result.get('messaging_balance')) | |
user_output['data_balance'] = float(result.get('data_balance')) | |
user_output['status'] = True | |
user_output['description'] = '' | |
if user_output['voice_balance'] < WARN_VOICE: | |
user_output['status'] = False | |
user_output['description'] += ( | |
'Remaining voice balance is only %s minutes. ' % | |
user_output['voice_balance']) | |
if user_output['messaging_balance'] < WARN_MESSAGING: | |
user_output['status'] = False | |
user_output['description'] += ( | |
'Remaining SMS balance is only %s texts. ' % | |
user_output['messaging_balance']) | |
if user_output['data_balance'] < WARN_DATA: | |
user_output['status'] = False | |
user_output['description'] += ( | |
'Remaining data balance is only %s MB. ' % | |
user_output['data_balance']) | |
if user_output['status'] == False: | |
user_output['description'] += ( | |
'Next renewal is %s' % | |
user_output['expiration_date'] | |
) | |
return user_output | |
def notify_user(msg, config): | |
logger.info(' Sending Pushover notification') | |
data = { | |
'token': config['pushover_key'], | |
'user': config['pushover_user'], | |
'message': msg | |
} | |
response = requests.post('https://api.pushover.net/1/messages.json', data = data) | |
logger.debug(response) | |
if response.status_code != 200: | |
logger.warning(' Failed to send warning notification') | |
return False | |
return True | |
def main(): | |
with open(os.path.expanduser('~/.redpocket.conf')) as f: | |
config = yaml.load(f, Loader=yaml.SafeLoader) | |
logger.debug(config) | |
logger.info('Checking account status for %s - %s', config['username'], datetime.datetime.now()) | |
previous_output = {} | |
if os.path.exists('redpocket-balance.json'): | |
with open('redpocket-balance.json', 'r') as f: | |
previous_output = json.load(f) | |
user_output = {} | |
output = {} | |
try: | |
user_output = process_user(config) | |
except: | |
logger.error(' Failed to get user account data') | |
notify_user('Failed to get user account data', config) | |
raise SystemExit(1) | |
output[config['username']] = user_output | |
if not user_output['status']: | |
logger.info(' %s', user_output['description']) | |
if previous_output[config['username']]['status'] == True: | |
if not notify_user(user_output['description'], config): | |
# notification failed, reset status state so it will try again | |
user_output['status'] = True | |
else: | |
logger.info(' %s has been previously notified', config['username']) | |
logger.debug('Result : {}'.format(output)) | |
if user_output['description'] == '': | |
logger.info('Everything looks good') | |
with open('redpocket-balance.json', 'w') as f: | |
json.dump(output, f, indent=4, sort_keys=True) | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
~/.redpocket.conf
should look like