python texting code - unknown bug <SOLVED>

Forum rules
Before you post please read how to get help

python texting code - unknown bug <SOLVED>

Postby leef on Thu Feb 20, 2014 4:04 pm

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 leef on Fri Feb 21, 2014 12:47 am, edited 1 time in total.
leef
Level 1
Level 1
 
Posts: 7
Joined: Sun Dec 30, 2012 6:40 pm

Linux Mint is funded by ads and donations.
 

Re: python texting code - unknown bug

Postby passerby on Thu Feb 20, 2014 6:03 pm

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.
Home PC: Xubuntu 14.04 x64, Nvidia GTX650, i7 3770K, 16GB RAM, Asus Xonar DX
Work PC: Mint 13 x64 XFCE, Nvidia GT620, i5 3470, 8GB RAM
Laptop: System76 Kudu Pro, Xubuntu 14.04 x64

My HTML5 login themes + screenshots
User avatar
passerby
Level 6
Level 6
 
Posts: 1375
Joined: Sun Dec 16, 2012 7:29 pm

Re: python texting code - unknown bug

Postby leef on Thu Feb 20, 2014 6:19 pm

Thanks, great suggestions
leef
Level 1
Level 1
 
Posts: 7
Joined: Sun Dec 30, 2012 6:40 pm

Re: python texting code - unknown bug

Postby WharfRat on Thu Feb 20, 2014 6:19 pm

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
Image
User avatar
WharfRat
Level 12
Level 12
 
Posts: 4463
Joined: Thu Apr 07, 2011 8:15 pm

Re: python texting code - unknown bug

Postby leef on Thu Feb 20, 2014 8:19 pm

how do i increase the number of digits from 6 to 10?
leef
Level 1
Level 1
 
Posts: 7
Joined: Sun Dec 30, 2012 6:40 pm

Re: python texting code - unknown bug

Postby passerby on Thu Feb 20, 2014 10:23 pm

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?
Home PC: Xubuntu 14.04 x64, Nvidia GTX650, i7 3770K, 16GB RAM, Asus Xonar DX
Work PC: Mint 13 x64 XFCE, Nvidia GT620, i5 3470, 8GB RAM
Laptop: System76 Kudu Pro, Xubuntu 14.04 x64

My HTML5 login themes + screenshots
User avatar
passerby
Level 6
Level 6
 
Posts: 1375
Joined: Sun Dec 16, 2012 7:29 pm

Re: python texting code - unknown bug

Postby leef on Thu Feb 20, 2014 10:55 pm

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.
leef
Level 1
Level 1
 
Posts: 7
Joined: Sun Dec 30, 2012 6:40 pm

Re: python texting code - unknown bug

Postby passerby on Thu Feb 20, 2014 11:58 pm

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.
Home PC: Xubuntu 14.04 x64, Nvidia GTX650, i7 3770K, 16GB RAM, Asus Xonar DX
Work PC: Mint 13 x64 XFCE, Nvidia GT620, i5 3470, 8GB RAM
Laptop: System76 Kudu Pro, Xubuntu 14.04 x64

My HTML5 login themes + screenshots
User avatar
passerby
Level 6
Level 6
 
Posts: 1375
Joined: Sun Dec 16, 2012 7:29 pm

Re: python texting code - unknown bug

Postby leef on Fri Feb 21, 2014 12:40 am

perfection: thanks for the tips and the digit modification solution.
leef
Level 1
Level 1
 
Posts: 7
Joined: Sun Dec 30, 2012 6:40 pm

Linux Mint is funded by ads and donations.
 

Return to Scripts & Bash

Who is online

Users browsing this forum: No registered users and 2 guests