def setup_recommendations_and_likes(self):
        """This method sets up recommendationss to be tested in 
            other test methods.
        """
        r1 = Recommendation(
            title="Fire Doughnuts in Canada",
            content=
            "Ever tried jerk flavored doughnuts?, then you haven't lived!. Marley's yard gotchu!",
            business_name="Marleys Yard",
            business_address="2345 67 Ave NW",
            business_city="Edmonton",
            business_state="Alberta",
            business_country="Canada",
            business_rating=4,
            user_id=self.testuser_id)

        r2 = Recommendation(
            id=365,
            title="Deep Dish Pizza in Edmonton, Canada",
            content=
            "Looking for delicious deep dish pizza?, Chicago 001 has the best on Whyte Ave",
            business_name="Chicago 001",
            business_address="1946 Whyte Ave NW",
            business_city="Edmonton",
            business_state="Alberta",
            business_country="Canada",
            business_rating=5,
            user_id=self.u2id)

        db.session.add_all([r1, r2])
        db.session.commit()

        l1 = Likes(user_id=self.testuser_id, recommendation_id=365)
        db.session.add(l1)
        db.session.commit()
def recommend(userid, model):
    global model_version
    global canary_version
    if model == 1:
        if userid in predictions.index:
            row = predictions.loc[userid]
            labels = row.nlargest(20).keys().tolist()
            result = ",".join(labels)
            app.logger.info(str(userid) + "->" + result)

            # Store the recommendation in database
            reco = Recommendation(user_id=userid,
                                  movie_ids=labels,
                                  model=model_version,
                                  model_num=1)
            createRecommendation(reco)

            return result
        else:
            app.logger.info("User not indexed. Returning defaults")

            # Store the recommendation in database
            reco = Recommendation(user_id=userid,
                                  movie_ids=getDefaults().split(','),
                                  model=model_version,
                                  model_num=1)
            createRecommendation(reco)

            # Return response
            return getDefaults()

    if model == 2:
        if userid in canary_predictions.index:
            row = predictions.loc[userid]
            labels = row.nlargest(20).keys().tolist()
            result = ",".join(labels)
            app.logger.info(str(userid) + "->" + result)

            # Store the recommendation in database
            reco = CanaryRecommendation(user_id=userid,
                                        movie_ids=labels,
                                        model=canary_version,
                                        model_num=2)
            createCanaryRecommendation(reco)

            return result
        else:
            app.logger.info("User not indexed. Returning defaults")

            # Store the recommendation in database
            reco = CanaryRecommendation(user_id=userid,
                                        movie_ids=getDefaults().split(','),
                                        model=canary_version,
                                        model_num=2)
            createCanaryRecommendation(reco)

            # Return response
            return getDefaults()
Exemple #3
0
    def test_list_recommendations(self):
        """This test method tests to confirm that the logged in user can only
        view recommendations in their state and city."""

        L = Location(name="Home",
                     address="False Test Creek SW, Long Beach CA",
                     long=143.12,
                     lat=-234.5,
                     city="Long Beach",
                     state="CA",
                     user_id=self.testuser_id)
        L_id = 124
        L.id = L_id

        db.session.add(L)
        db.session.commit()

        r1 = Recommendation(
            title="Fire Doughnuts in LA",
            content=
            "Bro these mf doughnuts be smacking like gahdamn!. Check out Leonards bro!",
            business_name="Leonards",
            business_address="2345 Rodeo Ave",
            business_city="Long Beach",
            business_state="CA",
            business_country="US",
            business_rating=5,
            user_id=self.testuser_id)

        r2 = Recommendation(
            id=365,
            title="Deep Dish Pizza in Edmonton, Canada",
            content=
            "Looking for delicious deep dish pizza?, Chicago 001 has the best on Whyte Ave",
            business_name="Chicago 001",
            business_address="1946 Whyte Ave NW",
            business_city="Edmonton",
            business_state="Alberta",
            business_country="Canada",
            business_rating=5,
            user_id=self.u2id)

        db.session.add_all([r1, r2])
        db.session.commit()

        with self.client as c:
            with c.session_transaction() as sess:
                sess[CURR_USER_KEY] = self.testuser_id
                sess[CURR_LOCATION] = L_id

            resp = c.get("/recommendations/list")

            self.assertEqual(resp.status_code, 200)
            self.assertIn("Fire Doughnuts in LA", str(resp.data))
            self.assertNotIn("Deep Dish Pizza in Edmonton, Canada",
                             str(resp.data))
Exemple #4
0
    def test_delete_recommendation(self):
        """This test method tests to confirm that a recommendation is deleted when a delete request is sent by a logged in user."""

        r = Recommendation(
            id=419,
            title="Fire Doughnuts in LA",
            content=
            "Bro these mf doughnuts be smacking like gahdamn!. Check out Leonards bro!",
            business_name="Leonards",
            business_address="2345 Rodeo Ave",
            business_city="Long Beach",
            business_state="CA",
            business_country="US",
            business_rating=5,
            user_id=self.testuser_id)

        db.session.add(r)
        db.session.commit()

        with self.client as c:
            with c.session_transaction() as sess:
                sess[CURR_USER_KEY] = self.testuser_id

            resp = c.post("/recommendations/419/delete", follow_redirects=True)
            self.assertEqual(resp.status_code, 200)

            r = Recommendation.query.get(419)
            self.assertIsNone(r)
    def test_like_recommendation(self):
        """This test method tests to confirm that the 
            like recommendation functionality works.
        """
        r = Recommendation(
            id=3000,
            title="Jamaican Doughnuts in Canada",
            content="Jamaican Doughnuts are the bomb, got to Marley's on 67th!",
            business_name="Marleys Yard",
            business_address="2345 67 Ave NW",
            business_city="Edmonton",
            business_state="Alberta",
            business_country="Canada",
            business_rating=5,
            user_id=self.u2id)

        db.session.add(r)
        db.session.commit()

        with self.client as c:
            with c.session_transaction() as sess:
                sess[CURR_USER_KEY] = self.testuser_id

            resp = c.post("/recommendations/3000/like", follow_redirects=True)
            self.assertEqual(resp.status_code, 200)

            likes = Likes.query.filter(Likes.recommendation_id == 3000).all()
            self.assertEqual(len(likes), 1)
            self.assertEqual(likes[0].user_id, self.testuser_id)
Exemple #6
0
def add_recommendation():
    """This view function renders the form to add a new recommendation
       if a GET request is made. 
       If form validates, it adds new recommendation to dateMeet db and redirect to recommendations page.
    """

    if not g.user:
        flash("Access unauthorized.", "danger")
        return redirect("/")

    form = UserRecommendationAddForm()

    if form.validate_on_submit():
        recommendation = Recommendation(
            title=form.title.data,
            content=form.content.data,
            business_name=form.business_name.data,
            business_address=form.business_address.data,
            business_city=form.business_city.data,
            business_state=form.business_state.data,
            business_country=form.business_country.data,
            business_rating=form.business_rating.data)
        g.user.recommendations.append(recommendation)
        db.session.commit()

        return redirect(f"/users/{g.user.id}")

    return render_template('recommendations/new.html', form=form)
    def test_recommendation_likes(self):
        """This test method tests recommendation likes model"""

        r1 = Recommendation(
            title="Fire Doughnuts in Canada",
            content=
            "Ever tried jerk flavored doughnuts?, then you haven't lived!. Marley's yard gotchu!",
            business_name="Marleys Yard",
            business_address="2345 67 Ave NW",
            business_city="Edmonton",
            business_state="Alberta",
            business_country="Canada",
            business_rating=4,
            user_id=self.uid)

        u = User.register("User2", "Test", "*****@*****.**", "testuser2",
                          "passWord4u2", None, None)
        uid2 = 2222
        u.id = uid2

        db.session.add(r1)
        db.session.commit()

        u.likes.append(r1)

        db.session.commit()

        l = Likes.query.filter(Likes.user_id == uid2).all()

        # There should be only one liked message and it should be m1

        self.assertEqual(len(l), 1)
        self.assertEqual(l[0].recommendation_id, r1.id)
Exemple #8
0
def do_import_reco(file_path) -> Recommendation:
    reco_dataframe = pd.read_csv(file_path)
    total_reco = new_reco = 0

    for row in reco_dataframe.itertuples():
        if str(row.benefit) == 'nan':
            continue
        reco = Recommendation.query.get(row.id)
        if not reco:
            reco = Recommendation()
            reco.id = row.id
            new_reco += 1

        reco.title = row.title
        if str(row.content) != 'nan':
            reco.content = row.content
        # TODO: temporary with set a default value
        else:
            reco.content = "Contenu non défini"
        if str(row.benefit) != 'nan':
            reco.benefit = row.benefit
        else:
            reco.benefit = 0.0
        if str(row.benefit_description) != 'nan':
            reco.benefit_description = row.benefit_description
        if str(row.how_to) != 'nan':
            reco.how_to = row.how_to
        reco.type = FootprintType({'label': row.category})
        total_reco += 1
        BaseObject.check_and_save(reco)

    print('Recommendations updated : %s' % str(total_reco - new_reco))
    print('Recommendations created : %s' % new_reco)
def recommend_song():
    data = request.get_json()

    if not data['to_user']:
        return jsonify({"msg": "Missing to_user parameter"}), 400
    if not data['from_user']:
        return jsonify({"msg": "Missing from_user parameter"}), 400
    if not data['song_id']:
        return jsonify({"msg": "Missing song_id parameter"}), 400

    to_user = data['to_user']
    from_user = data['from_user']
    song_id = data['song_id']
    message = data['message']

    try:
        new_recommendation = Recommendation(to_user=to_user,
                                            from_user=from_user,
                                            song_id=song_id,
                                            message=message)
        db.session.add(new_recommendation)
        db.session.commit()
        return jsonify({"msg": "Recommendation sent!"}), 201
    except Exception as e:
        print(e)
        return jsonify({"msg": "Something went wrong making the rec."}), 400
 def _format_recommendation_response(
     response: dict, ) -> List[Recommendation]:
     if not response or not response.get('tracks'):
         raise NoContentFound(
             f'Invalid or missing content in response {response}')
     formatted_data = []
     recommendations = response['tracks']
     for recommendation in recommendations:
         formatted_data.append(
             Recommendation(
                 **{
                     'playlist':
                     recommendation['album']['name'],
                     'url':
                     recommendation['external_urls']['spotify'],
                     'artists': [
                         artist['name']
                         for artist in recommendation['artists']
                     ],
                     'total_tracks':
                     round(recommendation['album']['total_tracks']),
                     'duration_seconds':
                     round(recommendation['duration_ms'] / 60, 2),
                 }))
     return formatted_data
    def test_recommend_add_remove(self):

        new_user = User(
            username='******',
            first_name='rec',
            last_name='user',
            password='******',
            email='*****@*****.**',
        )
        new_user.id = 11114
        db.session.add(new_user)

        new_rec = Recommendation(recommender_id=11114,
                                 recommend_to_user_id=11111,
                                 drink_id=1)
        new_rec.id = 10000
        db.session.add(new_rec)

        rec = Recommendation.query.get(10000)

        self.assertEqual(rec.id, 10000)
        self.assertEqual(rec.recommender_id, 11114)
        self.assertEqual(rec.recommend_to_user_id, 11111)
        self.assertEqual(rec.drink_id, 1)

        db.session.delete(rec)
        db.session.commit()

        check_rec = Recommendation.query.get(10000)

        self.assertEqual(check_rec, None)
Exemple #12
0
def _create_recommendation(user, offer, mediation=None):
    recommendation = Recommendation()
    recommendation.user = user

    if offer:
        recommendation.offer = offer
    else:
        offer = offer_queries.find_offer_by_id(mediation.offerId)
        recommendation.offer = offer

    if mediation:
        recommendation.mediation = mediation
    else:
        mediation = Mediation.query \
            .filter(Mediation.offer == offer) \
            .filter(Mediation.isActive) \
            .order_by(func.random()) \
            .first()
        recommendation.mediation = mediation

    if recommendation.mediation:
        recommendation.validUntilDate = datetime.utcnow() + timedelta(days=3)
    else:
        recommendation.validUntilDate = datetime.utcnow() + timedelta(days=1)

    if offer.lastStock and offer.lastStock.bookingLimitDatetime:
        recommendation.validUntilDate = min(
            recommendation.validUntilDate,
            offer.lastStock.bookingLimitDatetime - timedelta(minutes=1))

    PcObject.save(recommendation)
    return recommendation
    def test_delete_recommend_post(self):
        with self.client as client:
            with client.session_transaction() as sess:
                sess[KEY]=999

            new_user = User(
                id=1002,
                username='******',
                first_name='test',
                last_name='user3',
                password='******',
                email='*****@*****.**',
            )

            db.session.add(new_user)

            new_recommend = Recommendation(recommender_id=1002, recommend_to_user_id=999, drink_id='1')
            rec_id = 9999
            new_recommend.id = rec_id
            db.session.add(new_recommend)

            resp = client.post('/user/recommendations', json={'recId': 9999}, follow_redirects=True)

            self.assertIn("You don\\\'t have any recommendations yet", str(resp.data))
            
            del_new_user = User.query.get(1002)
            db.session.delete(del_new_user)
Exemple #14
0
def make_blank_recommendations(user):
    firstrec = Recommendation(
        student_id=user.user_id,
        recommender_id=User.query.filter_by(email=user.rec1email,
                                            role=2).first().user_id)
    secondrec = Recommendation(
        student_id=user.user_id,
        recommender_id=User.query.filter_by(email=user.rec2email,
                                            role=2).first().user_id)
    thirdrec = Recommendation(
        student_id=user.user_id,
        recommender_id=User.query.filter_by(email=user.rec3email,
                                            role=2).first().user_id)
    db.session.add(firstrec)
    db.session.add(secondrec)
    db.session.add(thirdrec)
    db.session.commit()
Exemple #15
0
def create_recommendations(recommendation_id):
    """ Create Recommendation View """
    data = request.get_json()
    rec = Recommendation()
    rec.deserialize(data)
    rec.save()
    message = rec.serialize()

    return render_template('manage.html', name="Manage",
                           result=message), status.HTTP_201_CREATED
def generate_recommendations():
    original = get_user_track_array()
    original_pd = pd.DataFrame(original)
    song_to_song = pd.DataFrame(index=original_pd.columns,
                                columns=original_pd.columns)

    for i in xrange(len(song_to_song.columns)):
        for j in xrange(len(song_to_song.columns)):
            similarity = 1 - cosine(original_pd.ix[:, i], original_pd.ix[:, j])
            song_to_song.ix[i, j] = similarity
            if math.isnan(similarity):
                song_to_song.ix[i, j] = 0

    data_neighbours = pd.DataFrame(index=song_to_song.columns,
                                   columns=[range(1, 11)])

    for i in range(0, len(song_to_song.columns)):
        data_neighbours.ix[i, :10] = song_to_song.ix[0:, i].sort_values(
            ascending=False)[:10].index

    data_sims = pd.DataFrame(index=original_pd.index,
                             columns=original_pd.columns)

    for i in range(0, len(data_sims.index)):
        for j in range(0, len(data_sims.columns)):
            user = data_sims.index[i]
            product = data_sims.columns[j]

            if original_pd.ix[i][j] == 1:
                data_sims.ix[i][j] = 0
            else:
                product_top_names = data_neighbours.ix[product][1:10]
                product_top_sims = song_to_song.ix[product].sort_values(
                    ascending=False)[1:10]
                user_purchases = original_pd.ix[user, product_top_names]

                data_sims.ix[i][j] = getScore(user_purchases, product_top_sims)

    result_dict = {}

    for i in xrange(len(data_sims.index)):
        top_matches = data_sims.ix[i, :].sort_values(
            ascending=False)[0:10].index.values
        result_dict[i + 1] = [x + 1 for x in top_matches]

    for user_id, track_ids in result_dict.items():
        recommendations = []
        for track_id in track_ids:
            recommendations.append(
                Recommendation(user_id=user_id, track_id=track_id))
        db.session.add_all(recommendations)
        db.session.commit()
def recommendations():
    return [
        Recommendation(
            **{
                'playlist': 'album name',
                'url': 'https://open.spotify.com/track/7GhIk7Il098yCjg4BQjzvb',
                'artists': ['artist_1', 'artist_2'],
                'total_tracks': 11,
                'duration_seconds': round(11700 / 60, 2),
            }
        )
        for _ in range(2)
    ]
Exemple #18
0
def create_tuto_mediation_if_non_existent_for_user(user, tuto_mediation):

    already_existing_tuto_recommendation = Recommendation.query\
        .filter_by(mediation=tuto_mediation, user=user)\
        .first()
    if already_existing_tuto_recommendation:
        return

    recommendation = Recommendation()
    recommendation.user = user
    recommendation.mediation = tuto_mediation
    recommendation.validUntilDate = datetime.utcnow() + timedelta(weeks=2)
    PcObject.save(recommendation)
Exemple #19
0
def create_recommendation(offer=None, user=None, mediation=None, idx=None, date_read=None,
                          valid_until_date=datetime.utcnow() + timedelta(days=7), search=None,
                          is_clicked=False):
    recommendation = Recommendation()
    recommendation.id = idx
    recommendation.offer = offer
    recommendation.user = user
    recommendation.mediation = mediation
    recommendation.dateRead = date_read
    recommendation.validUntilDate = valid_until_date
    recommendation.search = search
    recommendation.isClicked = is_clicked
    return recommendation
Exemple #20
0
    def test_unauthorized_recommendation_delete(self):
        """This test method confirms that an unauthorized user 
           cannot delete a recommendation.
        """

        # We create a separate user first
        u = User.register(first_name="Userrrr",
                          last_name="Test",
                          email="*****@*****.**",
                          username="******",
                          password="******",
                          image_url=None,
                          header_url=None)

        u.id = 589

        # Now we create a recommendation made by testuser

        r = Recommendation(
            id=419,
            title="Fire Doughnuts in LA",
            content=
            "Bro these mf doughnuts be smacking like gahdamn!. Check out Leonards bro!",
            business_name="Leonards",
            business_address="2345 Rodeo Ave",
            business_city="Long Beach",
            business_state="CA",
            business_country="US",
            business_rating=5,
            user_id=self.testuser_id)

        db.session.add_all([u, r])
        db.session.commit()

        with self.client as c:
            with c.session_transaction() as sess:
                sess[CURR_USER_KEY] = 589

            resp = c.post("/recommendations/419/delete", follow_redirects=True)
            self.assertEqual(resp.status_code, 200)
            self.assertIn("Access unauthorized.", str(resp.data))

            r = Recommendation.query.get(419)
            self.assertIsNotNone(r)
Exemple #21
0
    def execute(self, user: User, data) -> Recommendation:
        if user is None:
            raise BadUserException()

        if data is None:
            raise BadArgException

        recommendation = Recommendation()
        recommendation.user_id = user.id
        recommendation.title = data.get('title')
        recommendation.content = data.get('content')
        recommendation.estimated_success_time = data.get(
            'estimated_success_time')
        recommendation.difficulty_level = data.get('difficulty_level')
        recommendation.benefit = data.get('benefit')
        recommendation.type = data.get('type')

        BaseObject.check_and_save(recommendation)

        return recommendation
Exemple #22
0
def recommend_form(drink_id):
    """Show form for posting recommendations, creates new recommendations from POST"""

    if 'user' not in session:
        flash('Please log in or sign up to send recommendations', 'danger')
        return redirect('/')

    form = RecommendForm()

    user_id = session['user']
    user = User.query.get(user_id)
    drink = Drink.query.get(drink_id)

    form = RecommendForm()
    form.recommend_to_name.choices = [
        (user.id, user.username)
        for user in User.query.filter(User.id != user_id).all()
    ]
    form.username.data = user.username
    form.drink.data = drink.name

    if request.method == 'POST':

        username = form.username.data
        recommend_to_id = form.recommend_to_name.data
        drink = form.drink.data

        user = User.query.filter(User.username == username).first()
        drink = Drink.query.filter(Drink.name == drink).first()

        new_recommend = Recommendation(recommender_id=user.id,
                                       recommend_to_user_id=recommend_to_id,
                                       drink_id=drink.id)
        db.session.add(new_recommend)
        db.session.commit()

        return redirect('/user/favorites')

    else:

        return render_template('form.html', form=form, drink_id=drink_id)
Exemple #23
0
    def post(self):
        """ Creates a Recommendation

        This endpoint will create a Recommendations based the data in the body that is posted.
        We assume when a user ask for a recommendation they will provide a Product Id
        And a Type in the following format:
            { 'product_id': <int>, 'type': '<[up-sell|accessory|cross-sell]>' }
        """

        #data = request.get_json()
        rec = Recommendation()
        rec.deserialize(api.payload)
        rec.save()

        location_url = api.url_for(RecommendationResource,
                                   recommendation_id=rec.id,
                                   _external=True)

        return rec.serialize(), status.HTTP_201_CREATED, {
            'Location': location_url
        }
    def test_recommendation_model(self):
        """Does the basic recommendation model work?"""

        r = Recommendation(
            title="The best burger in the world",
            content=
            "The best burger in the entire world definitely is Marcos Famous DT Edmonton",
            business_name="Marcos Famous",
            business_address="13425 36 Ave NW",
            business_city="Edmonton",
            business_state="Alberta",
            business_country="Canada",
            business_rating=4,
            user_id=self.uid)

        db.session.add(r)
        db.session.commit()

        # User should have 1 message
        self.assertEqual(self.user.recommendations[0].business_country,
                         "Canada")
        self.assertEqual(self.user.recommendations[0].business_rating, 4)
        self.assertEqual(len(self.user.recommendations), 1)
    def test_one_recommend_recommends_page(self):
        with self.client as client:
            with client.session_transaction() as sess:
                sess[KEY]=999
            
            new_user = User(username='******', first_name='test2', last_name='user', email='*****@*****.**', password='******')
            new_id = 1001
            new_user.id = new_id
            db.session.add(new_user)

            new_recommend = Recommendation(recommender_id=new_user.id, recommend_to_user_id=999, drink_id='1')
            db.session.add(new_recommend)

            resp = client.get('/user/recommendations')
            
            self.assertIn('<table ', str(resp.data))
            self.assertIn('<thead>', str(resp.data))
            self.assertIn('Name</th>', str(resp.data))
            self.assertIn('Recommended by</th>', str(resp.data))
            self.assertIn('<tbody>', str(resp.data))
            self.assertIn('1</td>', str(resp.data))
            self.assertIn('<a href="/drinks/1">A1</a>', str(resp.data))
            self.assertIn('test2</td>', str(resp.data))
            self.assertIn('Remove</button>', str(resp.data))
Exemple #26
0
def add_like(username, movie_id):
    """Handle Likes"""
    find_fav(movie_id)
    favorites = find_fav(movie_id)

    #update the form data
    id = favorites['ID']
    title_id = favorites['Title']
    actors = favorites['Actors']
    plot = favorites['Plot']
    username = favorites['Username']

    #Movie Model Blueprint
    movie = Recommendation(id=(id),
                           title_id=(title_id),
                           actors=(actors),
                           plot=(plot),
                           username=(username))

    db.session.add(movie)

    db.session.commit()

    return redirect(f"/users/{username}")
Exemple #27
0
def recommendations(id_subject):
    subject = Subject.query.filter_by(id=id_subject).first_or_404()

    # recommen = subject.recommendations if subject.recommendations else []
    # print(subject.recommendations)

    form = RecommendationsForm(request.form, id=176, party_ids=[])

    if form.validate_on_submit():
        for r in form.party_ids.data:
            print(r.id)
            rc = Recommendation(
                recommendation=True,
                party_id=r.id,
                subject_id=id_subject,
            )
            db.session.add(rc)
            db.session.commit()

        return redirect(url_for('subjects.view', id_subject=id_subject))

        # print(form.recommendations.data)

    return render_template('subjects/recommendations.html', form=form)
Exemple #28
0
    def scrape_reco_list(self,
                         tab_button,
                         wait_for_recommendations_load_seconds=1):
        recommendations = Recommendations()
        # recommendations_element = self.browser.find_element_by_class_name('pv-recommendations-section')

        # button text tells how many recommendations there are, e.g. 'Received (0)':
        num_recommendations = tab_button.text.strip()
        print(f"   searching for {num_recommendations} recommendations")
        if num_recommendations.endswith('(0)'): return recommendations

        id_to_scrape = tab_button.get_attribute('aria-controls')
        controlled_div_element = self.browser.find_element_by_id(id_to_scrape)

        # Click Show more if the button exists
        # If there are more than 2 recommendations there will be a third button "Show more"
        # <button class="pv-profile-section__see-more-inline pv-profile-section__text-truncate-toggle link link-without-hover-state"
        # aria-controls="recommendation-list" aria-expanded="false" type="button">Show more
        showed_more = False  # used to determine whether to click "show less" button
        while True:
            try:
                more_inline_button = controlled_div_element.find_element_by_class_name(
                    'pv-profile-section__see-more-inline')
                ActionChains(self.browser).move_to_element(
                    more_inline_button).click(more_inline_button).perform()
                showed_more = True
                time.sleep(wait_for_recommendations_load_seconds)
            except NoSuchElementException as e:
                break  # keep clicking until the button is not found

        # recommendations are a series of <li class="pv-recommendation-entity"> elements found underneath the div controlled by the button
        print(
            f"   found {len(controlled_div_element.find_elements_by_class_name('pv-recommendation-entity'))} recommendations"
        )
        for rec_element in controlled_div_element.find_elements_by_class_name(
                'pv-recommendation-entity'):
            recommendation = Recommendation()

            # <a data-control-name="recommendation_details_profile" href=linkedin_profile_url_of_recommender
            a_tag = rec_element.find_element_by_tag_name('a')
            recommendation.linkedin_url = a_tag.get_attribute('href')

            # <div class="pv-recommendation-entity__detail">
            detail_element = a_tag.find_element_by_class_name(
                'pv-recommendation-entity__detail')

            #      <h3 class="t-16 t-black t-bold">Mike P Lewis</h3>
            recommendation.name = detail_element.find_element_by_tag_name(
                'h3').text.strip()

            p_tags = detail_element.find_elements_by_tag_name('p')
            #      <p class="pv-recommendation-entity__headline t-14 t-black t-normal pb1">CEO &amp; Co-founder of Onward</p>
            recommendation.title_co = p_tags[0].text.strip()

            #      <p class="t-12 t-black--light t-normal"> November 5, 2013, Mike P managed Jennifer directly </p>
            #                  OR 'reported directly to Jennfier'
            rec_date_relationship = p_tags[1].text.strip()
            parts = rec_date_relationship.split(',')
            recommendation.date = ','.join(parts[0:2]).strip()
            recommendation.relationship = ''.join(parts[2:]).strip()

            if not recommendation.name:
                raise ValueError(f"failed to scrape recommendation name")
            recommendations.add(recommendation)

        # if showed_more:
        #     print(f"   CLICKING show less button")
        #     controlled_div_element.find_element_by_class_name('pv-profile-section__see-less-inline').click()
        #     time.sleep(wait_for_recommendations_load_seconds)

        return recommendations
Exemple #29
0
def edit(request):
    # TODO: Clean up this messy method.

    # Require login.
    if not (request.user.is_authenticated()):
        return redirect_to_login(request.get_full_path())

    recs = Recommendation.objects.filter(user=request.user)
    category_types = CategoryType.objects.all()
    all_categories = Category.objects.all()
    context = {
        'recs': recs,
        'category_types': category_types,
        'cat_check_htmls': []
    }
    books_in_c = {}
    for c in all_categories:
        books_in_c[c] = [r for r in recs.filter(book__category=c) \
                                        .distinct()]
    print >> sys.stderr, repr(books_in_c)
    for rec in recs:
        b = ''
        for ct in category_types:
            b += '<p style="font-weight: bold">%s</p>' % ct.description
            for c in ct.get_categories():
                b += '<input type="checkbox" name="{0}" id="{1}{0}"'.format(
                    c.slug, rec.id)
                if rec in books_in_c[c]:
                    b += 'checked="checked" '
                b += '/><label for="%d%s">%s</label><br />' % (rec.id, c.slug,
                                                               c.name)
        context['cat_check_htmls'].append(b)

    if 'keywords' in request.GET:
        context['results'] = gbooks.search(request.GET['keywords'])
    elif 'gid' in request.POST:
        if 'action' in request.POST:
            b = Book.objects.get(gid=request.POST['gid'])
            r = Recommendation.objects.get(user=request.user, book=b)
            if request.POST['action'] == 'update':
                for c in Category.objects.all():
                    if c.slug in request.POST:
                        c.books.add(b)
                    else:
                        c.books.remove(b)
                r.comment = request.POST['blurb']
                r.save()
                print >> sys.stderr, repr(request.POST)
            elif request.POST['action'] == 'delete':
                if len(Recommendation.objects.filter(book=r.book)) == 1:
                    os.unlink(
                        os.path.join('/opt/infxbooklist/bookcovers/',
                                     r.book.cover_image))
                    r.book.delete()
                r.delete()
        else:
            # Make the book if doesn't exist, update if does
            gb = gbooks.get(request.POST['gid'])
            b = Book.objects.get_or_create(gid=gb.gid)[0]
            b.title = gb.title
            b.authors = gb.authors
            b.isbn = gb.isbn
            try:
                # Download the thumbnail image from Google
                req = urllib2.Request(gb.thumbnail_url)
                req.add_header("User-Agent", "Mozilla")
                try:
                    # TODO: These images are never deleted. Write a cron script to
                    #       scrub the dir of images that tables no longer reference.
                    image_link = urllib2.urlopen(req)
                    img_data = image_link.read()
                    image_link.close()
                    rand_fn = datetime.datetime.utcnow().isoformat(
                    ) + '__' + str(random.randint(0, sys.maxint))
                    rand_pth = os.path.join('/opt/infxbooklist/bookcovers',
                                            rand_fn)
                    with open(rand_pth, 'w') as f:
                        f.write(img_data)
                    b.cover_image = rand_fn
                except Exception, e:
                    print >> sys.stderr, "Tried to save thumbnail, but got exception:", repr(
                        e)
            finally:
                b.save()
            # Create a Recommendation, which links User and Book
            Recommendation(user=request.user, book=b).save()
        # Redirect to avoid refresh issues
        return HttpResponseRedirect("/edit/")
    # Go.
    return render_to_response('edit.html', context)