Kodi Community Forum

Full Version: [GUIDE] How to add a login function to your addon
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
[GUIDE] HOW TO ADD A LOGIN FUNCTION TO YOUR ADDON

Image

Hope this helps some people!

I've recently made a neat bit of code that works for logging in to most sites, and a fairly reliable method to adapt it to different sites. I've therefore written up a little guide to facilitate more people providing login functions in their addons.

I stress that it should work for most sites, but it won't work for all sites.

Its not very complicated, but i've gone into a lot of detail.
So don't be put off by the length of this post.

First of all, here are the login functions. (You will need to modify this)
Download weblogin.py
and here is the function that you should use in your addon to load html with,
so that you can get all that nice user-related data
you will need to slightly adjust this -- see step 20
Download gethtml.py

How does this login code work?
This login code works by filling out the username and password forms on a site, sending the request and saving the cookies sent by the response into a file if the login was successful. To check if the login was successful it scrapes the html of the response for some text that only appears in the html when you are logged in. You then use the cookie file when loading any urls from that website (see the code snippet at the end of the post)

What you need to do to configure the login code for your target site
You will need:
Google Chrome or a web traffic sniffer installed.
A working login for your site.




1. First of all, put your username and password in the TESTING SETTINGS field of weblogin.py

FINDING THE STRING TO CHECK WHETHER YOU ARE LOGGED IN

2. Next, open the login page of your website in Google Chrome, and open the page source in a tab.

3. Now log in. Again, open the page source in another new tab.

4. Now compare the two page sources and try and find a difference between the two that could be used to check whether you are logged in, using the Ctrl + F search function. Looking at the actual web page (not page source) can often give you clues ..... for some sites, you can find 'Welcome' or 'Hello' in the page source. Alternatively you could just check to see if your username can be found in the logged in page source.

5. You've got your special text, so now put that into the weblogin.py :
Go to the check_login function and set logged_in_string either to username (no quotes) or 'your-special-bit-of-text'

GETTING THE LOGIN URL AND FORM DATA
6. In Chrome, close the tabs you previously opened, log back out of your website and go back to to the login page.

7. Right click anywhere on the page, and select Inspect Element. A big info bar will pop up. Select the Network tab at the top of the bar. Then make sure that the All entry is selected at the bottom. Then click on the Timeline column, and in the dropdown menu select Start Time. To make sure that the field is clear before you begin, click the little Clear button to the left of All.

It should look like this:
Image

8. You are now ready to capture your values. Enter your details into the Username and Password fields on the webpage, and log in to your site. You should see loads of requests being captured in the field.

9. Once everything has finished loading scroll back up the field, and look for the first things that happened when you clicked the log in button. You should hopefully see the file that was sent to log you in. It might be named something like login or signin.php etc.

10. If you are not sure which file sent your Username and password, look through the Headers of the top/ first sent files. Scroll down within the Headers entry and try and find your username and password under the Form Data section.

11. Assuming you have now found the Headers of the file which sent your log in to the site, you can now get your values to use in weblogin.py . At the top of the header, you should see a value named "Request URL:". This is your login url. Save this for later.

12. Further down the Header you should see a section named Form Data. Copy this section and paste it into an empty text file for later.

For example:
Image

13. Now go back to your weblogin.py , and scroll down to the doLogin function. You'll want to paste your login url to the login_url string, and put your form data in the login_data section between the {curly brackets} (but first delete my Fantasti.cc form data). Separate field and value with a ':' and each field with a ','. Hardcode all the fields to the values you found in Form Data, except the username and password fields - they should be filled with the words: username and password repectively (with no quotes).

TESTING
14. Make sure that you have put your username and password into the TESTING SETTINGS, and now test if it all works by running the file. (press F5). It should print True if the login succeeds. If this works, now try deliberately misspelling your username or password and run it again. It should now print False.

15. Has something gone wrong? Check through the form values (login_data), login_url, and the login checking string.


TO USE THIS IN YOUR ADDON
16. Delete your data from the TESTING SETTINGS! You don't want to be sharing your username and password with the whole world via your source code.

17. Look in the USAGE section of weblogin.py for instructions, its very easy to call this from your default.py

18. You'll want to call weblogin.py on your addon's startup, so call it along with the first directories you add.

19. You will also need to put a Use Login option, and a Username and Password field in your settings.xml along with the language strings. Here is an example of such a settings.xml
Code:
<settings>
    <setting id="use-account" type="bool" label="30200" default="false"/>
    <setting id="username" type="text" label="30201" default="" enable="!eq(-1,false)"/>
    <setting id="password" type="text" label="30202" default="" option="hidden" enable="!eq(-2,false)"/>
    <setting id="hide-successful-login-messages" type="bool" label="30203" default="false" enable="!eq(-3,false)"/>
</settings>

Then see this very rough example for how to handle those settings in the default.py
You'll want to call STARTUP_ROUTINES() when you start the addon. This example demonstrates how you can use notifications to display whether the login worked, and adding certain directories if login is succcessful. If you are clever, you could even create a get avatar function to display your user's avatar in the successful login notification.

PHP Code:
import xbmc,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

20. You will also need to use the cookiefile when loading any web pages you intend to scrape (if you want to be logged in). Its simple to do. If you want, you can use the supplied gethtml.py (link at top of post).
you will need to open it and edit the list of urls it will load with cookies. it also contains usage instructions.








.
I would put a login system that works this addon http://zip.net/bclw7h login page simply login the rest leaves me, can you help me?
please Sad
Hi anarchintosh,

Please check your inbox.
hello , i want that , but i dont understain , please help me
I would put a login system into Live TV, can you help me ?
looks good I am going to try this out
i think it's good adn i think it will work. i will try later but i think there will be no problems, you did job very good, really amazing.
for those who still do not know how and what to do, just ask questions
again, great text and thank you
cheers
I recommend two scripts one is requests and the other is mechanise
(2015-05-16, 15:05)quangcanh Wrote: [ -> ]I would put a login system into Live TV, can you help me ?

What legal livetv service do you represent?
I tried and I need to write again, really good GUIDE, step by step. I have no objections, like I said, I will try it and I did.
i see that some of us had problems, but Im not sure where and why.
Once again, thank you anarchintosh.
Cheers
For transparency email moved to thread

(2015-12-01, 21:15)pipcan Wrote: [ -> ]i would like to know why you have had me blocked and second what is quote "What legal livetv service do you represent?" about so requests and mechanism are what band of have you watched the hursham show seen i was on it and well band me

@pipcan I do not have you blocked! I have no clue what you are talking about...

As for my post, its simple... @quangcanh stated he wants to add a login to his LiveTV plugin... there is only ONE way to legally lock content, and that's if you own it; to which I inquired "...what legal service does he represent?"

I'm only trying to determine the true nature of @quangcanh statement... Some users collect freely distributed illegal links, then lock and sell them... a shameful act!

If you are a user of questionable stature, who aligns themselves with characters you will not find comfort from me...

It should be noted, I have no issues with code being posted, only an issue when a user outright indicates his illegal intentions.
(2015-12-02, 21:04)Lunatixz Wrote: [ -> ]For transparency email moved to thread

Email to [email protected]:

(2015-12-01, 21:15)pipcan Wrote: [ -> ]i would like to know why you have had me blocked and second what is quote "What legal livetv service do you represent?" about so requests and mechanism are what band of have you watched the hursham show seen i was on it and well band me

@pipcan I do not have you blocked! I have no clue what you are talking about...

As for my post, its simple... @quangcanh stated he wants to add a login to his LiveTV plugin... there is only ONE way to legally lock content, and that's if you own it; to which I inquired "...what legal service does he represent?"

I'm only trying to determine the true nature of @quangcanh statement... Some users collect freely distributed illegal links, then lock and sell them... a shameful act!

If you are a user of questionable stature, who aligns themselves with characters you will not find comfort from me...

It should be noted, I have no issues with code being posted, only an issue when a user outright indicates his illegal intentions.
@Lunatixz - well done! after 3 drinks I still can't figure out WTF he's saying there.
Have been testing out this code keep getting LOGGED IN: False, what have i done wrong

here is my weblogin.py http://pastebin.com/yW0zTL9c what have I done wrong, Please help out
(2016-09-24, 04:07)delaroy Wrote: [ -> ]Have been testing out this code keep getting LOGGED IN: False, what have i done wrong

here is my weblogin.py http://pastebin.com/yW0zTL9c what have I done wrong, Please help out

not sure if you changed it, but your given username and pass are incorrect so obviously you get False.
Pages: 1 2