Permalink
Browse files

Seems to work OK, but do we need the instapaper's token?

  • Loading branch information...
1 parent d553fb8 commit 3159a77c01e962df9d5404312d32a374bc73497e @ydoovv ydoovv committed Oct 16, 2010
Showing with 313 additions and 22 deletions.
  1. +46 −15 main.py
  2. +96 −0 simplecookies.py
  3. +21 −2 static/style.css
  4. +3 −2 templates/base.html
  5. +147 −3 templates/main.html
View
@@ -12,25 +12,56 @@
from django.utils import simplejson as json
from google.appengine.ext.webapp import template
+from simplecookies import Cookies
+
class MainHandler(webapp.RequestHandler):
- def get(self):
- id_ = self.request.get("id")
- page = self.request.get("p")
- if page == "": page = "1"
- proxy = 'http://api.twitter.com/1/'
- url = '%s/favorites/%s.json?page=%s' % (proxy, id_, page)
- res = urlfetch.fetch(url)
- favs = json.loads(res.content)
- template_values = {"tweets": [], "id": id_, "page": int(page) + 1, "flashes":{"message": "Here is %s's favorites." % id_}}
- for fav in favs:
- tweet = fav["text"]
- urls = re.findall('http[s]?://(?:[a-zA-Z]|[0-9]|[$-_@.&+]|[!*\(\),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+', tweet)
- for url in urls:
- tweet = tweet.replace(url, '<a href="%s">%s</a>' % (url, url))
- template_values["tweets"].append({"content":"%s" % tweet})
+ def get_(self):
+ cookies = Cookies(self, max_age=180)
+ if 'user' in cookies:
+ self.response.out.write(cookies['user'])
+ else:
+ cookies['user'] = 'vvoody'
+
+ def render(self, template_values):
path = os.path.join(os.path.dirname(__file__), 'templates/main.html')
self.response.out.write(template.render(path, template_values))
+ def get(self):
+ cookies = Cookies(self, max_age=2678400) # one month expiration
+ template_values = {}
+ id_ = self.request.get("id")
+ instapaper_cookie = "instapaper" in cookies and cookies["instapaper"] or ""
+ instapaper_get = self.request.get("instapaper")
+
+ if instapaper_cookie == "" and id_ == "":
+ flashes = "First here? 'Howto' page will tell you howto fill up the fields below."
+ elif id_ == "":
+ flashes = "Please tell us your twitter id."
+ elif instapaper_cookie == "" and instapaper_get == "":
+ flashes = "Please tell us the link location of your Instapaper's 'Read Later'."
+ else:
+ if "instapaper" not in cookies:
+ cookies["instapaper"] = instapaper_get
+ template_values["instapaper"] = instapaper_cookie
+ page = self.request.get("p")
+ if page == "": page = "1"
+ proxy = 'http://ydoovv.appspot.com/'
+ url = '%s/favorites/%s.json?page=%s' % (proxy, id_, page)
+ res = urlfetch.fetch(url)
+ favs = json.loads(res.content)
+ template_values = {"tweets": [], "id": id_, "page": int(page) + 1}
+ for fav in favs:
+ tweet = fav["text"]
+ urls = re.findall('http[s]?://(?:[a-zA-Z]|[0-9]|[$-_@.&+]|[!*\(\),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+', tweet)
+ for url in urls:
+ tweet = tweet.replace(url, '<a href="%s">%s</a>' % (url, url))
+ template_values["tweets"].append({"content":"%s" % tweet, "inlineurl": "%s" % url})
+ flashes = "Here is %s's favorites." % id_
+ template_values["next"] = "yes"
+ template_values["flashes"] = flashes
+ self.render(template_values)
+ #self.response.out.write(template_values["tweets"])
+
def main():
application = webapp.WSGIApplication(
[
View
@@ -0,0 +1,96 @@
+# -*- coding: utf-8 -*-
+
+# Scratched from http://is.gd/g4sZT
+
+import UserDict
+from Cookie import BaseCookie
+class Cookies(UserDict.DictMixin):
+ def __init__(self,handler,**policy):
+ self.response = handler.response
+ self._in = handler.request.cookies
+ self.policy = policy
+ if 'secure' not in policy and handler.request.environ.get('HTTPS', '').lower() in ['on', 'true']:
+ policy['secure']=True
+ self._out = {}
+ def __getitem__(self, key):
+ if key in self._out:
+ return self._out[key]
+ if key in self._in:
+ return self._in[key]
+ raise KeyError(key)
+ def __setitem__(self, key, item):
+ self._out[key] = item
+ self.set_cookie(key, item, **self.policy)
+ def __contains__(self, key):
+ return key in self._in or key in self._out
+ def keys(self):
+ return self._in.keys() + self._out.keys()
+ def __delitem__(self, key):
+ if key in self._out:
+ del self._out[key]
+ self.unset_cookie(key)
+ if key in self._in:
+ del self._in[key]
+ p = {}
+ if 'path' in self.policy: p['path'] = self.policy['path']
+ if 'domain' in self.policy: p['domain'] = self.policy['domain']
+ self.delete_cookie(key, **p)
+ #begin WebOb functions
+ def set_cookie(self, key, value='', max_age=None,
+ path='/', domain=None, secure=None, httponly=False,
+ version=None, comment=None):
+ """
+ Set (add) a cookie for the response
+ """
+ cookies = BaseCookie()
+ cookies[key] = value
+ for var_name, var_value in [
+ ('max-age', max_age),
+ ('path', path),
+ ('domain', domain),
+ ('secure', secure),
+ ('HttpOnly', httponly),
+ ('version', version),
+ ('comment', comment),
+ ]:
+ if var_value is not None and var_value is not False:
+ cookies[key][var_name] = str(var_value)
+ if max_age is not None:
+ cookies[key]['expires'] = max_age
+ header_value = cookies[key].output(header='').lstrip()
+ self.response.headers._headers.append(('Set-Cookie', header_value))
+ def delete_cookie(self, key, path='/', domain=None):
+ """
+ Delete a cookie from the client. Note that path and domain must match
+ how the cookie was originally set.
+ This sets the cookie to the empty string, and max_age=0 so
+ that it should expire immediately.
+ """
+ self.set_cookie(key, '', path=path, domain=domain,
+ max_age=0)
+ def unset_cookie(self, key):
+ """
+ Unset a cookie with the given name (remove it from the
+ response). If there are multiple cookies (e.g., two cookies
+ with the same name and different paths or domains), all such
+ cookies will be deleted.
+ """
+ existing = self.response.headers.get_all('Set-Cookie')
+ if not existing:
+ raise KeyError(
+ "No cookies at all have been set")
+ del self.response.headers['Set-Cookie']
+ found = False
+ for header in existing:
+ cookies = BaseCookie()
+ cookies.load(header)
+ if key in cookies:
+ found = True
+ del cookies[key]
+ header = cookies.output(header='').lstrip()
+ if header:
+ self.response.headers.add('Set-Cookie', header)
+ if not found:
+ raise KeyError(
+ "No cookie has been set with the name %r" % key)
+ #end WebOb functions
View
@@ -159,7 +159,8 @@ div.page div.twitbox p {
}
div.page div.twitbox input[type="text"] {
- width: 585px;
+ width: 280px;
+ color: #888;
}
div.page div.twitbox input[type="submit"] {
@@ -194,4 +195,22 @@ div.error {
.notfound {
text-align: center;
-}
+}
+
+.bookmarklet {
+ display: inline-block;
+ float: right;
+ font-family: 'Lucida Grande', Verdana, sans-serif;
+ font-weight: bold;
+ font-size: 11px;
+ -webkit-border-radius: 8px;
+ -moz-border-radius: 8px;
+ color: #fff;
+ background-color: #626262;
+ border: 1px solid #626262;
+ padding: 0px 7px 1px 7px;
+ text-shadow: #3b3b3b 1px 1px 0px;
+ min-width: 62px;
+ text-align: center;
+ vertical-align: 2px;
+}
View
@@ -10,11 +10,12 @@
<h1>Digest Your Twitter's Favorites</h1>
<div class=navigation>
<a href="/">Home</a> |
- <a href="/about">About</a> |
+ <a href="/about">Howto</a> |
+ <a href="/about">About</a>
</div>
{% if flashes %}
<ul class=flashes>
- <li>{{ flashes.message }}</li>
+ <li>{{ flashes }}</li>
</ul>
{% endif %}
<div class=body>
Oops, something went wrong.

0 comments on commit 3159a77

Please sign in to comment.