def courseinfo(id): if id <= 0 or len(str(id)) >= 10: raise mem_exceptions.CourseError("This course can't exist!") try: coursetest(id) #does the course exist? if exist == False: raise mem_exceptions.CourseError("This course doesn't exist!") text = get_pagetext("http://memrise.com/course/" + str(id)) matched_lines = [line for line in text.split('\n') if "" in line] url = ("http://memrise.com/course/" + str(id) + "/" + str(matched_lines[25]) + "/leaderboard").replace(" ", "-").lower() author = get_pagetext(url) author_line = [line for line in author.split('\n') if "" in line] if matched_lines[26] != u"Welcome to Memrise!": line26 = "\n" + matched_lines[26] else: line26 = "" try: return str( matched_lines[22:25] ) + "\n" + matched_lines[25] + line26 + "\n" + author_line[22] except IndexError: pass except UnicodeEncodeError: if matched_lines[26] != u"Welcome to Memrise!": line26 = "\n" + matched_lines[26] else: line26 = "" return str(matched_lines[22:25]) + "\n" + matched_lines[25] + line26 except IOError: raise mem_exceptions.NoConnection()
def courses_teaching_number(user): usertest(user) if exist == False: raise mem_exceptions.UserError() if exist == True: try: url = "http://memrise.com/user/" + user + "/courses/teaching" text = get_pagetext(url) matched_lines = [line for line in text.split('\n') if "" in line] return matched_lines[36] except IOError: raise mem_exceptions.NoConnection()
def usertest(user): try: url = "https://www.memrise.com/user/" + user global pagetext pagetext = urllib.urlopen(url).read() if "<h2>The page you are trying to get to doesn't exist.</h2>" in pagetext: global exist exist = False else: exist = True except IOError: raise mem_exceptions.NoConnection()
def coursenumber(): try: start = 1580000 for i in range(1, 20000, 1000): nstart = start + i print nstart url = "http://www.memrise.com/course/" + str(nstart) if "<h2>The page you are trying to get to doesn't exist.</h2>" in urllib.urlopen( url).read(): return "Current course: " + nstart break except IOError: raise mem_exceptions.NoConnection()
def coursetest(id): try: url = "http://www.memrise.com/course/" + str(id) if "<h2>The page you are trying to get to doesn't exist.</h2>" in urllib.urlopen( url).read(): global exist exist = False return exist else: exist = True return exist except IOError: raise mem_exceptions.NoConnection()
def courses_teaching(user): usertest(user) if exist == False: raise mem_exceptions.UserError() if exist == True: try: url = "http://memrise.com/user/" + user + "/courses/teaching" text = get_pagetext(url) matched_lines = [line for line in text.split('\n') if "" in line] for el in range(39, len(matched_lines)-42): print matched_lines[el] except IOError: raise mem_exceptions.NoConnection()
def points(user): usertest(user) if exist == False: raise mem_exceptions.UserError() if exist == True: try: url = "https://www.memrise.com/user/" + user points_line = linenumber(url, 'Points') lines = pagetext.split("\n") return lines[points_line-4] except TypeError: return 0 except IOError: raise mem_exceptions.NoConnection()
def mems(user): usertest(user) if exist == False: raise mem_exceptions.UserError() if exist == True: try: url = "https://www.memrise.com/user/" + user + "/mems/created/" mems_line = linenumber(url, 'Created') lines = pagetext.split("\n") return lines[mems_line-1] except TypeError: return 0 except IOError: raise mem_exceptions.NoConnection()
def top50learncompetition(user): """True, if the user is in the top 50 of the September Back-to-School learning Competition""" usertest(user) if exist == False: raise mem_exceptions.UserError() if exist == True: try: url = "http://memrise.com/user/" + user if 'data-original-title="TOP' in urllib.urlopen(url).read(): return True else: return False except IOError: raise mem_exceptions.NoConnection()
def promember(user): """True, if user is pro member and False, if user isn't pro member!""" usertest(user) if exist == False: raise mem_exceptions.UserError() if exist == True: try: url = "http://memrise.com/user/" + user if 'data-original-title="Pro' in urllib.urlopen(url).read(): return True else: return False except IOError: raise mem_exceptions.NoConnection()
def number_of_following(user): usertest(user) if exist == False: raise mem_exceptions.UserError() if exist == True: try: url = "https://www.memrise.com/user/" + user following_line = linenumber(url, 'Following') lines = pagetext.split("\n") return lines[following_line-5] except TypeError: return 0 except IOError: raise mem_exceptions.NoConnection()
def dailygoal(user): usertest(user) if exist == False: raise mem_exceptions.UserError() if exist == True: try: url = "https://www.memrise.com/user/" + user + "/mems/created/" mems_line = linenumber(url, 'streak') lines = list(pagetext.split("\n")) line = str(lines[mems_line+9]) repl = line.replace("</b>", "").replace("<b>", "") #kill HTML return repl except TypeError: pass except IOError: raise mem_exceptions.NoConnection()
def allcourses(course_info=False): """print a list of all available courses""" if course_info == False: for i in range(1600000): if coursetest(i) == True: print i if course_info == True: for i in range(1600000): try: if coursetest(i) == True: print str(i), courseinfo(i) except KeyboardInterrupt: break except IOError: raise mem_exceptions.NoConnection() if course_info == Ellipsis: raise SyntaxError else: raise mem_exceptions.CourseError( "course_info can't be like this. Only true or false.")