Beispiel #1
0
def stats(stream_encoded):
    values = None
    try:
        values = hash_gen.decrypt(str(stream_encoded)) #values stores user_id, stream_id
    except TypeError:
        app.logger.error("Error in hashids.decrypt! {}".format(stream_encoded))
    if values:
        user_id, stream_id = values
    else:
        user_id, stream_id = None, -1
    stream = Stream.query.filter_by(id=stream_id).first_or_404()
    if session.get('digestif') is None:
        return redirect(url_for("stats_auth", service=stream.service))
    oauth_token = session.get("digestif")["a"]
    if oauth_token != stream.oauth_token:
        # we'll 404 for now but should have better logic
        # it is possible the token we have stored is invalid and the
        # user may need to reauthorize
        other_stream = Stream.query.filter_by(oauth_token=oauth_token).first_or_404()
        stream_encoded=hash_gen.encrypt(other_stream.user_id, other_stream.id)
        return redirect(url_for("stats", stream_encoded=stream_encoded))
    if stream.user_id != user_id:
        app.logger.error("Mismatching user and stream! {}, {}".format(stream.user_id, user_id))
        return "Mismatching user and stream!"
    owner = stream.user.email
    subscribers = Subscription.query.filter_by(stream_id=stream.id).filter(Subscription.frequency != 0).all()
    digest_counts = {}
    for subscriber in subscribers:
        digest_counts[subscriber] = Digest.query.filter_by(subscription_id=subscriber.id).count()
    return render_template("stats.html", stream=stream, owner=owner, subscribers=subscribers, digest_counts=digest_counts)
Beispiel #2
0
def send_digest(digest, env):
    digest_encoded = hash_gen.encrypt(digest.id)
    subscription = digest.subscription
    stream = subscription.stream
    if stream.service == FLICKR:
        entries = FlickrPhoto.query.filter(FlickrPhoto.date_uploaded > digest.start_date,
                                           FlickrPhoto.date_uploaded <= digest.end_date,
                                           FlickrPhoto.stream_id == stream.id).order_by(FlickrPhoto.date_taken).all()
    elif stream.service == INSTAGRAM:
        entries = InstagramPhoto.query.filter(InstagramPhoto.date_uploaded > digest.start_date,
                                              InstagramPhoto.date_uploaded <= digest.end_date,
                                              InstagramPhoto.stream_id == stream.id).order_by(InstagramPhoto.date_taken).all()
    else:
        entries = None
    user = subscription.user
    template = env.get_template("email_digest.html")
    html = template.render(entries=entries, stream=stream, digest_encoded=digest_encoded, digest=digest, email=True, max_images=10)
    html_email = premailer.transform(html, base_url="http://digestif.me")
    title = "A new photo digest from {}".format(metadata(stream))
    text_email =  "Digestif\n\nYou have a new digest of photographs to view from {}. View this email as HTML or visit http://digestif.me/digest/{}\n\nWant to change the delivery rate? Adjust your subscription at http://digestif.me{}\n\n Digestif converts your photostream into an email digest. Your friends and family subscribe and decide how frequently they want digests delivered. That way, when you post new photographs your friends and family are notified on their terms.".format(metadata(stream), digest_encoded, stream.subscribe_url())
    category = "digest:stream:{}".format(stream.id)
    if sendgrid_send(user.email, title, text_email, html_email, categories=category):
        digest.delivered = True
        app.logger.info("Digest delivered to {}".format(user.email))
        db.session.commit()
Beispiel #3
0
def stats_auth():
    service = request.args.get("service", None)
    if service == str(FLICKR):
        return flickr_oauth.authorize(callback=url_for('handle_flickr_authorization', stats="1"), perms="read")
    elif service == str(INSTAGRAM):
        return instagram_oauth.authorize(callback="http://digestif.me"+url_for('handle_instagram_authorization', stats="1"), scope="basic")
    elif session.get('digestif'):
        oauth_token = session.get("digestif")["a"]
        other_stream = Stream.query.filter_by(oauth_token=oauth_token).first_or_404()
        stream_encoded = hash_gen.encrypt(other_stream.user_id, other_stream.id)
        return redirect(url_for("stats", stream_encoded=stream_encoded))
    else:
        return render_template("stats_login.html")
Beispiel #4
0
def handle_instagram_authorization(resp):
    if resp is None:
        return redirect(url_for("landing"))
    instagram_user = resp.get("user")
    instagram_id = instagram_user["id"]
    access_token = resp.get("access_token")
    if not instagram_user or not access_token:
        app.logger.warning("no instagram auth {}, {}".format(instagram_user, access_token))
        return redirect(url_for("landing"))
    session["digestif"] = {"a" : access_token }
    # email means we create a new account
    email = request.args.get("email")
    if email:
        stream = build_user_stream(email, instagram_id, INSTAGRAM, access_token, "")
        return redirect(stream.subscribe_url())
    # stats means we redirect to statistics page
    stats = request.args.get("stats")
    if stats:
        stream = Stream.query.filter_by(foreign_key=instagram_id).first()
        if stream:
            return redirect(url_for("stats", stream_encoded=hash_gen.encrypt(stream.user_id, stream.id)))
    return redirect(url_for("landing"))
Beispiel #5
0
def send_digest(digest, env):
    digest_encoded = hash_gen.encrypt(digest.id)
    subscription = digest.subscription
    stream = subscription.stream
    if stream.service == FLICKR:
        entries = FlickrPhoto.query.filter(
            FlickrPhoto.date_uploaded > digest.start_date,
            FlickrPhoto.date_uploaded <= digest.end_date,
            FlickrPhoto.stream_id == stream.id).order_by(
                FlickrPhoto.date_taken).all()
    elif stream.service == INSTAGRAM:
        entries = InstagramPhoto.query.filter(
            InstagramPhoto.date_uploaded > digest.start_date,
            InstagramPhoto.date_uploaded <= digest.end_date,
            InstagramPhoto.stream_id == stream.id).order_by(
                InstagramPhoto.date_taken).all()
    else:
        entries = None
    user = subscription.user
    template = env.get_template("email_digest.html")
    html = template.render(entries=entries,
                           stream=stream,
                           digest_encoded=digest_encoded,
                           digest=digest,
                           email=True,
                           max_images=10)
    html_email = premailer.transform(html, base_url="http://digestif.me")
    title = "A new photo digest from {}".format(metadata(stream))
    text_email = "Digestif\n\nYou have a new digest of photographs to view from {}. View this email as HTML or visit http://digestif.me/digest/{}\n\nWant to change the delivery rate? Adjust your subscription at http://digestif.me{}\n\n Digestif converts your photostream into an email digest. Your friends and family subscribe and decide how frequently they want digests delivered. That way, when you post new photographs your friends and family are notified on their terms.".format(
        metadata(stream), digest_encoded, stream.subscribe_url())
    category = "digest:stream:{}".format(stream.id)
    if sendgrid_send(user.email,
                     title,
                     text_email,
                     html_email,
                     categories=category):
        digest.delivered = True
        app.logger.info("Digest delivered to {}".format(user.email))
        db.session.commit()
Beispiel #6
0
def handle_flickr_authorization(resp):
    if resp is None:
        return redirect(url_for("landing"))
    if isinstance(resp, OAuthException):
        app.logger.error("OAuthException: {}, {}".format(resp, resp.data))
        abort(502)
    flickr_id = resp.get("user_nsid")
    oauth_token_secret = resp.get("oauth_token_secret")
    oauth_token = resp.get("oauth_token")
    if not flickr_id or not oauth_token_secret or not oauth_token:
        app.logger.warning("no flickr auth {}, {}, {}".format(flickr_id, oauth_token, oauth_token_secret))
        return redirect(url_for("landing"))
    session["digestif"] = {"a" : oauth_token }
    email = request.args.get("email")
    if email:
        stream = build_user_stream(email, flickr_id, FLICKR, oauth_token, oauth_token_secret)
        return redirect(stream.subscribe_url())
    # stats means we redirect to statistics page
    stats = request.args.get("stats")
    if stats:
        stream = Stream.query.filter_by(foreign_key=flickr_id).first()
        if stream:
            return redirect(url_for("stats", stream_encoded=hash_gen.encrypt(stream.user_id, stream.id)))
    return redirect(url_for("landing"))
Beispiel #7
0
 def subscribe_url(self):
     encoded = hash_gen.encrypt(self.user_id, self.id)
     return 'subscribe', {'stream_encoded': encoded}