# -*- coding: utf-8 -*-

import re

from module.plugins.internal.Account import Account


class ShareonlineBiz(Account):
    __name__    = "ShareonlineBiz"
    __type__    = "account"
    __version__ = "0.35"
    __status__  = "testing"

    __description__ = """Share-online.biz account plugin"""
    __license__     = "GPLv3"
    __authors__     = [("Walter Purcaro", "vuolter@gmail.com")]


    def api_response(self, user, password, req):
        return self.load("http://api.share-online.biz/cgi-bin",
                         get={'q'       : "userdetails",
                              'aux'     : "traffic",
                              'username': user,
                              'password': password})


    def parse_info(self, user, password, data, req):
        premium     = False
        validuntil  = None
        trafficleft = -1
        maxtraffic  = 100 * 1024 * 1024 * 1024  #: 100 GB

        api = {}
        for line in self.api_response(user, password, req).splitlines():
            if "=" in line:
                key, value = line.split("=")
                api[key] = value

        self.log_debug(api)

        if api['a'].lower() != "not_available":
            req.cj.setCookie("share-online.biz", 'a', api['a'])

            premium = api['group'] in ("PrePaid", "Premium", "Penalty-Premium")

            validuntil = float(api['expire_date'])

            traffic     = float(api['traffic_1d'].split(";")[0])

            if maxtraffic > traffic:
                trafficleft = maxtraffic - traffic
            else:
                trafficleft = -1

        maxtraffic  /= 1024  #@TODO: Remove `/ 1024` in 0.4.10
        trafficleft /= 1024  #@TODO: Remove `/ 1024` in 0.4.10

        return {'premium'    : premium,
                'validuntil' : validuntil,
                'trafficleft': trafficleft,
                'maxtraffic' : maxtraffic}


    def login(self, user, password, data, req):
        html = self.api_response(user, password, req)
        err  = re.search(r'\*\*(.+?)\*\*', html)
        if err:
            self.log_error(err.group(1).strip())
            self.login_fail()

@@ -203,9 +203,9 @@
 %220.3
-5
+6
 %22%0A  
@@ -446,20 +446,19 @@
       re
-turn
+s =
  self.lo
@@ -499,17 +499,16 @@
 i-bin%22,%0A
- 
         
@@ -584,17 +584,16 @@
         
- 
 'aux'   
@@ -607,17 +607,16 @@
 affic%22,%0A
- 
         
@@ -683,17 +683,16 @@
         
- 
 'passwor
@@ -711,199 +711,35 @@
 %7D)%0A%0A
-%0A
     
-def parse_info(self, user, password, data, req):%0A        premium     = False%0A        validuntil  = None%0A        trafficleft = -1%0A        maxtraffic  = 100 * 1024 * 1024 * 1024  #: 100 GB
+    self.log_debug(res)
 %0A%0A  
@@ -777,46 +777,11 @@
  in 
-self.api_response(user, password, req)
+res
 .spl
@@ -910,26 +910,83 @@
     
-self.log_debug(api
+if not api%5B'a'%5D:%0A            self.login_fail(_(%22Invalid username/password%22)
 )%0A%0A 
@@ -1016,9 +1016,9 @@
 r() 
-!
+=
 = %22n
@@ -1048,65 +1048,316 @@
     
-req.cj.setCookie(%22share-online.biz%22, 'a', api%5B'a'%5D)%0A%0A    
+self.login_fail(_(%22No info available%22))%0A%0A        return api%0A%0A%0A    def parse_info(self, user, password, data, req):%0A        premium     = False%0A        validuntil  = None%0A        trafficleft = -1%0A        maxtraffic  = 100 * 1024 * 1024 * 1024  #: 100 GB%0A%0A        api = self.api_response(user, password, req)%0A%0A
     
@@ -1368,16 +1368,19 @@
 premium 
+   
 = api%5B'g
@@ -1431,21 +1431,16 @@
 emium%22)%0A
-%0A    
         
@@ -1478,26 +1478,24 @@
 date'%5D)%0A
-%0A 
         
    traff
@@ -1482,27 +1482,24 @@
 '%5D)%0A        
-   
 traffic     
@@ -1497,17 +1497,16 @@
 ffic    
- 
 = float(
@@ -1547,20 +1547,16 @@
         
-    
 if maxtr
@@ -1576,36 +1576,32 @@
 ic:%0A            
-  
-  
 trafficleft = ma
@@ -1631,22 +1631,14 @@
     
-    
 else:%0A
-    
     
@@ -2018,20 +2018,19 @@
         
-html
+api
  = self.
@@ -2075,17 +2075,16 @@
     err 
- 
 = re.sea
@@ -2109,13 +2109,13 @@
 *', 
-html)
+api)%0A
 %0A   
@@ -2122,16 +2122,20 @@
      if 
+not 
 err:%0A   
@@ -2147,44 +2147,73 @@
     
-self.log_error(err.group(1).strip())
+req.cj.setCookie(%22share-online.biz%22, 'a', api%5B'a'%5D)%0A        else:
 %0A   
@@ -2237,10 +2237,22 @@
 in_fail(
+err.group(1)
 )%0A

Improve account