Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
#!/usr/bin/python
#12/03/2014
#version: Eng Eder de Souza
#package: AGI Portabilidade
#copyright: Copyright (C) Open Source.
#license: GNU/GPL
#Exemplo de como chamar este script em seu Asterisk:
#exten => _4444.,1,AGI(operadora2014.py,${EXTEN:4})
#exten => _4444.,n,NoOP( Operadora = ${Operadora})
import urllib2
import urllib
import re
import sys
env = {}
while 1:
line = sys.stdin.readline().strip()
if line == '':
break
key,data = line.split(':')
if key[:4] <> 'agi_':
sys.stderr.write("Did not work!\n");
sys.stderr.flush()
continue
key = key.strip()
data = data.strip()
if key <> '':
env[key] = data
for key in env.keys():
sys.stderr.write(" -- %s = %s\n" % (key, env[key]))
sys.stderr.flush()
imput=sys.argv[1]
ddd = imput[:2]
rnumber = imput[2:len(imput)]
lnumber = rnumber[len(rnumber)-4:]
fnumber = rnumber[:len(rnumber)-4]
number="(" + ddd + ")" + " " + fnumber + "-" + lnumber
url='http://www.qualoperadora.net'
#coloquem aqui um edereço de proxy :-)
#proxy = urllib2.ProxyHandler({'http': '177.1.139.242:3128'})
proxy = urllib2.ProxyHandler({'http': '186.238.51.149:8080'})
opener = urllib2.build_opener(proxy)
urllib2.install_opener(opener)
query_args = { 'ref0952':'http://www.qualoperadora.net/', 'telefone':number }
data = urllib.urlencode(query_args)
headers = {'User-Agent' : 'Mozilla/5.0 (Windows NT 6.2; WOW64; rv:27.0) Gecko/20100101 Firefox/27.0'}
request = urllib2.Request(url, data, headers)
request.add_header('Referer', 'http://www.qualoperadora.net/')
response = urllib2.urlopen(request)
page = response.read()
response.close()
if "Operadora" in page:
page = page.replace('</title>','')
find = re.search('Operadora: (.*)', page)
operadora, tipo, cidade = find.group(1).split('-')
sys.stdout.write('SET VARIABLE Operadora "%s"\n'% str(operadora.strip()))
sys.stdout.flush()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.