Exemple #1
0
def create_user_from_github_user(access_token, github_user):
    # FIXME: Unacceptable method. Too long, doing a lot of stuff, error prone, etc..
    # FIXME: Refactor me please. Destruct me

    logging.warn("[USER][GITHUB] {}".format(github_user))

    user_login = github_user.get("login")

    social_account = SocialUser.query.filter_by(nick=user_login,
                                                acc_type='gh').first()

    user = User.query.filter_by(nick=user_login).first()

    #if user is not None:
    #    return user
    u = user
    email = github_user.get("email", "")
    name = github_user.get("name", "")

    if user is None:
        u = User()
        #u.id = idgen_client.get()
        u.nick = user_login
        u.email = email
        u.pic_url = github_user.get("avatar_url")

        name_parts = name.split(" ")

        if len(name_parts) > 1:
            u.first_name = name_parts[0] or ""
            u.last_name = " ".join(name_parts[1:])
        else:
            u.last_name = name

    if social_account is None:

        su = SocialUser()

        su.user_id = u.id
        su.nick = user_login
        su.acc_type = 'gh'
        su.email = email
        su.follower_count = github_user.get("followers")
        su.following_count = github_user.get("following")
        su.blog = github_user.get("blog")
        su.ext_id = github_user.get("id")
        su.name = github_user.get("name", "")
        su.hireable = github_user.get("hireable", False)
        su.access_token = access_token
        u.social_accounts.append(su)

        db.session.add(u)

        try:
            db.session.commit()
        except Exception, e:
            logging.warn(e)
            db.session.rollback()
            sentry_client.captureException()
        finally:
Exemple #2
0
def get_feed(rss_url, stats=False):
    post_ids = []

    try:
        for o in download_feed_return_objects(rss_url):
            if o is None:
                continue
            if isinstance(o, Post):
                post_ids.append(o.id)
            session.add(o)
        try:
            if len(post_ids):
                session.commit()
        except Exception, ex:
            session.rollback()
            error_reporter.captureException()
            logging.exception(ex)
            return False

        if len(post_ids) > 0:
            logging.info(u"Saved %d posts for %s" %
                         (len(post_ids), safe_str(rss_url)))
        else:
            logging.info(u"No Posts saved for %s" % safe_str(rss_url))

        for id in post_ids:
            if stats:
                task('get_post_stats', args=(str(id), )).apply_async()
            task('index_post', args=(str(id), )).apply_async()
Exemple #3
0
def get_profile_by_nick(nick):
    user = None
    exception = False

    try:
        user = CsUser.filter(nick=nick).first()
    except Exception, ex:
        exception = True
        logging.exception(ex)
        sentry_client.captureException()
Exemple #4
0
 def load_user(user_id):
     logging.warn("[USER]Finding user {}".format(user_id))
     try:
         return User.query.get(user_id)
     except Exception, ex:
         logging.exception(ex)
         try:
             from pyhackers.sentry import sentry_client  # OMG
             sentry_client.captureException()
         finally:
             return None
Exemple #5
0
 def load_user(user_id):
     logging.warn("[USER]Finding user {}".format(user_id))
     try:
         return User.query.get(user_id)
     except Exception, ex:
         logging.exception(ex)
         try:
             from pyhackers.sentry import sentry_client  # OMG
             sentry_client.captureException()
         finally:
             return None
Exemple #6
0
def index_data(data, index='sweet', doc='post', id=None):
    logging.warn("Indexing data %s" % (id if id is not None else ""))
    try:
        res = conn.index(data, index, doc, id=id)
    except ElasticSearchException:
        sentry_client.captureException()
        return False

    id = None

    if res is not None:
        id = res.get("_id", None)

    return id, res
Exemple #7
0
def find_existing_posts(feed_id, post_id_hashes, post_link_hashes):
    try:

        return session.query(Post.post_id_hash, Post.link_hash).filter(
            and_(
                Post.feed_id == feed_id,
                or_(
                    Post.post_id_hash.in_(post_id_hashes),
                    Post.link_hash.in_(post_link_hashes),
                )))
    except:
        session.rollback()
        error_reporter.captureException()
        return None
Exemple #8
0
def rss_exists(url):
    rss_hash = url_hash(url)
    feed = None

    try:
        feed = session.query(Feed).filter_by(rss_hash=rss_hash).first()
    except:
        error_reporter.captureException()
        try:
            session.rollback()
        except:
            error_reporter.captureException()
        raise

    return feed
Exemple #9
0
def load_project(slug, current_user):

    project = OpenSourceProject.query.filter_by(slug=slug).first()
    if project is None:
        return

    related_projects = OpenSourceProject.query.filter_by(parent=slug).order_by(
        OpenSourceProject.watchers.desc()).limit(100)

    follower_list = []

    try:
        followers = [f.user_id for f in ProjectFollower.filter(project_id=project.id).limit(20)]
        follower_list = [f for f in user_list_from_ids(followers)]
    except Exception, ex:
        sentry_client.captureException()
        logging.exception(ex)
Exemple #10
0
def load_user(user_id, current_user=None):
    """
    Loads all the details about the user including Followers/Following/OpenSource projects list.
    """
    if user_id is None:
        return None

    logging.warn("Loading user {}".format(user_id))
    user = User.query.get(user_id)

    user_followers, user_following = [], []

    # FIXME: This try/except block is ugly as hell. Refactor please!
    try:
        followers = [
            f.follower_id
            for f in UserFollower.filter(user_id=user_id).limit(20)
        ]
        following = [
            f.following_id
            for f in UserFollowing.filter(user_id=user_id).limit(20)
        ]

        cassa_users = user_list_from_ids(set(followers + following), dict=True)

        def expand(o):
            extras = o.extended
            dict_val = o._as_dict()
            dict_val.update(**extras)
            return dict_val

        user_followers = [
            filter(lambda x: x.get('id') == u, cassa_users)[0]
            for u in followers
        ]
        user_following = [
            filter(lambda x: x.get('id') == u, cassa_users)[0]
            for u in following
        ]

    except Exception, ex:
        logging.warn(ex)
        sentry_client.captureException()
Exemple #11
0
def get_rss(url):
    results = None
    success = False
    feed_parser_inst = None

    try:
        feed_parser_inst = download_rss(url)
        success = True
    except TimeoutError:
        pass
    except (UnicodeEncodeError, Exception):
        error_reporter.captureException(**dict(url=url))

    if success:
        results = extract_rss_results(feed_parser_inst, url=url)
    else:
        print "Url not successfull %s " % safe_str(url)

    return results, success
Exemple #12
0
def load_project(slug, current_user):

    project = OpenSourceProject.query.filter_by(slug=slug).first()
    if project is None:
        return

    related_projects = OpenSourceProject.query.filter_by(parent=slug).order_by(
        OpenSourceProject.watchers.desc()).limit(100)

    follower_list = []

    try:
        followers = [
            f.user_id
            for f in ProjectFollower.filter(project_id=project.id).limit(20)
        ]
        follower_list = [f for f in user_list_from_ids(followers)]
    except Exception, ex:
        sentry_client.captureException()
        logging.exception(ex)
Exemple #13
0
def load_user(user_id, current_user=None):
    logging.warn("Loading user {}".format(user_id))
    user = User.query.get(user_id)

    user_followers, user_following, os_projects = [], [], []

    # FIXME: This try/except block is ugly as hell. Refactor please!
    try:
        followers = [
            f.follower_id
            for f in UserFollower.filter(user_id=user_id).limit(20)
        ]
        following = [
            f.following_id
            for f in UserFollowing.filter(user_id=user_id).limit(20)
        ]

        projects = [p.project_id for p in UserProject.filter(user_id=user_id)]
        os_projects = OpenSourceProject.query.filter(
            OpenSourceProject.id.in_(projects)).order_by(
                OpenSourceProject.stars.desc()).all()
        cassa_users = user_list_from_ids(set(followers + following), dict=True)

        def expand(o):
            extras = o.extended
            dict_val = o._as_dict()
            dict_val.update(**extras)
            return dict_val

        user_followers = [
            filter(lambda x: x.get('id') == u, cassa_users)[0]
            for u in followers
        ]
        user_following = [
            filter(lambda x: x.get('id') == u, cassa_users)[0]
            for u in following
        ]

    except Exception, ex:
        logging.warn(ex)
        sentry_client.captureException()
Exemple #14
0
        except Exception, e:
            logging.warn(e)
            db.session.rollback()
            sentry_client.captureException()
        finally:
            CsUser.create(id=u.id, nick=u.nick, extended=dict(pic=u.pic_url))
        try:
            notification = u"Id:{} Nick:[{}] Name:[{}] Email:[{}] Followers:{}".format(
                u.id, user_login, name, email, github_user.get("followers", 0))

            worker_queue.enqueue(notify_registration,
                                 args=(notification, ),
                                 result_ttl=0)
        except Exception, ex:
            logging.exception(ex)
            sentry_client.captureException()

    return u

    # TODO: Create a task to fetch all the other information..

    # starred = user.get_starred()
    # for s in starred:
    #     print s.full_name, s.watchers

    # pub_events = user.get_public_events()

    # for e in pub_events:
    #     print e.id, e.type, e.repo.full_name

Exemple #15
0
                         (len(post_ids), safe_str(rss_url)))
        else:
            logging.info(u"No Posts saved for %s" % safe_str(rss_url))

        for id in post_ids:
            if stats:
                task('get_post_stats', args=(str(id), )).apply_async()
            task('index_post', args=(str(id), )).apply_async()

    except Exception, ex:
        error_dict = {"rss_url": safe_str(rss_url)}

        logging.warn(u"{0}Exception Occured:{1}{0}".format((20 * "="),
                                                           ex.message))
        try:
            error_reporter.captureException(**error_dict)
        except Exception, error_reporter_exception:
            logging.exception(error_reporter_exception)


def url_hash(url):
    return base64.urlsafe_b64encode(hashlib.md5(safe_str(url)).digest())


def rss_exists(url):
    rss_hash = url_hash(url)
    feed = None

    try:
        feed = session.query(Feed).filter_by(rss_hash=rss_hash).first()
    except: