Skip to content

Instantly share code, notes, and snippets.

@nealtz
Last active August 29, 2015 13:57
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nealtz/9713098 to your computer and use it in GitHub Desktop.
Save nealtz/9713098 to your computer and use it in GitHub Desktop.
Redirect Telefonnumbers via TNG.de
# -*- coding: utf-8 -*-
# Check and change redirection status of a telefon number via the
# website of my telephone network service provider TNG.de
# Python Script for iOS Pythonista App (Vers. 1.4)
from bs4 import BeautifulSoup
import requests, console, re, sys, time, exceptions
try:
telNumber = '012342648463' # Your number that you want to redirect
# redirect target number 1
redirectTargetNameOne, redirectNumberOne = 'Number One: %s', '0123456789'
# redirect target number 2
redirectTargetNameTwo, redirectNumberTwo = 'Number Two: %s', '012345678910'
loginURL1 = 'https://www.tng.de/web/guest/home;jsessionid='
loginURL2 = '?p_p_id=TNG_MYTNG_LOGIN&p_p_lifecycle=1&p_p_state=normal&p_p_mode=view&p_p_col_id=column-3&p_p_col_count=2&saveLastPath=0&_TNG_MYTNG_LOGIN_struts_action=%2Fmytng_login%2Fview&_TNG_MYTNG_LOGIN_cmd=update'
# Fill in your details here to be posted to the login form.
payload = {
'_TNG_MYTNG_LOGIN_redirect': '/group/mytng/start',
'_TNG_MYTNG_LOGIN_rememberMe': 'false',
'_TNG_MYTNG_LOGIN_login': 'USERNAME', # Enter your Username
'_TNG_MYTNG_LOGIN_password': 'PASSWORD' # Enter your Password
}
# Use 'with' to ensure the session context is closed after use.
with requests.Session() as s:
# A first request to get jsessionid
console.show_activity()
r = s.get('https://www.tng.de/web/guest/home')
jsessionidValue = r.cookies['JSESSIONID']
loginURL = loginURL1 + jsessionidValue + loginURL2
# Login TNG Website
s.post(loginURL, data=payload)
console.hide_activity()
def tngstatus(telNumber, redirectTargetNameOne, redirectNumberOne, redirectTargetNameTwo, redirectNumberTwo):
"""Get status of redirection"""
console.show_activity()
r = s.get('https://www.tng.de/group/mytng/dienste/telefon-und-faxdienste')
#Extract Number and Data
soup = BeautifulSoup(r.text)
# Check if action is still in progress
actionActive = soup.find("td", text=telNumber).next_sibling.next_sibling.next_sibling.next_sibling.next
try:
if str(actionActive['alt']) == 'Exportvorgang aktiv':
time.sleep(3)
console.hide_activity()
tngstatus(telNumber, redirectTargetNameOne, redirectNumberOne, redirectTargetNameTwo, redirectNumberTwo)
except TypeError: # if there is no actionActivate string
pass
findNumber = soup.find("td", text=telNumber)
numberStatus = "'" + str(soup.find("td", text=telNumber).next.next.next.next.next).replace("\n","") + "'"
if redirectNumberOne in numberStatus:
actRedStat = 1
elif redirectNumberTwo in numberStatus:
actRedStat = 2
elif 'keine' in numberStatus:
actRedStat = 3
else:
actRedStat = 4
findButton = findNumber.find_next("a")
vOpsSearch = re.search("vOpts:(.+?)':'vOpts", str(findButton))
if vOpsSearch:
vOpsContent = vOpsSearch.group(1)
vOpsContent = 'vOpts:' + vOpsContent
findViewState = soup.find(id="javax.faces.ViewState")
viewState = str(findViewState['value'])
console.hide_activity()
# Show redirection status
massage = "Weiterleitung von %s ist aktuell %s" % (telNumber, numberStatus)
choice = console.alert('Status', massage, 'Ändern?', 'Fertig!')
if choice != 1:
s.get('https://www.tng.de/c/portal/logout')
sys.exit()
# Choose new redirection target
massage = massage + ' und soll nun umgeleitet werden auf:'
opt1 = redirectTargetNameOne % redirectNumberOne
opt2 = redirectTargetNameTwo % redirectNumberTwo
opt3 = 'Andere Nummer eingeben'
opt4 = 'Rufumleitung lösen'
if actRedStat == 1:
choice1, choice2, choice3 = opt2, opt3, opt4
elif actRedStat == 2:
choice1, choice2, choice3 = opt1, opt3, opt4
elif actRedStat == 3:
choice1, choice2, choice3 = opt1, opt2, opt3
elif actRedStat == 4:
choice1, choice2, choice3 = opt1, opt2, opt4
choice = console.alert('Ändern', massage, choice1, choice2, choice3)
if choice == 1 and actRedStat != 1:
forewardNumber, cfuActive = redirectNumberOne, 'true'
elif (choice == 2 and actRedStat > 2) or (choice == 1 and actRedStat == 1):
forewardNumber, cfuActive = redirectNumberTwo, 'true'
elif choice == 3 and actRedStat != 3:
forewardNumber, cfuActive = redirectNumberOne, 'false'
else:
forewardNumber = EnterRedirectNumber(massage)
cfuActive = 'true'
redirectnumber(forewardNumber, cfuActive, vOpsContent, viewState, redirectTargetNameOne, redirectNumberOne, redirectTargetNameTwo, redirectNumberTwo)
def EnterRedirectNumber(message):
NewRedNr = console.input_alert('Weiterleitung einrichten', message, 'OK')
if NewRedNr.isdigit() == False:
console.hud_alert('Keine gültige Telefonnummer eingegeben!', 'error', 3.0)
EnterRedirectNumber(message)
return NewRedNr
def redirectnumber(forewardNumber, cfuActive, vOpsContent, viewState, redirectTargetNameOne, redirectNumberOne, redirectTargetNameTwo, redirectNumberTwo):
"""Set Redirection Number"""
# Post Onclick Button
console.show_activity()
tngButtonPressURL = 'https://www.tng.de/group/mytng/dienste/telefon-und-faxdienste?p_p_id=TNG_VOICE&p_p_lifecycle=1&p_p_state=normal&p_p_mode=view&p_p_col_id=column-1&p_p_col_count=1&_TNG_VOICE_com.sun.faces.portlet.VIEW_ID=%2Fhtml%2Fportlet%2Fext%2Ftng_voice%2Findex.jsp&_TNG_VOICE_com.sun.faces.portlet.NAME_SPACE=_TNG_VOICE_'
payload = {
'vOpts': 'vOpts',
'vOpts:autorefresh': 'false',
'javax.faces.ViewState': viewState,
vOpsContent: vOpsContent
}
s.post(tngButtonPressURL, data=payload)
r = s.get(tngButtonPressURL)
# Extract Data for Redirect Form Entery
soup = BeautifulSoup(r.text)
findViewState = soup.find(id="javax.faces.ViewState")
viewState = str(findViewState['value'])
# Post Form to Redirect Number
tngChangeRedirectURL = 'https://www.tng.de/group/mytng/dienste/telefon-und-faxdienste?p_p_id=TNG_VOICE&p_p_lifecycle=1&p_p_state=normal&p_p_mode=view&p_p_col_id=column-1&p_p_col_count=1&_TNG_VOICE_com.sun.faces.portlet.VIEW_ID=%2Fhtml%2Fportlet%2Fext%2Ftng_voice%2Fcfc.jsp&_TNG_VOICE_com.sun.faces.portlet.NAME_SPACE=_TNG_VOICE_'
payload = {
'cfcform': 'cfcform',
'cfcform:cfuActive': cfuActive,
'cfcform:cfuNumber': forewardNumber,
'cfcform:cfnrActive': 'false',
'cfcform:cfnrNumber': '',
'cfcform:cfnrTimeout': '30',
'cfcform:cfbActive': 'false',
'cfcform:cfbNumber': '',
'cfcform:j_id_jsp_759935655_50': 'Änder.+speichern',
'javax.faces.ViewState': viewState
}
s.post(tngChangeRedirectURL, data=payload)
time.sleep(5)
console.hide_activity()
tngstatus(telNumber, redirectTargetNameOne, redirectNumberOne,redirectTargetNameTwo, redirectNumberTwo)
tngstatus(telNumber,redirectTargetNameOne, redirectNumberOne, redirectTargetNameTwo, redirectNumberTwo)
except Exception, e:
print e
console.hud_alert('Die TNG Website ist derzeit überlastet! Probiere es später nochmal!', 'error', 3.0)
sys.exit()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment