|
#!/usr/local/bin python3 |
|
|
|
### Python 3 Virgin Mobile Minute Texter |
|
### by Michael Holachek |
|
### Version 1.1 |
|
|
|
import sys |
|
import re |
|
import json |
|
import datetime |
|
import urllib.request as req |
|
import urllib.parse as parse |
|
|
|
# specify your SendHub and Virgin Mobile account details here |
|
|
|
VIRGINMOBILEACCOUNT = {'min':1235551234, 'vkey':0000} # Virgin Mobile phone # and PIN |
|
MESSAGETO = 13015551234 # number we want to send text message to |
|
|
|
SENDHUBNUMBER = 7035551234 # the number you used to sign up on SendHub (NOT the SendHub number) |
|
SENDHUBAPIKEY = "apikeyhere" # API Key from https://www.sendhub.com/settings |
|
|
|
|
|
def main(): |
|
# check first command line argument |
|
modeArg = str(sys.argv[1]) |
|
now = datetime.datetime.now() |
|
VirginMobileHTML = VirginMobileLogin() |
|
Data = Calculations(VirginMobileHTML) |
|
minutesRemaining = Data[0] |
|
daysLeft = Data[1] |
|
# no need to send texts everytime it checks |
|
# only send one if it is Sunday, you have less than an hour on your plan, |
|
# or you're running this manually |
|
if now.weekday() == 6 or minutesRemaining <= 60 or modeArg == "run": |
|
preMessage = LowMinuteCheck(minutesRemaining) |
|
ContactId = FindContactId() |
|
SendTextMessage(ContactId, preMessage, minutesRemaining, daysLeft) |
|
|
|
def VirginMobileLogin(): |
|
# connect to Virgin Mobile's site and login |
|
print("Logging in to Virgin Mobile.") |
|
URLdata = req.Request("https://www1.virginmobileusa.com/login/login.do", parse.urlencode(VIRGINMOBILEACCOUNT).encode('utf-8'), {"Content-Type": "application/x-www-form-urlencoded"}) |
|
page = req.urlopen(URLdata) |
|
print("Request finished.") |
|
return page.read() |
|
|
|
def Calculations(html): |
|
# let's calculate the remaining minutes |
|
minutesUsed = int(re.findall(b'<p id="remaining_minutes"><strong>([0-9]+)',html, re.I)[0]) |
|
minutesTotal = int(re.findall(b'<th>Anytime Minutes</th><td>([0-9]+)',html, re.I)[0]) |
|
minutesRemaining = minutesTotal - minutesUsed |
|
|
|
# and also the remaining days in the plan |
|
now = datetime.datetime.now() |
|
chargeDate = int(re.findall(b'<li id="charge_date"><h3>You will be charged on</h3><p>([0-9]+)',html, re.I)[0]) |
|
daysLeft = now.day - chargeDate |
|
|
|
print("You have {0} minutes remaining and {1} daysLeft on your plan.".format(minutesRemaining, daysLeft)) |
|
return minutesRemaining, daysLeft |
|
|
|
def LowMinuteCheck(minutesRemaining): |
|
# if you are low on minutes, add a special warning |
|
if minutesRemaining < 60: |
|
return "Uh oh!\n" |
|
else: |
|
return "" |
|
|
|
def FindContactId(): |
|
# first, search SendHub contact list with number to find contact id |
|
print("Attempting to find phone number {0} in your SendHub contacts list.".format(MESSAGETO)) |
|
contactList = req.urlopen("https://api.sendhub.com/v1/contacts/?username={0}&api_key={1}".format(SENDHUBNUMBER, SENDHUBAPIKEY)) |
|
contactListData = json.loads(contactList.read().decode('utf-8')) |
|
for contact in range(len(contactListData['objects'])): |
|
contactNumber = int(re.sub(r'\D+', '', contactListData['objects'][contact]['number'])) |
|
print(contactNumber) |
|
if contactNumber == int(MESSAGETO): |
|
print("Found it!") |
|
return contactListData['objects'][contact]['id'] |
|
# make a new SendHub contact if number cannot be found |
|
print("Sorry, I couldn't find that number in your SendHub contacts.\nI will attempt to make it.") |
|
POSTdata = {"name":"MinuteCheck Contact","number":MESSAGETO[1:]} |
|
URLdata = req.Request("https://api.sendhub.com/v1/contacts/?username={0}&api_key={1}".format(SENDHUBNUMBER, SENDHUBAPIKEY), json.dumps(POSTdata).encode('utf-8'), {"Content-Type": "application/json"}) |
|
page = req.urlopen(URLdata) |
|
response = json.loads(page.read()) |
|
page.close() |
|
print("Good to go! I created the new contact (Contact ID {0} successfully.".format(response['id'])) |
|
return response['id'] |
|
|
|
def SendTextMessage(ContactId, preMessage, minutesRemaining, daysLeft): |
|
# now compose a text message and send you the details via SMS |
|
sendToNumber = FindContactId() |
|
print("I will now send a text message to {0} (Contact ID {1})".format(MESSAGETO, ContactId)) |
|
POSTdata = {"contacts":[ContactId],"text":"{0}{1} minutes remaining.\n{2} days left this period.".format(preMessage,minutesRemaining,daysLeft)} |
|
request = req.Request("https://api.sendhub.com/v1/messages/?username={0}&api_key={1}".format(SENDHUBNUMBER, SENDHUBAPIKEY), json.dumps(POSTdata).encode('utf-8'), {"Content-Type": "application/json"}) |
|
page = req.urlopen(request) |
|
response = json.loads(page.read().decode('utf-8')) |
|
print("SMS Sent!") |
|
page.close() |
|
|
|
if __name__ == '__main__': |
|
main() |