Advertisement
Guest User

Untitled

a guest
Sep 23rd, 2016
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.53 KB | None | 0 0
  1. # -*- coding: UTF-8 -*-
  2.  
  3. """
  4. weblogin
  5. by Anarchintosh @ xbmcforums
  6. Copyleft (GNU GPL v3) 2011 onwards
  7.  
  8. this example is configured for Fantasti.cc login
  9. See for the full guide please visit:
  10. http://forum.xbmc.org/showthread.php?p=772597#post772597
  11.  
  12.  
  13. USAGE:
  14. in your default.py put:
  15.  
  16. import weblogin
  17. logged_in = weblogin.doLogin('a-path-to-save-the-cookie-to','the-username','the-password')
  18.  
  19. logged_in will then be either True or False depending on whether the login was successful.
  20. """
  21.  
  22. import os
  23. import re
  24. import urllib,urllib2
  25. import cookielib
  26.  
  27. ### TESTING SETTINGS (will only be used when running this file independent of your addon)
  28. # Remember to clear these after you are finished testing,
  29. # so that your sensitive details are not in your source code.
  30. # These are only used in the:  if __name__ == "__main__"   thing at the bottom of this script.
  31. myusername = 'delaroystudios'
  32. mypassword = 'RepubliC7891'
  33. #note, the cookie will be saved to the same directory as weblogin.py when testing
  34.  
  35.  
  36. def check_login(source,username):
  37.    
  38.     #the string you will use to check if the login is successful.
  39.     #you may want to set it to:    username     (no quotes)
  40.     logged_in_string = 'Hello'
  41.  
  42.     #search for the string in the html, without caring about upper or lower case
  43.     if re.search(logged_in_string,source,re.IGNORECASE):
  44.         return True
  45.     else:
  46.         return False
  47.  
  48.  
  49. def doLogin(cookiepath, username, password):
  50.  
  51.     #check if user has supplied only a folder path, or a full path
  52.     if not os.path.isfile(cookiepath):
  53.         #if the user supplied only a folder path, append on to the end of the path a filename.
  54.         cookiepath = os.path.join(cookiepath,'cookies.lwp')
  55.        
  56.     #delete any old version of the cookie file
  57.     try:
  58.         os.remove(cookiepath)
  59.     except:
  60.         pass
  61.  
  62.     if username and password:
  63.  
  64.         #the url you will request to.
  65.         login_url = 'http://www.delaroystudios.com/geniustv/login.php'
  66.  
  67.         #the header used to pretend you are a browser
  68.         header_string = 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-GB; rv:1.9.0.3) Gecko/2008092417 Firefox/3.0.3'
  69.  
  70.     #build the form data necessary for the login
  71.         login_data = urllib.urlencode({'username':username, 'password':password, 'login':'Login'})
  72.  
  73.         #build the request we will make
  74.         req = urllib2.Request(login_url, login_data)
  75.         req.add_header('User-Agent',header_string)
  76.  
  77.         #initiate the cookielib class
  78.         cj = cookielib.LWPCookieJar()
  79.  
  80.         #install cookielib into the url opener, so that cookies are handled
  81.         opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))
  82.  
  83.         #do the login and get the response
  84.         response = opener.open(req)
  85.         source = response.read()
  86.         response.close()
  87.  
  88.         #check the received html for a string that will tell us if the user is logged in
  89.         #pass the username, which can be used to do this.
  90.         login = check_login(source,username)
  91.  
  92.         #if login suceeded, save the cookiejar to disk
  93.         if login == True:
  94.             cj.save(cookiepath)
  95.  
  96.         #return whether we are logged in or not
  97.         return login
  98.    
  99.     else:
  100.         return False
  101.  
  102. #code to enable running the .py independent of addon for testing
  103. if __name__ == "__main__":
  104.     if myusername is '' or mypassword is '':
  105.         print 'YOU HAVE NOT SET THE USERNAME OR PASSWORD!'
  106.     else:
  107.         logged_in = doLogin(os.getcwd(),myusername,mypassword)
  108.         print 'LOGGED IN:',logged_in
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement