2014-04-01, 22:15
I'm trying to create an addon to log into my site, but I'm not succeeding, first time i want an alert with login successfully or error, thank you in advance for help!
I am following this tutorial
http://forum.xbmc.org/showthread.php?tid=99082 |
addon.xml
default.py
weblogin.py
gethtml.py
settings.xml
I am following this tutorial
http://forum.xbmc.org/showthread.php?tid=99082 |
addon.xml
Code:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<addon id="plugin.video"
name="VerTelevisao"
version="1.0"
provider-name="x">
<requires>
<import addon="xbmc.python" version="2.0"/>
<import addon="script.module.simplejson" />
</requires>
<extension point="xbmc.python.pluginsource"
library="default.py">
<provides>video</provides>
</extension>
<extension point="xbmc.addon.metadata">
<summary>Tv</summary>
<description>Tv.</description>
<platform>all</platform>
</extension>
</addon>
Code:
import xbmc
import xbmcaddon
def Notify(title,message,times,icon):
xbmc.executebuiltin("XBMC.Notification("+title+","+message+","+times+","+icon+")")
def LOGIN(username,password,hidesuccess):
uc = username[0].upper() + username[1:]
lc = username.lower()
logged_in = weblogin.doLogin(__datapath__,username,password)
if logged_in == True:
avatar = get_avatar(lc)
if hidesuccess == 'false':
Notify('Welcome back '+uc,'Fantasti.cc loves you','4000',avatar)
addDir(uc+"'s Videos",main_url+'user/'+lc+'/videos/save_date',1,avatar)
addDir(uc+"'s Collections",main_url+'user/'+lc+'/collections',2,avatar)
addDir(uc+"'s Favourited Collections",main_url+'user/'+lc+'/collections/favourited',2,avatar)
addDir(uc+"'s Rated Collections",main_url+'user/'+lc+'/collections/rated',2,avatar)
elif logged_in == False:
Notify('Login Failure',uc+' could not login','4000',default_image)
def STARTUP_ROUTINES():
#deal with bug that happens if the datapath doesn't exist
if not os.path.exists(__datapath__):
os.makedirs(__datapath__)
#check if user has enabled use-login setting
usrsettings = xbmcaddon.Addon(id=__addonname__)
use_account = usrsettings.getSetting('use-account')
if use_account == 'true':
#get username and password and do login with them
#also get whether to hid successful login notification
username = usrsettings.getSetting('username')
password = usrsettings.getSetting('password')
hidesuccess = usrsettings.getSetting('hide-successful-login-messages')
LOGIN(username,password,hidesuccess)
weblogin.py
Code:
# -*- coding: utf-8 -*-
import re
import os
import urllib
import urllib2
import cookielib
### TESTING SETTINGS (will only be used when running this file independent of your addon)
# Remember to clear these after you are finished testing,
# so that your sensitive details are not in your source code.
# These are only used in the: if __name__ == "__main__" thing at the bottom of this script.
myusername = ''
mypassword = ''
#note, the cookie will be saved to the same directory as weblogin.py when testing
def check_login(source,username):
#the string you will use to check if the login is successful.
#you may want to set it to: username (no quotes)
logged_in_string = 'Painel'
#search for the string in the html, without caring about upper or lower case
if re.search(logged_in_string,source,re.IGNORECASE):
return True
else:
return False
def doLogin(cookiepath, username, password):
#check if user has supplied only a folder path, or a full path
if not os.path.isfile(cookiepath):
#if the user supplied only a folder path, append on to the end of the path a filename.
cookiepath = os.path.join(cookiepath,'cookies.lwp')
#delete any old version of the cookie file
try:
os.remove(cookiepath)
except:
pass
if username and password:
#the url you will request to.
login_url = 'http://www./login/index.php'
#the header used to pretend you are a browser
header_string = 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-GB; rv:1.9.0.3) Gecko/2008092417 Firefox/3.0.3'
#build the form data necessary for the login
login_data = urllib.urlencode({'usr_email':username, 'pwd':password, 'memento':1, 'x':0, 'y':0, 'doLogin':'login'})
#build the request we will make
req = urllib2.Request(login_url, login_data)
req.add_header('User-Agent',header_string)
#initiate the cookielib class
cj = cookielib.LWPCookieJar()
#install cookielib into the url opener, so that cookies are handled
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))
#do the login and get the response
response = opener.open(req)
source = response.read()
response.close()
#check the received html for a string that will tell us if the user is logged in
#pass the username, which can be used to do this.
login = check_login(source,username)
#if login suceeded, save the cookiejar to disk
if login == True:
cj.save(cookiepath)
#return whether we are logged in or not
return login
else:
return False
#code to enable running the .py independent of addon for testing
if __name__ == "__main__":
if myusername is '' or mypassword is '':
print 'YOU HAVE NOT SET THE USERNAME OR PASSWORD!'
else:
logged_in = doLogin(os.getcwd(),myusername,mypassword)
print 'LOGGED IN:',logged_in
gethtml.py
Code:
'''
gethtml with cookies support v1
by anarchintosh @ xbmcforums
Copyleft = GNU GPL v3 (2011 onwards)
this function is paired with weblogin.py
and is intended to make it easier for coders wishing
to scrape source of pages, while logged in to that site.
USAGE:
!!!!!First set the compatible_urllist below!!!!!!!!!
import gethtml
to load html without cookies
source = gethtml.get(url)
to load html with cookies
source = gethtml.get(url,'my-path-to-cookiefile')
'''
import urllib,urllib2
import cookielib
import os
import re
#!!!!!!!!!!! Please set the compatible_urllist
#set the list of URLs you want to load with cookies.
#matches bits of url, so that if you want to match www243.megaupload.com/ you can just put '.megaupload.com/' in the list.
compatible_urllist = ['http://fantasti.cc/','http://77.247.181.97/']
def url_for_cookies(url):
#ascertain if the url contains any of the phrases in the list. return True if a match is found.
for compatible_url in compatible_urllist:
if re.search(compatible_url,url):
url_is_compatible = True
break
else: url_is_compatible = False
return url_is_compatible
def get(url,cookiepath=None):
#print 'processing url: '+url
# use cookies if cookiepath is set and if the cookiepath exists.
if cookiepath is not None:
#only use cookies for urls specified
if url_for_cookies(url) == True:
#check if user has supplied only a folder path, or a full path
if not os.path.isfile(cookiepath):
#if the user supplied only a folder path, append on to the end of the path a common filename.
cookiepath = os.path.join(cookiepath,'cookies.lwp')
#check that the cookie exists
if os.path.exists(cookiepath):
cj = cookielib.LWPCookieJar()
cj.load(cookiepath)
req = urllib2.Request(url)
req.add_header('User-Agent', 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-GB; rv:1.9.0.3) Gecko/2008092417 Firefox/3.0.3')
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))
response = opener.open(req)
link=response.read()
response.close()
return link
else: return _loadwithoutcookies(url)
else: return _loadwithoutcookies(url)
else: return _loadwithoutcookies(url)
def _loadwithoutcookies(url):
req = urllib2.Request(url)
req.add_header('User-Agent', 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-GB; rv:1.9.0.3) Gecko/2008092417 Firefox/3.0.3')
response = urllib2.urlopen(req)
link=response.read()
response.close()
return link
Code:
<settings>
<category label="General">
<setting id="use-account" type="bool" label="30200" default="false"/>
<setting id="username" type="text" label="login" default="" enable="!eq(-1,false)"/>
<setting id="password" type="text" label="senha" default="" option="hidden" enable="!eq(-2,false)"/>
<setting id="hide-successful-login-messages" type="bool" label="30203" default="false" enable="!eq(-3,false)"/>
</category>
</settings>