Advertisement
Guest User

Untitled

a guest
Feb 27th, 2011
394
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.74 KB | None | 0 0
  1. import re
  2. import urllib
  3. import traceback
  4.  
  5. class FileHostModule:
  6.     """
  7.     Generic FileHostModule for resolving and checking links for file hosts within XBMC addons
  8.     """
  9.     def getIconFileHost(self):
  10.         """
  11.         Returns a URL with an icon for the filehost.
  12.         """
  13.         return "default.png"
  14.    
  15.     def getIconURL(self, url):
  16.         """
  17.         Returns a URL with an icon for the specified URL
  18.         """
  19.         return self.getIconFileHost()
  20.    
  21.     def getRegularExpression(self):
  22.         """
  23.         Returns a regular expression that finds links of this file host within HTML source.
  24.         """
  25.         print "FileHostModule.py: Default regular expression returned from FileHostModule interface. Please override this method!"
  26.         return r"((https?|ftp|gopher|telnet|file|notes|ms-help):((//)|(\\\\))+[\w\d:#@%/;$()~_?\+-=\\\.&]*)" #matches all valid http/https/ftp urls
  27.    
  28.     def isValidLink(self, url):
  29.         """
  30.         Checks if the supplied link is valid for this file host
  31.         """
  32.         return (len(self.filterValidURLs([url]))>0)
  33.    
  34.     def filterValidURLs(self,urls):
  35.         """
  36.         Checks if the supplied links are valid for this file host
  37.         """
  38.         res=[]
  39.         for url in urls:
  40.             if re.findall(self.getRegularExpression(),url)!=None:
  41.                 res.append(url)
  42.         return res
  43.  
  44.     def filterOnlineURLs(self,urls):
  45.         """
  46.         Filters a list of URLs for the ones that are accessible with this module
  47.         """
  48.         validurls=self.filterValidURLs(urls)
  49.         res=[]
  50.         for url in validurls:
  51.             try:
  52.                 fh=urllib.urlopen(url)
  53.                 if fh.getcode()==200:
  54.                     res.append(url)
  55.                 else:
  56.                     pass
  57.             except:
  58.                 pass
  59.         return res
  60.  
  61.  
  62.     def isURLOnline(self,url):
  63.         """
  64.         Checks if a URL of this file host is online
  65.         """
  66.         return (len(self.filterOnlineURLs([url]))>0)
  67.  
  68.     def isLoginValid(self,username,password):
  69.         """
  70.         Checks if the specified login credentials are valid
  71.         """
  72.         return True
  73.  
  74.     def isLoginForStreaming(self,username,password):
  75.         """
  76.         Checks if the supplied link is valid for streaming files within XBMC (think about free or expired accounts)
  77.         """
  78.         return True
  79.     def login(self,username,password):
  80.         """
  81.         Logs into a file host service and stores cookie information for later use
  82.         """
  83.         return True
  84.  
  85.     def isLoggedIn(self):
  86.         """
  87.         Checks if the user is still logged in
  88.         """
  89.         return True
  90.    
  91.     def logout(self):
  92.         """
  93.         Checks if the supplied link is valid for this file host
  94.         """
  95.         return True
  96.  
  97.     def resolveURLs(self,urls):
  98.         """
  99.         Resolves a url for this module's host
  100.         """
  101.         return self.filterOnlineURLs(urls)
  102.  
  103.     def resolveURL(self,url):
  104.         """
  105.         Checks if the supplied link is valid for this file host
  106.         """
  107.         try:
  108.             return self.filterOnlineURLs([url])[0]
  109.         except:
  110.             return None
  111.    
  112.     def findURLs(self,pagesource):
  113.         """
  114.         Finds URLs of this host within HTML page source
  115.         """
  116.         res=[]
  117.         reres=re.findall(self.getRegularExpression(),pagesource)
  118.         for oneurl in reres:
  119.             try:
  120.                 res.append(oneurl[0])
  121.             except:
  122.                 pass
  123.         return res
  124.  
  125.     def findAndResolveURLs(self, pagesource):
  126.         """
  127.         Resolves all links found in the supplied HTML source to streamable URLs
  128.         """
  129.         r=self.findURLs(pagesource)
  130.         return self.resolveURLs(r)
  131.  
  132.     def getFileName(self,url):
  133.         """
  134.         Returns the filename of a URL for this host (some do not store this in the URL)
  135.         """
  136.         try:
  137.             return re.findall(r"/([\w_.-]*?(?=\?)|[\w_.-]*$/)",url)[0]
  138.         except:
  139.             return ""
  140.        
  141.     def getPlaylistItems(self,urls):
  142.         """
  143.         returns playlist items (resolved) for the specified urls. Adds all available meta info to it (icon, filename, etc.)
  144.         """
  145.         res=[]
  146.         for url in urls:
  147.             #create playlist item here
  148.             pass
  149.         return res
  150.    
  151.     def getPlaylistItem(self,url):
  152.         """
  153.         returns a playlist item (resolved) for the specified url. Adds all available meta info to it (icon, filename, etc.)
  154.         """
  155.         return self.getPlaylistItems([url])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement