Byron Moola
Byron Moola

Reputation: 33

phone number validating from list using RESTful api

I am trying to write a program to validate phone numbers by parsing 3 parameters. I want the number value to come from the number list i specified and repeat the request until the list ends.

from urllib import request, parse
import json

phone_number = open(input(f'\n{cy}Enter Phone Number List{res} : '),'r').read().splitlines()

print('------------------------------------------------------------------')


for i in phone_number :
    
    url = 'https://neutrinoapi.net/phone-validate'
    params = {
        'user-id': 'my user id goes here',
        'api-key': 'my api key goes here',
        'number' = 'phone_number' + str(i)
    }

    postdata = parse.urlencode(params).encode()
    req = request.Request(url, data=postdata)
    response = request.urlopen(req)
    result = json.loads(response.read().decode("utf-8"))

    if answer["valid==true"]:
        print(f'{gr}{answer["number"]}{res}{yl} => {cy}{answer["valid"]}{res}')
        save = open(f'Result/{answer["valid"]}.txt', 'a+')
        save.write(str(i) + '\n')
    else:
        print(f'{red}{answer["number"]} => Die{res}')
        dk = open('Result/invalid.txt', 'a+')
        dk.write(str(i) + '\n')


input('Click Enter For Exit ...!')

Upvotes: 0

Views: 232

Answers (2)

rzlvmp
rzlvmp

Reputation: 9394

Here is a correct example (based on documentation):

from urllib import request, parse
import json

# input file format: one line - one number
# 8012345678
# 801234

with open(input('Path to phone numbers file: '), 'r') as f:
  phone_nums = f.read().splitlines()

for phone_num in phone_nums:
  url = 'https://neutrinoapi.net/phone-validate'
  params = {
    'user-id': 'username',
    'api-key': 'secretsecretsecret',
    'country-code': 'JP',
    'number': phone_num
  }

  postdata = parse.urlencode(params).encode()
  req = request.Request(url, data=postdata)
  response = request.urlopen(req)
  result = json.loads(response.read().decode("utf-8"))

  print(f'------------ {phone_num} ---------------')
  for k, v in result.items():
    print(k, ':', v)

  print('-------------------------------')
  if not result["valid"]:
    print(f'{phone_num} is a NOT valid phone number')
  else:
    print(f'{phone_num} is a valid phone number')

print('-------------------------------')

Output:

------------ 8012345678 ---------------
valid : True
country : Japan
country-code : JP
prefix-network : NTT Docomo
international-number : +818012345678
location : Japan
local-number : 080-1234-5678
type : mobile
currency-code : JPY
international-calling-code : 81
is-mobile : True
country-code3 : JPN
-------------------------------
8012345678 is a valid phone number
------------ 801234 ---------------
valid : False
country : Japan
country-code : JP
prefix-network : 
international-number : +81801234
location : Japan
local-number : 801234
type : unknown
currency-code : JPY
international-calling-code : 81
is-mobile : False
country-code3 : JPN
-------------------------------
801234 is a NOT valid phone number
-------------------------------

Upvotes: 2

Byron Moola
Byron Moola

Reputation: 33

from urllib import request, parse
import json

phone_nums= open(input(f'\n{cy}Enter Phone Number List{res} : 
'),'r').read().splitlines()

for phone_num in phone_nums:
  url = 'https://neutrinoapi.net/phone-validate'
  params = {
'user-id': "user id here",
    'api-key': "secretsecretkey here",
    'number':'phone_num' +str(phone_num),
    'country-code' : 'US'
  }

  postdata = parse.urlencode(params).encode()
  req = request.Request(url, data=postdata)
  response = request.urlopen(req)
  result = json.loads(response.read().decode("utf-8"))

  print(f'------------{phone_num}---------------')
  for k, v in result.items():
  print(k, ':', v)

  print('-------------------------------')
  if not result["valid"]:
    print(f'{phone_num} is a NOT valid phone number')
  else:
     print(f'{phone_num} is a valid phone number')
     save = open(f'Result/valid.txt', 'a+')
     save.write(str(phone_num) + '\n')

print('-------------------------------')

This worked fine Thanks @rzlvmp for the input

Upvotes: 0

Related Questions