From 3be8d531ce7c6500170bde8b8dbe49e7dc59ad39 Mon Sep 17 00:00:00 2001
From: Florian Franzen <FlorianFranzen@gmail.com>
Date: Mon, 9 Apr 2012 18:30:54 +0200
Subject: Added premiumize.me support, using their new API
 (https://secure.premiumize.me/?show=api). This fixes #416.

---
 module/plugins/accounts/PremiumizeMe.py | 40 ++++++++++++++++++++
 module/plugins/hooks/PremiumizeMe.py    | 65 +++++++++++++++++++++++++++++++++
 module/plugins/hoster/PremiumizeMe.py   | 41 +++++++++++++++++++++
 3 files changed, 146 insertions(+)
 create mode 100644 module/plugins/accounts/PremiumizeMe.py
 create mode 100644 module/plugins/hooks/PremiumizeMe.py
 create mode 100644 module/plugins/hoster/PremiumizeMe.py

(limited to 'module')

diff --git a/module/plugins/accounts/PremiumizeMe.py b/module/plugins/accounts/PremiumizeMe.py
new file mode 100644
index 000000000..768fcd783
--- /dev/null
+++ b/module/plugins/accounts/PremiumizeMe.py
@@ -0,0 +1,40 @@
+from module.plugins.Account   import Account
+
+from module.common.json_layer import json_loads
+
+class PremiumizeMe(Account):
+    __name__ = "PremiumizeMe"
+    __version__ = "0.1"
+    __type__ = "account"
+    __description__ = """Premiumize.Me account plugin"""
+    
+    __author_name__ = ("Florian Franzen")
+    __author_mail__ = ("FlorianFranzen@gmail.com")
+
+    def loadAccountInfo(self, user, req):
+        
+        # Get user data from premiumize.me
+        status = self.getAccountStatus(user, req)
+            
+        # Parse account info
+        account_info = {"validuntil": float(status['result']['expires']),
+                        "trafficleft": status['result']['trafficleft_bytes'] / 1024}
+
+        return account_info
+
+    def login(self, user, data, req):
+        
+        # Get user data from premiumize.me
+        status = self.getAccountStatus(user, req)
+        
+        # Check if user and password are valid
+        if status['status'] != 200:
+            self.wrongPassword()
+
+    
+    def getAccountStatus(self, user, req):
+        
+        # Use premiumize.me API v1 (see https://secure.premiumize.me/?show=api) to retrieve account info and return the parsed json answer
+        answer = req.load("https://api.premiumize.me/pm-api/v1.php?method=accountstatus&params[login]=%s&params[pass]=%s" % (user, self.accounts[user]['password']))            
+        return json_loads(answer)
+        
\ No newline at end of file
diff --git a/module/plugins/hooks/PremiumizeMe.py b/module/plugins/hooks/PremiumizeMe.py
new file mode 100644
index 000000000..3825e9219
--- /dev/null
+++ b/module/plugins/hooks/PremiumizeMe.py
@@ -0,0 +1,65 @@
+from module.plugins.internal.MultiHoster import MultiHoster
+
+from module.common.json_layer      import json_loads
+from module.network.RequestFactory import getURL
+
+class PremiumizeMe(MultiHoster):
+    __name__ = "PremiumizeMe"
+    __version__ = "0.1"
+    __type__ = "hook"
+    __description__ = """Premiumize.Me hook plugin"""
+
+    __config__ = [("activated", "bool", "Activated", "False"),
+                  ("hosterListMode", "all;listed;unlisted", "Use for hosters (if supported):", "all"), 
+                  ("hosterList", "str", "Hoster list (comma separated)", "")]
+
+    __author_name__ = ("Florian Franzen")
+    __author_mail__ = ("FlorianFranzen@gmail.com")
+
+    replacements = [("freakshare.net", "freakshare.com")]
+
+    interval = 0 # Disable periodic calls, we dont use them anyway
+    
+    def getHoster(self):     
+        # If no accounts are available there will be no hosters available
+        if not self.account or not self.account.canUse():
+            return []
+        
+        # Get account data
+        (user, data) = self.account.selectAccount()
+        
+        # Get supported hosters list from premiumize.me using the json API v1 (see https://secure.premiumize.me/?show=api)
+        answer = getURL("https://api.premiumize.me/pm-api/v1.php?method=hosterlist&params[login]=%s&params[pass]=%s" % (user, data['password']))
+        data = json_loads(answer)
+        
+        
+        # If account is not valid thera are no hosters available
+        if data['status'] != 200:
+            return []
+        
+        # Extract hosters from json file 
+        hosters = set(data['result']['hosterlist'])
+    
+                
+        # Read config to check if certain hosters should not be handled
+        configMode = self.getConfig('hosterListMode')
+        if configMode in ("listed", "unlisted"):
+            configList = set(self.getConfig('hosterList').strip().lower().replace('|',',').replace(';',',').split(','))
+            configList.discard(u'')
+            if configMode == "listed":
+                hosters &= configList
+            else:
+                hosters -= configList
+        
+        return list(hosters)      
+            
+    def coreReady(self):
+        # Get account plugin and check if there is a valid account available
+        self.account = self.core.accountManager.getAccountPlugin("PremiumizeMe")     
+        if not self.account.canUse():
+            self.account = None
+            self.logError(_("Please add a valid premiumize.me account first and restart pyLoad."))
+            return
+                  
+        # Run the overwriten core ready which actually enables the multihoster hook 
+        return MultiHoster.coreReady(self)
\ No newline at end of file
diff --git a/module/plugins/hoster/PremiumizeMe.py b/module/plugins/hoster/PremiumizeMe.py
new file mode 100644
index 000000000..2b1306820
--- /dev/null
+++ b/module/plugins/hoster/PremiumizeMe.py
@@ -0,0 +1,41 @@
+from module.plugins.Hoster    import Hoster
+
+from module.common.json_layer import json_loads
+
+class PremiumizeMe(Hoster):
+    __name__ = "PremiumizeMe"
+    __version__ = "0.1"
+    __type__ = "hoster"        
+    __description__ = """Premiumize.Me hoster plugin"""
+        
+    # Since we want to allow the user to specify the list of hoster to use we let MultiHoster.coreReady create the regex patterns for us using getHosters in our PremiumizeMe hook.
+    __pattern__ = ""
+    
+    __author_name__ = ("Florian Franzen")
+    __author_mail__ = ("FlorianFranzen@gmail.com")
+
+    def process(self, pyfile):
+        # Check account
+        if not self.account or not self.account.canUse():
+            self.logError(_("Please enter a valid premiumize.me account or deactivate this plugin"))
+            self.fail("No valid premiumize.me account provided")
+        
+        # Get account data
+        (user, data) = self.account.selectAccount()
+                       
+        # Get rewritten link using the premiumize.me api v1 (see https://secure.premiumize.me/?show=api)
+        answer = self.load("https://api.premiumize.me/pm-api/v1.php?method=directdownloadlink&params[login]=%s&params[pass]=%s&params[link]=%s" % (user, data['password'], pyfile.url))
+        data = json_loads(answer)                
+
+        # Check status and decide what to do
+        status = data['status']
+        if status == 200:
+            self.download(data['result']['location'])
+        elif status == 400:
+            self.fail("Invalid link")
+        elif status == 404: 
+            self.offline()
+        elif status >= 500:
+            self.tempOffline()
+        else:
+            self.fail(data['statusmessage'])
\ No newline at end of file
-- 
cgit v1.2.3