python texting code - unknown bug <SOLVED>

About writing shell scripts and making the most of your shell
Forum rules
Topics in this forum are automatically closed 6 months after creation.
Locked
leef

python texting code - unknown bug <SOLVED>

Post by leef »

i can't figure out what's wrong with this code. no error msgs, but it does not function.

Code: Select all

import urllib
import urllib2
number = raw_input("Please Enter The Phone Number:\n")
message = raw_input("Please Enter Your Message:\n")
prov = ''
url2 = 'http://www.txt2day.com/lookup.php'
url = 'http://www.onlinetextmessage.com/send.php'
values2 = {'action' : 'lookup',
           'pre' : number[0:3],
           'ex' : number[3:6],
           'myButton' : 'Find Provider'}
data2 = urllib.urlencode(values2)  ##provider checker
req2 = urllib2.Request(url2, data2)
response2 = urllib2.urlopen(req2)
the_page2 = response2.read()
if 'Telus' in the_page2:
    prov = '192'
if 'Bell' in the_page2:
    prov = '48'
if 'Rogers' in the_page2:
    prov = '162'
if 'Sprint' in the_page2:
    prov = '175'
if 'T-Mobile' in the_page2:
    prov = '182'
if 'Verizon' in the_page2:
    prov = '203'
if 'Virgin Mobile' in the_page2:
    prov = '205'
if 'AT&T' in the_page2:
    prov = '41'
if 'Boost' in the_page2:
    prov = '54'
print prov
if prov == 0:
    print "Failed To Identify Provider\n"
    exit
values = {'code' : '',
          'number' : number,
          'from' : '',
          'remember' : 'n',
          'subject' : '',
          'carrier' : prov,
          'quicktext' : '',
          'message' : message,
          's' : 'Send Message'}
data = urllib.urlencode(values)  ##text sender
req = urllib2.Request(url, data)
response = urllib2.urlopen(req)
the_page = response.read() 
Last edited by LockBot on Wed Dec 28, 2022 7:16 am, edited 2 times in total.
Reason: Topic automatically closed 6 months after creation. New replies are no longer allowed.
passerby

Re: python texting code - unknown bug

Post by passerby »

The script functions for me, as long as the phone number entered is 6 digits. You might want to include that in the prompt and add validation.
Also, you initialize prov as ' ', but the final check you perform is against 0, so even an unknown or invalid number won't print "Failed To Identify Provider", hence the lack of any output on an invalid number.

General insights to help you improve the script:
-There's no validation on the number or message. Consider putting the prompts in loops with validation before moving onto the next line
-Either initialize prov to 0, or change prov == 0 to prov == ' ', otherwise it's dead code
-Your current if statements check the entirety of the_page2 every time. Instead, consider searching the_page2 once and assigning the value of the line "Most Likely Provider is unknown" to another variable, then searching that variable instead
-Only one provider should be applicable to a single number, so consider changing the individual if statements into an if, elif, elif, ..., else statement. This would also allow you to use the else block to check for an unidentified number instead of checking the value of prov.
Last edited by passerby on Thu Feb 20, 2014 6:44 pm, edited 1 time in total.
leef

Re: python texting code - unknown bug

Post by leef »

Thanks, great suggestions
WharfRat

Re: python texting code - unknown bug

Post by WharfRat »

leef,

No go for me :(

The site http://www.txt2day.com/lookup.php does not return my correct service provider. You might want to try a different site.

Change if prov == 0: to if prov == '': if you want it to exit on failure.

UPDATE: Also change AT&T to Att
leef

Re: python texting code - unknown bug

Post by leef »

how do i increase the number of digits from 6 to 10?
passerby

Re: python texting code - unknown bug

Post by passerby »

leef wrote:how do i increase the number of digits from 6 to 10?
You don't. http://www.txt2day.com/lookup.php only accepts the first 6 digits of a 10 digit number.
Unless you want to get a 10 digit number from the user and only keep/use 6, in which case you'd do something like: number = number[:6]
Maybe I'm misunderstanding your question?
leef

Re: python texting code - unknown bug

Post by leef »

I'm new to python so I'm probably misunderstanding. I'd be thankful if you would eradicate my fallacious reasoning.
I thought that the code was prescribing 2 independent instructions:
1. txt2day.com/lookup.php to find the provider given x data
2. onlinemtextmessage.om/send.ph to send the data given the provided from text2day
I don't understand why 'number' would be truncated do text2day's 6 digit limit since 'number' is defined independently.

I know this isn't an instructional sub-forum, I'm missing something that's essential.
passerby

Re: python texting code - unknown bug

Post by passerby »

Alright, gotcha. I was indeed misunderstanding your question.
In that case, leave the validation at 10 numbers and use 0:3 and 3:6 as you were originally.
The problem I was getting before with more than 6 numbers was just to do with the if... section and prov.
leef

Re: python texting code - unknown bug

Post by leef »

perfection: thanks for the tips and the digit modification solution.
Locked

Return to “Scripts & Bash”