Skip to content

Tonyy18/Sololearn-API

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Sololearn API Unofficial

Sololearn API for python.

Login system needs to be updated!! Not working otherwise

The script is outdated and propably requires javascript. Will be converted into selenium at some point

Requires requests, BeautifulSoup 4 and json libraries

Documentation

Login

Before you can use this API you must login with your sololearn account

import sl
login = sl.Login("email", "password")

Login instance contains following properties

getPosts

Returns a list of Post objects

Parameters
  • order Trending, MostRecent, Unanswered, MyQuestions, MyAnswers. Trending is the default value
  • page Each page returns 20 posts
  • query Keyword used to search posts
import sl
login = sl.Login("email", "password")
posts = login.getPosts(order="myquestions", page=2)

More information in Post object section

getCodes

Returns a list of Code objects

Parameters
  • order Trending, MostRecent, MostPopular, MyCodes
  • page Each page has 20 codes
  • query Keyword used to search codes
  • language Codes by specific language. Must be used with the alias of the language. Listed below. Searches all languages by default
Language aliases

web, cpp, c, cs, java, py, php, rb, kt, swift

import sl
login = sl.Login("email", "password")
codes = login.getCodes(query="website", language="web")

More information in Code object section

getUser

Returns User object for given user id

import sl
login = sl.Login("email", "password")
user = login.getUser(7745624)
print(user.name)
print(user.level + " " + user.xp)

More information in User object section

newCode

Creates and saves a new code for your user

Parameters
  • name
  • code source code
  • cssCode only used with web language to specify source code for css
  • jsCode only used with web language to specify source code for javascript
  • language Required. Must be used with the alias of the language. Listed below
  • public boolean
Language aliases

web, cpp, c, cs, java, py, php, rb, kt, swift

import sl
login = sl.Login("email", "password")
login.newCode(language="py", name="Python code", code="print('hello world')", public=True)
login.newCode(language="web", name="Web code", code="html code", cssCode="css code", jsCode="javascript code", public=False)

Post

Post object contains information for a single post

It contains following properties

getAnswers

Returns a list of Answer objects

Parameters
  • index starting index. Returns 20 answers starting from the index
  • order get answers by votes or date
import sl
login = sl.Login("email", "password")
posts = login.getPosts()
answers = posts[0].getAnswers(order="date", index=100)

More information in Answer object section

sendAnswer

Sends an answer to the parent Post object

Parameters
  • text text for the answer

edit

(Post object)

Parameters
  • title
  • text
  • tags (list)

Only possible for your own posts/questions

If parameter is not used it will keep its original content

import sl
login = sl.Login("email, "password")
posts = login.getPosts(order="myquestions")
posts[0].edit(title="answer title", text="answer body", tags=["tag1", "tag2"])

User

Is used to store information for user

profile

This method is used to get more specific information about the user

Returns Profile object

This method takes no parameters

Profile

Retrieves more specific information for user

Only callable from User object

It contains following properties

  • id
  • name
  • avatar
  • level
  • xp
  • courses (dictionary)
  • codes (list of Code objects)
  • certificates (dictionary)
  • achievements (dictionary)

Parent object properties are synchronized along with the Profile object properties

Answer

Answer object contains information for a single answer

Answer objects are returned from getAnswers method in Post object

It contains following properties

edit

(Answer object)

Takes a single string as parameter

Only possible for your own answers

import sl
login = sl.Login("email", "password")
posts = login.getPosts()
answers = posts[0].getAnswers(order="date", index=100)
answers[0].edit("edited")

Code

Post object contains information for a single post

It contains following properties

source

Returns dictionary including source code for Code object

Values
  • code the actual source code. Contains html source code when using web language
  • cssCode Contains source code for css language. only used with web language. Otherwise empty
  • jsCode Contains source code for javascript language. only used with web language. Otherwise empty
import sl
login = sl.Login("email", "password")
codes = login.getCodes()
source = codes[0].source()
print(source["code"])

edit

(Code object)

Parameters
  • name
  • code source code
  • cssCode only used when editing code with web language
  • jsCode only used when editing code with web language
  • language Keeps the original language by default. Must be used with the alias of the language. Listed below
  • public

Only possible for your own codes

Language aliases

web, cpp, c, cs, java, py, php, rb, kt, swift

import sl
login = sl.Login("email", "password")
codes = login.getCodes()
codes[0].edit(name="Edited", code="print('edited')", public=False)

execute

Executes the code and returns the output

Doesn't work with web language

import sl
login = sl.Login("email", "password")
login.newCode(language="py", code="print('Hello World')")
codes = login.profile().codes
print(codes[0].execute())

delete

This method is property of Post, Answer and Code objects

Deletes the object

This method takes no parameters

import sl
login = sl.Login("email", "password")
posts = login.getPosts(order="myquestions")
codes = login.getCodes(order="mycodes")
answers = posts[0].getAnswers()
posts[0].delete()
codes[0].delete()
if(answers[0].author.id == login.id):
    answers[0].delete()

vote

This method is property of Post, Answer and Code objects

Sends a vote to the parent object

parameters
  • amount values 1, 0 and -1

value 1 upvotes

value 0 removes upvote or downvote

value -1 downvotes

import sl
login = sl.Login("email", "password")
posts = login.getPosts()
codes = login.getCodes()
answers = posts[0].getAnswers()
posts[0].vote(1)
codes[0].vote(0)
answers[0].vote(-1)

json

Returns json data from the parent object

Exists in every object

Examples

Delete all your questions/codes

import sl
login = sl.Login("email", "password")
questions = login.getPosts(order="myquestions")
for question in questions:
    question.delete()

User info for trending codes

import sl
login = sl.Login("email", "password")
codes = login.getCodes()
for code in codes:
    print(code.author.id)
    print(code.author.name)
    print(code.author.avatar)

Create new code and execute it

import sl
login = sl.Login("email", "password")
login.newCode(language="py", name="new code", code="print('Hello World')")
codes = login.profile().codes
print(codes[0].execute())

Releases

No releases published

Packages

No packages published

Languages