Esempio n. 1
0
def getSylabus(className, username, password):
    plugin.log.info("getSylabus for %s." % className.encode('utf-8'))

    cookies = getClassCookieOrLogin(username, password, className)
    url = get_syllabus_url(className=className)

    sylabus_txt = get_page(url, cookies=cookies, allow_redirects=False)
    plugin.log.debug("sylabus_txt = %s", sylabus_txt.decode(
        'unicode_escape', 'ignore').encode('ascii', 'ignore'))
    not_logged_in = 'with a Coursera account' in sylabus_txt or (
        "// First check the URL and line number of the error" in sylabus_txt)

    if not_logged_in:
        plugin.log.info("Cookies for %s are old. Logging in to class",
                        className.encode('utf-8'))
        cookies = getClassCookies(className, username, password)
        sylabus_txt = get_page(url, cookies=cookies, allow_redirects=False)
        plugin.log.debug("sylabus_txt = %s", sylabus_txt.encode('utf-8'))
        not_logged_in = 'with a Coursera account' in sylabus_txt or (
            "// First check the URL and line number of the error"
            in sylabus_txt)
        if not_logged_in:
            raise Exception("Unable to login to class")
        else:
            cookies_class = loadSavedClassCookies(username)
            cookies_class[className] = cookies.get_dict()

    parsed = parse_syllabus(sylabus_txt)

    return parsed
Esempio n. 2
0
def playLecture(courseShortName, lecture_id):
    username = plugin.get_setting('username')
    password = plugin.get_setting('password')

    if isSettingsBad(username, password):
        return []

    sylabus = getSylabus(courseShortName, username, password)
    if sylabus is None:
        return []

    sections = sylabus['sections']

    for lecture_contents in sections.values():
        sections = lecture_contents["sections"]
        for section_name, section in sections.iteritems():
            if section["lecture_id"] == lecture_id:
                plugin.log.debug("FOUND!: %s", section_name.encode(
                    'ascii', 'ignore'))

                cookies = getClassCookies(courseShortName, username, password)
                path = getContentURL(section, cookies)
                if path is not None:
                    plugin.log.info('Handle: ' + str(plugin.handle))

                    plugin.set_resolved_url(path)

                    if "Subtitle" in section['resources']:
                        player = xbmc.Player()
                        while not player.isPlaying():
                            time.sleep(1)
                        player.setSubtitles(section['resources']["Subtitle"])

                break
Esempio n. 3
0
def playLecture(courseShortName, lecture_id):
    username = plugin.get_setting('username')
    password = plugin.get_setting('password')

    if isSettingsBad(username, password):
        return []

    sylabus = getSylabus(courseShortName, username, password)
    if sylabus is None:
        return []

    sections = sylabus['sections']

    for lecture_contents in sections.values():
        sections = lecture_contents["sections"]
        for section_name, section in sections.iteritems():
            if section["lecture_id"] == lecture_id:
                plugin.log.debug("FOUND!: %s",
                                 section_name.encode('ascii', 'ignore'))

                cookies = getClassCookies(courseShortName, username, password)
                path = getContentURL(section, cookies)
                if path is not None:
                    plugin.log.info('Handle: ' + str(plugin.handle))

                    plugin.set_resolved_url(path)

                    if "Subtitle" in section['resources']:
                        player = xbmc.Player()
                        while not player.isPlaying():
                            time.sleep(1)
                        player.setSubtitles(section['resources']["Subtitle"])

                break
Esempio n. 4
0
def listLectureContents(courseShortName, section_num):
    username = plugin.get_setting('username')
    password = plugin.get_setting('password')
    number_episodes = plugin.get_setting('number_episodes', bool)

    if isSettingsBad(username, password):
        return []

    sylabus = getSylabus(courseShortName, username, password)
    if sylabus is None:
        return []

    instructor_name = sylabus['instructor_name']
    instructor_role = sylabus['instructor_role']

    sections = sylabus['sections']

    lecture_desired = None
    for lecture_contents in sections.values():
        if str(lecture_contents['section_num']) == section_num:
            lecture_desired = lecture_contents
            break

    if lecture_desired is None:
        plugin.log.warn("Lecture %d for %s not found",
                        section_num, courseShortName)
        return []

    section_lecture = lecture_desired["sections"]

    ret = []
    for section_name, section in section_lecture.iteritems():
        title, duration = extractDuration(section_name)

        lecture_num = section['lecture_num']  # int(section["lecture_id"])

        play_url = plugin.url_for(endpoint="playLecture",
                                  courseShortName=courseShortName,
                                  lecture_id=str(section["lecture_id"]))

        episode_num = lecture_num+1

        if number_episodes:
            title = "%d. %s" % (episode_num, title)

        info = {
            'episode': episode_num,
            'season': int(section_num)+1,
            'title': title,
            'watched': section["viewed"],
        }

        if instructor_name is not None and instructor_name != "":
            if instructor_role is not None and instructor_role != "":
                info['castAndRole'] = [(instructor_name, instructor_role)]
            else:
                info['cast'] = [instructor_name]

        if sylabus["course_name"] != "":
            info['tvshowtitle'] = sylabus["course_name"]

        if duration is not None:
            info["duration"] = duration

        if plugin.get_setting('enable_subtitles', bool):
            path = play_url
            if section['resources']["Lecture Video"] is None:
                path = None
        else:
            cookies = getClassCookies(courseShortName, username, password)
            path = getContentURL(section, cookies)

        plugin.log.debug(info)
        if path is not None:
            ret.append({
                'label': title,
                'path': path,
                'is_playable': True,
                'info': info,
                'context_menu': [
                    ("Play with subtitles", "XBMC.RunPlugin(%s)" % play_url),
                    ("Play with subtitles2", "XBMC.PlayMedia(%s)" % play_url)
                ],
            })
    plugin.add_sort_method(SortMethod.EPISODE)
    plugin.log.debug(dir(SortMethod))
    return list(sorted(ret, key=lambda x: x["info"]["episode"]))
Esempio n. 5
0
def listLectureContents(courseShortName, section_num):
    username = plugin.get_setting('username')
    password = plugin.get_setting('password')
    number_episodes = plugin.get_setting('number_episodes', bool)

    if isSettingsBad(username, password):
        return []

    sylabus = getSylabus(courseShortName, username, password)
    if sylabus is None:
        return []

    instructor_name = sylabus['instructor_name']
    instructor_role = sylabus['instructor_role']

    sections = sylabus['sections']

    lecture_desired = None
    for lecture_contents in sections.values():
        if str(lecture_contents['section_num']) == section_num:
            lecture_desired = lecture_contents
            break

    if lecture_desired is None:
        plugin.log.warn("Lecture %d for %s not found", section_num,
                        courseShortName)
        return []

    section_lecture = lecture_desired["sections"]

    ret = []
    for section_name, section in section_lecture.iteritems():
        title, duration = extractDuration(section_name)

        lecture_num = section['lecture_num']  # int(section["lecture_id"])

        play_url = plugin.url_for(endpoint="playLecture",
                                  courseShortName=courseShortName,
                                  lecture_id=str(section["lecture_id"]))

        episode_num = lecture_num + 1

        if number_episodes:
            title = "%d. %s" % (episode_num, title)

        info = {
            'episode': episode_num,
            'season': int(section_num) + 1,
            'title': title,
            'watched': section["viewed"],
        }

        if instructor_name is not None and instructor_name != "":
            if instructor_role is not None and instructor_role != "":
                info['castAndRole'] = [(instructor_name, instructor_role)]
            else:
                info['cast'] = [instructor_name]

        if sylabus["course_name"] != "":
            info['tvshowtitle'] = sylabus["course_name"]

        if duration is not None:
            info["duration"] = duration

        if plugin.get_setting('enable_subtitles', bool):
            path = play_url
            if section['resources']["Lecture Video"] is None:
                path = None
        else:
            cookies = getClassCookies(courseShortName, username, password)
            path = getContentURL(section, cookies)

        plugin.log.debug(info)
        if path is not None:
            ret.append({
                'label':
                title,
                'path':
                path,
                'is_playable':
                True,
                'info':
                info,
                'context_menu':
                [("Play with subtitles", "XBMC.RunPlugin(%s)" % play_url),
                 ("Play with subtitles2", "XBMC.PlayMedia(%s)" % play_url)],
            })
    plugin.add_sort_method(SortMethod.EPISODE)
    plugin.log.debug(dir(SortMethod))
    return list(sorted(ret, key=lambda x: x["info"]["episode"]))