#!/usr/bin/env python

import requests
import ConfigParser
import argparse


LOGIN = ''
PASSWORD = ''

TARIFF_CODES = {'300':'POS-MA13-0002', '350':'POS-MA13-0003', '400':'POS-MA13-0004', '450':'POS-MA13-0005', '500':'POS-MA13-0006', '550':'POS-MA13-0007', '600':'POS-MA13-0008', '650':'POS-MA13-0009', '700':'POS-MA13-0010', '750':'POS-MA13-0011', '800':'POS-MA13-0012', '850':'POS-MA13-0013', '900':'POS-MA13-0014'}
AVAILABLE_SPEED = TARIFF_CODES.keys()
AVAILABLE_SPEED.sort()


def get_args():
    arg_parse = argparse.ArgumentParser(description='Script to change yota tariff or show one (It shows tariff price not speed)')
    arg_parse.add_argument('-t', '--tariff', dest='tariff', choices=AVAILABLE_SPEED, type=str, help='Tariff to set for yota in rubles')
    arg_parse.add_argument('-s', '--show', dest='show', action="store_true", help='Show the current tariff in rubles')
    args = arg_parse.parse_args()
    return args

def auth_yota(sess):
    auth_url = 'https://login.yota.ru/UI/Login'

    payload = {'IDToken1' : LOGIN, 'IDToken2' : PASSWORD, 
                'goto' : 'https://my.yota.ru/selfcare/loginSuccess', 
                'gotoOnFail' : 'https://my.yota.ru/selfcare/loginError',
                'old-token' : '',
                'org' : 'customer' }
    result = sess.post(auth_url, payload)
    return result

def change_offer(sess, product, speed):
    tariff_url = 'https://my.yota.ru/selfcare/devices/changeOffer'
    tariff_payload = {'product' : product, 'offerCode' : TARIFF_CODES[speed],
                      'homeOfferCode' : '', 'areOffersAvailable' : 'false',
                      'period' : '', 'status' : 'custom',
                      'autoprolong': 0, 'isSlot' : 'false', 'resourceId' : '',
                      'Device' : 1, 'username' : '', 'isDisablingAutoprolong' : 'false'}

    result = sess.post(tariff_url, tariff_payload)
    return result

def show_offer(offerCode):
    for key, value in TARIFF_CODES.iteritems():
        if value == offerCode:
            print 'Your tariff is %s' % key
            break
    else:
        print 'Error. Yota nas not such tariff'
    return


def main():
    args = get_args()

    sess = requests.Session()
    r = auth_yota(sess)
    output = r.text.split('\n')
    product = ''
    offerCode = ''
    for line in output:
        if 'name="product"' in line:
            product = line.split()[3].split('=')[1]
            product = int(product.replace('"', ''))
        if 'name="offerCode"' in line:
            offerCode = line.split()[3].split('=')[1]
            offerCode = offerCode.replace('"', '')

    if args.show:
        if offerCode == '':
            print 'Error. Offer code value is empty'

        show_offer(offerCode)
    else:
        if product == '':
            print 'Error. Product value is empty'

        change_offer(sess, product, args.tariff)



if __name__ == '__main__':
    main()

@@ -68,16 +68,39 @@
 rgparse%0A
+from lxml import etree%0A
 %0A%0ALOGIN 
@@ -2243,386 +2243,321 @@
 ss)%0A
+%0A
     
-output = r.text.split('%5Cn')%0A    product = ''%0A    offerCode = ''%0A    for line in output:%0A        if 'name=%22product%22' in line:%0A            product = line.split()%5B3%5D.split('=')%5B1%5D%0A            product = int(product.replace('%22', ''))%0A        if 'name=%22offerCode%22' in line:%0A            offerCode = line.split()%5B3%5D.split('=')%5B1%5D%0A            offerCode = offerCode.replace('%22', '')%0A%0A
+try:%0A        tree = etree.HTML(r.text)%0A        product = tree.xpath('//div%5Bcontains(@id, %22product_%22)%5D/form/input%5B1%5D')%5B0%5D.get('value')%0A        offerCode = tree.xpath('//div%5Bcontains(@id, %22product_%22)%5D/form/input%5B2%5D')%5B0%5D.get('value')%0A    except: #FIXME%0A        print 'Error. Something goes wrong'%0A    else:%0A    
     
@@ -2578,16 +2578,20 @@
         
+    
 if offer
@@ -2594,32 +2594,36 @@
 fferCode == '':%0A
+    
             prin
@@ -2668,16 +2668,20 @@
         
+    
 show_off
@@ -2694,30 +2694,38 @@
 erCode)%0A    
+    
 else:%0A
+    
         if p
@@ -2730,32 +2730,36 @@
  product == '':%0A
+    
             prin
@@ -2785,32 +2785,36 @@
 alue is empty'%0A%0A
+    
         change_o

Add xpath instead of dirty hack.