コード例 #1
0
ファイル: matchbot.py プロジェクト: fhocutt/matchbot
def getmentors(site):
    """Using the config data, get lists of available mentors for each
    category, filtering out mentors who have opted out of new matches.

    Return:
        mentors     : dict of lists of mentor names, keyed by mentor
                        category
        genmentors  : list of mentor names for mentors who will mentor
                        on any topic

    Assumes that the owner of the profile created the profile.
    """
    mentors = {}

    nomore = mbapi.getallcatmembers(optout, site)
    allgenmentors = mbapi.getallcatmembers(general, site)
    genmentors = [
        x for x in allgenmentors
        if x not in nomore and x['profile'].startswith(prefix)
    ]
    for category in mentorcats:
        try:
            catmentors = mbapi.getallcatmembers(category, site)
            mentors[category] = [
                x for x in catmentors
                if x not in nomore and x['profile'].startswith(prefix)
            ]
        except Exception as e:
            mblog.logerror(
                'Could not fetch list of mentors for {}'.format(category),
                exc_info=True)
            logged_errors = True
            continue
    return (mentors, genmentors)
コード例 #2
0
ファイル: matchbot.py プロジェクト: fhocutt/matchbot
def getmentors(site):
    """Using the config data, get lists of available mentors for each
    category, filtering out mentors who have opted out of new matches.

    Return:
        mentors     : dict of lists of mentor names, keyed by mentor
                        category
        genmentors  : list of mentor names for mentors who will mentor
                        on any topic

    Assumes that the owner of the profile created the profile.
    """
    mentors = {}

    nomore = mbapi.getallcatmembers(optout, site)
    allgenmentors = mbapi.getallcatmembers(general, site)
    genmentors = [x for x in allgenmentors if x not in nomore and
                  x['profile'].startswith(prefix)]
    for category in mentorcats:
        try:
            catmentors = mbapi.getallcatmembers(category, site)
            mentors[category] = [x for x in catmentors if x not in nomore and
                                 x['profile'].startswith(prefix)]
        except Exception as e:
            mblog.logerror('Could not fetch list of mentors for {}'.format(
                category), exc_info=True)
            logged_errors = True
            continue
    return (mentors, genmentors)
コード例 #3
0
ファイル: matchbot.py プロジェクト: Wikimedia-Sverige/hostbot
def getlearners(prevruntimestamp, site):
    """Get a list of learners who have created profiles since the last
    time this script started running.

    Returns a list of dictionaries, each containing the learner's
    profile pageid, the profile page title, the category, and the
    timestamp corresponding to when the category was added.

    If it is not possible to retrieve the new profiles in a given
    category, it skips that category and logs an error.
    """
    learners = []
    for category in requestcats:
        try:
            newlearners = mbapi.getnewmembers(category, site,
                                              prevruntimestamp)
            for userdict in newlearners:
                # Check that the page is actually in the Co-op
                if userdict['profile'].startswith(prefix):
                    learners.append(userdict)
                else:
                    pass
        except Exception as e:
            mblog.logerror('Could not fetch new profiles in {}'.format(
                category), exc_info=True)
            logged_errors = True
    return learners
コード例 #4
0
ファイル: matchbot.py プロジェクト: fhocutt/matchbot
def getlearners(prevruntimestamp, site):
    """Get a list of learners who have created profiles since the last
    time this script started running.

    Returns a list of dictionaries, each containing the learner's
    profile pageid, the profile page title, the category, and the
    timestamp corresponding to when the category was added.

    If it is not possible to retrieve the new profiles in a given
    category, it skips that category and logs an error.
    """
    learners = []
    for category in requestcats:
        try:
            newlearners = mbapi.getnewmembers(category, site, prevruntimestamp)
            mblog.logerror(newlearners)
            for userdict in newlearners:
                # Check that the page is actually in the Co-op
                if userdict['profile'].startswith(prefix):
                    learners.append(userdict)
                else:
                    pass
        except Exception as e:
            mblog.logerror(
                'Could not fetch new profiles in {}'.format(category),
                exc_info=True)
            logged_errors = True
    mblog.logerror(learners)
    mblog.logerror(learners == newlearners)
    return learners
コード例 #5
0
ファイル: matchbot.py プロジェクト: Wikimedia-Sverige/hostbot
def getlearnerinfo(learners, site):
    """Given a list of dicts containing information on learners, add
    the learner's username and userid to the corresponding dict. Return
    the changed list of dicts.

    Assumes that the owner of the profile created the profile.
    """
    for userdict in learners:
        try:
            learner, luid = mbapi.getpagecreator(userdict['profile'], site)
            userdict['learner'] = learner
            userdict['luid'] = luid
        except Exception as e:
            mblog.logerror('Could not get information for {}'.format(
                userdict['profile']), exc_info=True)
            logged_errors = True
            continue
    return learners
コード例 #6
0
ファイル: matchbot.py プロジェクト: fhocutt/matchbot
def getlearnerinfo(learners, site):
    """Given a list of dicts containing information on learners, add
    the learner's username and userid to the corresponding dict. Return
    the changed list of dicts.

    Assumes that the owner of the profile created the profile.
    """
    for userdict in learners:
        try:
            learner, luid = mbapi.getpagecreator(userdict['profile'], site)
            userdict['learner'] = learner
            userdict['luid'] = luid
        except Exception as e:
            mblog.logerror('Could not get information for {}'.format(
                userdict['profile']),
                           exc_info=True)
            logged_errors = True
            continue
    mblog.logerror(learners)
    return learners
コード例 #7
0
ファイル: matchbot.py プロジェクト: fhocutt/matchbot
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)
コード例 #8
0
ファイル: matchbot.py プロジェクト: fhocutt/matchbot
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)
コード例 #9
0
ファイル: matching.py プロジェクト: kpolimis/grantsbot
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)