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

from module.plugins.Account import Account
from module.common.json_layer import json_loads


class RapidgatorNet(Account):
    __name__    = "RapidgatorNet"
    __type__    = "account"
    __version__ = "0.08"

    __description__ = """Rapidgator.net account plugin"""
    __license__     = "GPLv3"
    __authors__     = [("zoidberg", "zoidberg@mujmail.cz")]


    API_URL = 'http://rapidgator.net/api/user'


    def loadAccountInfo(self, user, req):
        validuntil  = None
        trafficleft = None
        premium     = False
        sid         = None

        try:
            sid = self.getAccountData(user).get('sid')
            assert sid

            html = req.load("%s/info" % self.API_URL, get={'sid': sid})

            self.logDebug("API:USERINFO", html)

            json = json_loads(html)

            if json['response_status'] == 200:
                if "reset_in" in json['response']:
                    self.scheduleRefresh(user, json['response']['reset_in'])

                validuntil  = json['response']['expire_date'],
                trafficleft = float(json['response']['traffic_left']) / 1024,  #@TODO: Remove `/ 1024` in 0.4.10
                premium     = True
            else:
                self.logError(json['response_details'])

        except Exception, e:
            self.logError(e)

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


    def login(self, user, data, req):
        try:
            html = req.load('%s/login' % self.API_URL, post={"username": user, "password": data['password']})

            self.logDebug("API:LOGIN", html)

            json = json_loads(html)

            if json['response_status'] == 200:
                data['sid'] = str(json['response']['session_id'])
                return
            else:
                self.logError(json['response_details'])

        except Exception, e:
            self.logError(e)

        self.wrongPassword()

@@ -225,17 +225,17 @@
 _ = %220.0
-8
+9
 %22%0A%0A    _
@@ -393,17 +393,17 @@
 I_URL = 
-'
+%22
 http://r
@@ -424,17 +424,17 @@
 api/user
-'
+%22
 %0A%0A%0A    d
@@ -1068,17 +1068,16 @@
 e_date'%5D
-,
 %0A       
@@ -1145,17 +1145,16 @@
 ) / 1024
-,
   #@TODO

Fix https://github.com/pyload/pyload/issues/1118