Пример #1
0
def main():
    # Variables to log
    run_time = datetime.datetime.utcnow()
    edited_pages = False
    wrote_db = False
    logged_errors = False

    try:
        prevruntimestamp = timelog(run_time)
    except Exception as e:
        mblog.logerror(u'Could not get time of previous run', exc_info=True)
        logged_errors = True
        sys.exit()

    # Initializing site + logging in
    login = config['login']
    try:
        site = mwclient.Site((login['protocol'], login['site']),
                             clients_useragent=login['useragent'])
        site.login(login['username'], login['password'])
    except mwclient.LoginError as e:
        mblog.logerror(u'Login failed for {}'.format(login['username']),
                       exc_info=True)
        logged_errors = True
        sys.exit()

    # create a list of learners who have joined since the previous run
    learners = getlearnerinfo(getlearners(prevruntimestamp, site), site)
    mentors, genmentors = getmentors(site)

    for learner in learners:
        try:
            mentorcat = mentorcat_dict[learner['category']]
            mentor = match(mentors[mentorcat], genmentors)
        except Exception as e:
            mblog.logerror(u'Matching failed for {}'.format(
                learner['learner']),
                           exc_info=True)
            logged_errors = True
            continue

        try:
            mname, muid, matchmade = get_match_info(mentor, site)
            print((mname, muid, matchmade))
        except Exception as e:
            mblog.logerror(u'Could not get information for profile {}'.format(
                mentor['profile']),
                           exc_info=True)
            logged_errors = True
            continue

        try:
            invite_info = get_invite_info(learner, mname, matchmade, site)
            response = postinvite(invite_info)
            edited_pages = True
        except Exception as e:
            mblog.logerror(u'Could not post match on {}\'s page'.format(
                learner['learner']),
                           exc_info=True)
            logged_errors = True
            continue

        try:
            flowenabled = invite_info[3]
            revid, postid = getrevid(response, flowenabled)
            matchtime = gettimeposted(response, flowenabled)
            cataddtime = parse_timestamp(learner['cattime'])

            mblog.logmatch(luid=learner['luid'],
                           lprofileid=learner['profileid'],
                           muid=muid,
                           category=learner['category'],
                           cataddtime=cataddtime,
                           matchtime=matchtime,
                           matchmade=matchmade,
                           revid=revid,
                           postid=postid,
                           run_time=run_time)
            wrote_db = True
        except Exception as e:
            mblog.logerror(u'Could not write to DB for {}'.format(
                learner['learner']),
                           exc_info=True)
            logged_errors = True
            continue

    try:
        mblog.logrun(run_time, edited_pages, wrote_db, logged_errors)
    except Exception as e:
        mblog.logerror(u'Could not log run at {}'.format(run_time),
                       exc_info=True)
Пример #2
0
def main(filepath):
    run_time = datetime.datetime.utcnow()
    config = utils.load_config(filepath)
    edited_pages = False
    wrote_db = False
    logged_errors = False

    try:
        prevruntimestamp = utils.timelog(run_time, filepath)
    except Exception as e:
        mblog.logerror(u'Could not get time of previous run', exc_info=True)
        logged_errors = True
        mblog.logrun()
        sys.exit()

    try:
        site = login(config['login'])
    except mwclient.LoginError as e:
        mblog.logerror(u'Login failed for {}'.format(creds['username']),
                       exc_info=True)
        logged_errors = True
        mblog.logrun(filepath, run_time, edited_pages, wrote_db, logged_errors)
        sys.exit()

    try:
        profile_lists = get_profiles(prevruntimestamp, config['categories'],
                                     site)
        bare_profiles = filter_profiles(profile_lists[0], profile_lists[1],
                                        config['pages']['main'])
        new_profiles = get_profile_info(bare_profiles, config['categories'],
                                        config['pages'], site)
    except Exception as e:
        mblog.logerror(u'Could not construct profile dictionaries',
                       exc_info=True)
        logged_errors = True
        mblog.logrun(filepath, run_time, edited_pages, wrote_db, logged_errors)
        sys.exit()

    try:
        active_ideas = get_active_ideas(run_time, config)
    except Exception as e:
        mblog.logerror(u'Could not fetch active ideas from idealab_ideas',
                       exc_info=True)
        logged_errors = True
        mblog.logrun()
        sys.exit()

    ideas = {}

    for profile in new_profiles:
        skills = new_profiles[profile]['skills']
        interests = new_profiles[profile]['interests']

        try:
            idea_list = get_ideas_by_category(ideas, interests, skills, site,
                                              config['categories'])
            active_idea_list = filter_ideas(idea_list, active_ideas)
            final_ideas = choose_ideas(active_idea_list, 5)
        except Exception as e:
            mblog.logerror(u'Could not generate ideas for {}'.format(
                profile['profile_title']), exc_info=True)
            logged_errors = True
            continue

        # If not enough ideas, search on interest and skill individually
        if len(final_ideas) < 5:
            final_ideas = get_more_ideas(final_ideas, interests, skills, site,
                                         config['categories'])
        else:
            pass

        # If still not enough, fill from the list of all ideas
        if len(final_ideas) < 5:
            final_ideas = get_more_ideas(final_ideas, [], [], site,
                                         config['categories'])
        else:
            pass

        try:
            greeting = utils.buildgreeting(config['greetings']['greeting'],
                new_profiles[profile]['username'], final_ideas)
        except Exception as e:
            mblog.logerror(u'Could not create a greeting for {}'.format(
                learner['learner']), exc_info=True)
            logged_errors = True
            continue

        try:
            response = postinvite(new_profiles[profile]['talk_title'],
                                  greeting, 'Ideas for you', site)
            edited_pages = True
        except mwclient.MwClientError as e:
            mblog.logerror(u'Could not post match on {}\'s page'.format(
                profile['username']), exc_info=True)
            logged_errors = True
            continue

        try:
            match_info = collect_match_info(response, new_profiles[profile],
                                            final_ideas, run_time)
            sqlutils.logmatch(match_info, config['dbinfo'])
            wrote_db = True
        except Exception as e:
            mblog.logerror(u'Could not write to DB for {}'.format(
                learner['learner']), exc_info=True)
            logged_errors = True
            continue

    mblog.logrun(filepath, run_time, edited_pages, wrote_db, logged_errors)
Пример #3
0
def main():
    # Variables to log
    run_time = datetime.datetime.utcnow()
    edited_pages = False
    wrote_db = False
    logged_errors = False

    try:
        prevruntimestamp = timelog(run_time)
    except Exception as e:
        mblog.logerror(u'Could not get time of previous run', exc_info=True)
        logged_errors = True
        sys.exit()

    # Initializing site + logging in
    login = config['login']
    try:
        site = mwclient.Site((login['protocol'], login['site']),
                             clients_useragent=login['useragent'])
        site.login(login['username'], login['password'])
    except mwclient.LoginError as e:
        mblog.logerror(u'Login failed for {}'.format(login['username']),
                       exc_info=True)
        logged_errors = True
        sys.exit()

    # create a list of learners who have joined since the previous run
    learners = getlearnerinfo(getlearners(prevruntimestamp, site), site)
    mentors, genmentors = getmentors(site)

    for learner in learners:
        try:
            mentorcat = mentorcat_dict[learner['category']]
            mentor = match(mentors[mentorcat], genmentors)
        except Exception as e:
            mblog.logerror(u'Matching failed for {}'.format(
                learner['learner']), exc_info=True)
            logged_errors = True
            continue

        try:
            mname, muid, matchmade = get_match_info(mentor, site)
            print((mname, muid, matchmade))
        except Exception as e:
            mblog.logerror(u'Could not get information for profile {}'.format(
                mentor['profile']), exc_info=True)
            logged_errors = True
            continue

        try:
            invite_info = get_invite_info(learner, mname,
                                                  matchmade, site)
            response = postinvite(invite_info)
            edited_pages = True
        except Exception as e:
            mblog.logerror(u'Could not post match on {}\'s page'.format(
                learner['learner']), exc_info=True)
            logged_errors = True
            continue

        try:
            flowenabled = invite_info[3]
            revid, postid = getrevid(response, flowenabled)
            matchtime = gettimeposted(response, flowenabled)
            cataddtime = parse_timestamp(learner['cattime'])

            mblog.logmatch(luid=learner['luid'],
                           lprofileid=learner['profileid'],
                           muid=muid, category=learner['category'],
                           cataddtime=cataddtime,
                           matchtime=matchtime, matchmade=matchmade,
                           revid=revid, postid=postid, run_time=run_time)
            wrote_db = True
        except Exception as e:
            mblog.logerror(u'Could not write to DB for {}'.format(
                learner['learner']), exc_info=True)
            logged_errors = True
            continue

    try:
        mblog.logrun(run_time, edited_pages, wrote_db, logged_errors)
    except Exception as e:
        mblog.logerror(u'Could not log run at {}'.format(run_time),
                       exc_info=True)
Пример #4
0
    try:
        skill_idea_list = get_ideas_by_category(ideas, [], skills, site,
                                                config['categories'])
        interest_idea_list = get_ideas_by_category(ideas, interests, [], site,
                                                   config['categories'])
        active_add_ideas = filter_ideas(skill_idea_list + interest_idea_list,
                                          active_ideas)
        unique_active_extra_ideas = [x for x in active_add_ideas if x not in 
                                     final_ideas]
        ideas_to_add = choose_ideas(unique_active_extra_ideas,
                                    5 - len(final_ideas))
        final_ideas = final_ideas + ideas_to_add
        return final_ideas

    except Exception as e:
        mblog.logrun(u'Could not get more ideas', exc_info=True)
        logged_errors = True


def postinvite(pagetitle, greeting, topic, site):
    """Add a new section to a page and return the API POST result."""
    profile = site.Pages[pagetitle]
    result = profile.save(greeting, section='new', summary=topic)
    return result


def collect_match_info(response, profile_dict, matched_ideas, run_time):
    """Prepare information about matches made to be added to the
    matches database table. Return a list of dicts, where each
    dict is a row to add to the database.
    """