Ejemplo n.º 1
0
class HomePage(webapp.RequestHandler):
    def get(self):
        """Displays the setup page for logged-in users, and home page for others."""
        self.cookie = LilCookies(self, config.cookie_secret)
        id = self.cookie.get_secure_cookie("username")
        if not id:
            self.response.out.write(template.render("template/home.html", locals()))
        else:
            user = User.get_by_key_name(id)
            emails = Email.all().filter("username ="******"template/setup.html", locals()))

    def post(self):
        """Adds or deletes e-mail addresses for a logged-in user"""
        self.cookie = LilCookies(self, config.cookie_secret)
        id = self.cookie.get_secure_cookie("username")
        email = self.request.get("email").lower()
        delete = self.request.get("delete").lower()
        if id:
            if email:
                mapping = Email.get_by_key_name(email) or Email(key_name=email)
                mapping.username = self.cookie.get_secure_cookie("username")
                mapping.subscribe = datetime.datetime.utcnow().hour
                mapping.put()
            elif delete:
                mapping = Email.get_by_key_name(delete)
                if mapping:
                    mapping.delete()

        self.redirect("/")
Ejemplo n.º 2
0
  def get(self):

    cookies = LilCookies(self, secrets.cookie_secret)
    username = cookies.get_secure_cookie(cfg.COOKIE_USERNAME)

    if not username:
      self.error(404)
      self.response.out.write(not_found.geterr())
      return

    d = model.MarkovUserV1.get_by_key_name(username)
    if not d:
      self.error(404)
      self.response.out.write(not_found.geterr())
      return

    if d.generated is None:
      self.renderTemplate(cfg.NOTHING_TO_POST_TEMPLATE)
      return

    auth = OAuth(d.userToken, d.userSecret, consumer_key, secrets.consumer_secret)
    twitter = Twitter(auth=auth)

    result = setStatus(twitter, d.generated)
    #import example_response
    #result = example_response.response

    self.renderTemplate(cfg.POST_TEMPLATE, TIME=result['created_at'], USER=username, ID=result['id'], POST=d.generated)

    d.generated = None
    d.put()
Ejemplo n.º 3
0
class TwitterAuth(webapp.RequestHandler):
    """Handles Twitter OAuth. See oauth.py for more documentation"""

    def get(self):
        self.cookie = LilCookies(self, config.cookie_secret)
        id = self.cookie.get_secure_cookie("username")

        if self.request.get("oauth_token"):
            auth_token = self.request.get("oauth_token")
            auth_verifier = self.request.get("oauth_verifier")
            user_info = client.get_user_info(auth_token, auth_verifier=auth_verifier)

            # Create the user in the database
            if not id:
                id = user_info["username"]
                user = User.all().filter("user ="******"tw" + id)
            else:
                user = User.get_by_key_name(id)
            user.user = user_info["username"]
            user.name = user_info["name"]
            user.picture = user_info["picture"]
            user.token = user_info["token"]
            user.secret = user_info["secret"]
            user.put()

            self.cookie.set_secure_cookie("username", user.key().name())
            self.redirect("/")

        else:
            self.redirect(client.get_authorization_url())
Ejemplo n.º 4
0
class FacebookAuth(webapp.RequestHandler):
    """Handles Facebook OAuth. See http://developers.facebook.com/docs/api"""

    def get(self):
        self.cookie = LilCookies(self, config.cookie_secret)
        id = self.cookie.get_secure_cookie("username")

        if self.request.get("code"):
            # Get the access token in exchange for the code
            url = "https://graph.facebook.com/oauth/access_token?" + urllib.urlencode(
                {
                    "client_id": config.facebook_params["app_id"],
                    "client_secret": config.facebook_params["app_secret"],
                    "redirect_uri": self.request.url.split("?")[0],
                    "code": self.request.get("code"),
                }
            )
            out = urlfetch.fetch(url)
            if out.status_code != 200:
                self.response.out.write(out.content)
                return

            data = cgi.parse_qs(out.content)
            token = data["access_token"][0]

            # Get the user profile data
            out = urlfetch.fetch("https://graph.facebook.com/me?access_token=" + token)
            if out.status_code != 200:
                self.response.out.write(out.content)
                return

            # Create the user in the database
            user_info = json.loads(out.content)
            if not id:
                id = user_info["id"]
                user = User.all().filter("fb_id =", id).get() or User(key_name="fb:" + id)
            else:
                user = User.get_by_key_name(id)
            user.name = user_info["name"]
            user.fb_id = user_info["id"]
            user.fb_token = token
            user.put()

            self.cookie.set_secure_cookie("username", user.key().name())
            self.redirect("/")

        else:
            url = "https://graph.facebook.com/oauth/authorize?" + urllib.urlencode(
                {
                    "client_id": config.facebook_params["app_id"],
                    "redirect_uri": self.request.url.split("?")[0],
                    "scope": "publish_stream,offline_access,read_stream",
                }
            )
            self.redirect(url)
Ejemplo n.º 5
0
    def get(self):
        cookieutil = LilCookies(self, settings.COOKIE_SECRET)
        ig_user_id = cookieutil.get_secure_cookie(name="ig_user_id")

        profiles = Profile.all()
        profiles.filter("ig_user_id =", ig_user_id)
        profile = profiles.get()

        if profile:
            profile.delete()

        self.redirect("/")
Ejemplo n.º 6
0
    def get(self):
        cookieutil = LilCookies(self, settings.COOKIE_SECRET)
        ig_user_id = cookieutil.get_secure_cookie(name = "ig_user_id")

        profiles = Profile.all()
        profiles.filter("ig_user_id =", ig_user_id)
        profile = profiles.get()

        if profile:
            profile.delete()

        self.redirect("/")
Ejemplo n.º 7
0
    def get(self):
        cookieutil = LilCookies(self, settings.COOKIE_SECRET)
        ig_user_id = cookieutil.get_secure_cookie(name = "ig_user_id")

        profiles = Profile.all()
        profiles.filter("ig_user_id =", ig_user_id)
        profile = profiles.get()

        if profile:
            self.render_template("connected.html")
        else:
            self.render_template("not_connected.html", {
                "client_id": settings.INSTAGRAM_CONFIG["client_id"]
            })
Ejemplo n.º 8
0
    def get(self):
        cookieutil = LilCookies(self, settings.COOKIE_SECRET)
        ig_user_id = cookieutil.get_secure_cookie(name="ig_user_id")

        profiles = Profile.all()
        profiles.filter("ig_user_id =", ig_user_id)
        profile = profiles.get()

        if profile:
            profile.db_access_token_key = None
            profile.db_oauth_token_secret = None
            profile.put()

        self.redirect("/")
Ejemplo n.º 9
0
    def get(self):
        cookieutil = LilCookies(self, settings.COOKIE_SECRET)
        ig_user_id = cookieutil.get_secure_cookie(name="ig_user_id")

        profiles = Profile.all()
        profiles.filter("ig_user_id =", ig_user_id)
        profile = profiles.get()

        if profile:
            profile.db_access_token_key = None
            profile.db_oauth_token_secret = None
            profile.put()

        self.redirect("/")
Ejemplo n.º 10
0
    def get(self):
        cookieutil = LilCookies(self, settings.COOKIE_SECRET)
        ig_user_id = cookieutil.get_secure_cookie(name = "ig_user_id")

        profiles = Profile.all()
        profiles.filter("ig_user_id =", ig_user_id)
        profile = profiles.get()

        if profile and profile.fully_connected():
            self.render_template("connected.html")
        else:
            self.render_template("not_connected.html", {
                "profile": profile,
                "client_id": settings.INSTAGRAM_CONFIG["client_id"]
            })
Ejemplo n.º 11
0
  def get(self):

    cookies = LilCookies(self, secrets.cookie_secret)
    username = cookies.get_secure_cookie(cfg.COOKIE_USERNAME)

    if username is None:
      self.renderTemplate(cfg.INDEX_TEMPLATE)
      return

    d = model.MarkovUserV1.get_by_key_name(username)
    if d is None:
      cookies.clear_cookie(cfg.COOKIE_USERNAME)
      self.response.out.write(default)
      return

    self.renderTemplate(cfg.AUTHED_INDEX_TEMPLATE, USER=username)
Ejemplo n.º 12
0
    def get(self):
        cookieutil = LilCookies(self, settings.COOKIE_SECRET)
        ig_user_id = cookieutil.get_secure_cookie(name = "ig_user_id")


        profiles = Profile.all()
        profiles.filter("ig_user_id =", ig_user_id)
        profile = profiles.get()

        if profile and profile.db_access_token_key and \
                       profile.db_access_token_secret:
            self.redirect("/")
        elif profile and not (profile.db_access_token_key or
                              profile.db_access_token_secret):
            self.redirect("/dropbox/auth")
        else:
            self.redirect("/instagram/auth")
Ejemplo n.º 13
0
    def get(self):
        cookieutil = LilCookies(self, settings.COOKIE_SECRET)
        ig_user_id = cookieutil.get_secure_cookie(name="ig_user_id")

        dba = dropbox_auth.Authenticator(settings.DROPBOX_CONFIG)
        req_token = dba.obtain_request_token()

        profiles = Profile.all()
        profiles.filter("ig_user_id =", ig_user_id)
        profile = profiles.get()

        if not profile:
            self.redirect("/connect")
            return

        profile.db_oauth_token_key = req_token.key
        profile.db_oauth_token_secret = req_token.secret
        profile.put()

        authorize_url = dba.build_authorize_url(req_token, callback=settings.DROPBOX_CALLBACK)

        self.redirect(authorize_url)
Ejemplo n.º 14
0
  def get(self):

    cookies = LilCookies(self, secrets.cookie_secret)
    username = cookies.get_secure_cookie(cfg.COOKIE_USERNAME)

    if username is None:
      self.error(404)
      self.response.out.write(not_found.geterr())
      return

    d = model.MarkovUserV1.get_by_key_name(username)
    if not d:
      self.error(404)
      self.response.out.write(not_found.geterr())
      return

    stored_tweets = d.tweets
    if not stored_tweets:
      texts = fetch_tweets(d.userToken, d.userSecret)
      d.tweets = zlib.compress(pickle.dumps(texts))
      d.put()
    else:
      texts = pickle.loads(zlib.decompress(stored_tweets))

    order, table = 7, {}

    for text in texts:
        table = mrkvtwt.makeTableN(order, text, table)

    for generated in mrkvtwt.markov(order, table):
      continue

    tweet = ''.join(generated)[:140]

    d.generated = tweet
    d.put()

    self.renderTemplate(cfg.GENERATE_DONE_TEMPLATE, POST=d.generated)
Ejemplo n.º 15
0
    def get(self):
        cookieutil = LilCookies(self, settings.COOKIE_SECRET)
        ig_user_id = cookieutil.get_secure_cookie(name="ig_user_id")

        dba = dropbox_auth.Authenticator(settings.DROPBOX_CONFIG)
        req_token = dba.obtain_request_token()

        profiles = Profile.all()
        profiles.filter("ig_user_id =", ig_user_id)
        profile = profiles.get()

        if not profile:
            self.redirect("/connect")
            return

        profile.db_oauth_token_key = req_token.key
        profile.db_oauth_token_secret = req_token.secret
        profile.put()

        authorize_url = dba.build_authorize_url(
            req_token, callback=settings.DROPBOX_CALLBACK)

        self.redirect(authorize_url)