1. JD Maturen
  2. friendstream

Commits

JD Maturen  committed 36d71b4

finally adding embed support! :)

  • Participants
  • Parent commits d5fcde5
  • Branches default

Comments (0)

Files changed (6)

File main.py

View file
         - handle cached timeline data
     """
     
-    def get_client_timeline(self, access_token_keyname, template_name='templates/timeline.html'):
+    def get_client_timeline(self, access_token_keyname, template_name='templates/timeline.html', 
+        css=None):
         """
         Return cached user timeline
             - cache based on number of api calls user has per hour
         refresh_rate = cache_timeout * 1000
         
         template_values = {
-            'user' : user,
-            'timeline' : timeline,
+            'css': css,
             'rate_info' : response,
             'refresh_rate': refresh_rate,
+            'timeline' : timeline,
+            'user' : user,
             }
         
         html = template.render(template_name, template_values)
         return html
 
 
+class WidgetHandler(StreamHandler):
+    """Mirror of Mainhandler but for our widget iframe"""
+    
+    def get(self, user_id):
+        """setup iframe for given user"""
+        token = OAuthAccessToken.all().filter(
+            'user_id =', user_id).filter(
+            'service =', 'twitter').get()
+        if not token:
+            # @todo return better template
+            raise HTTP404('Not authorized for user %s.' % user_id)
+        
+        access_token_keyname = token.key().name()
+        
+        html = self.get_client_timeline(access_token_keyname,
+            template_name='templates/widget_index.html', css=['widget.css'])
+        self.response.out.write(html)
+
+
 class TimelineHandler(StreamHandler):
     """
     Provide the barebones stream for AJAX / Widget
                 'user_id =', user_id).filter(
                 'service =', 'twitter').get()
             if not token:
-                raise HTTP404('No auth token for the user %s' % user_id)
+                raise HTTP404('Not authorized for user %s.' % user_id)
             access_token_keyname = token.key().name()
             # @todo pass this token directly to TwitterOAuthClient
             # so it doesn't have to refetch it
         self.response.out.write(html)
 
 
+class EmbedHandler(StreamHandler):
+    """Serve embedding instruction page"""
+    
+    def get(self, user_id):
+        self.response.out.write(self.get_html(user_id))
+    
+    def get_html(self, user_id):
+        cache_key = 'embed-%d' % int(user_id)
+        html = memcache.get(cache_key)
+                if html is not None:
+                    return html
+                
+        template_values = {
+            'user_id' : user_id
+        }
+        html = template.render('templates/embed.html', template_values)
+        memcache.add(cache_key, html, 600)
+        return html
+
+
 class AboutHandler(FriendlyRequestHandler):
     """Serve about page"""
     
         html = template.render('templates/about.html', template_values)
         memcache.add('about', html, 600)
         return html
+        
 
 
 class FourOhFourHandler(FriendlyRequestHandler):
     ('/login', LoginHandler),
     ('/callback', CallbackHandler),
     (r'/timeline/(\d*)', TimelineHandler),
+    (r'/widget/(\d*)', WidgetHandler),
+    (r'/embed/(\d*)', EmbedHandler),
     (r'/500', Raise500Handler),
     (r'/(.+)', FourOhFourHandler),
     ], debug=debug)

File static/widget.css

View file
+/**
+ * Copyright (c) 2009 JD Maturen
+ *
+ * CSS licensed under the Creative Commons 
+ * Attribution-Share Alike 3.0 United States license.
+ */
+
+body {
+    width           : 300px;
+    padding         : 1em 0;
+    font-family     : "Lucida Grande", sans-serif;
+    font-size       : 12px;
+}
+
+img {
+    height  : 24px;
+    width   : 24px;
+}
+
+h1 a, h1 a:visited {
+    color           : #BD1B0E; /* comp: #0B9122 */;
+    text-decoration : none;
+}

File templates/base.html

View file
         
     </head>
     <body>
+        {% block header %}
         <h1><span class="friend">Friend</span> Stream</h1>
+        {% endblock %}
         {% block content %}{% endblock %}
         <!--[if lt IE 8]>
         <script src="http://ie7-js.googlecode.com/svn/version/2.0(beta3)/IE8.js" type="text/javascript"></script>

File templates/embed.html

View file
+{% extends 'base.html' %}
+
+{% block content %}
+<p><a href="/">« Home</a></p>
+
+<p>
+    You can embed your FriendStream into any website with this bit of code: <input type="text" 
+    size="80" value="&lt;iframe frameborder=&quot;0&quot; height=&quot;432&quot; name=&quot;friendstream&quot; scrolling=&quot;yes&quot; src=&quot;http://fs.idlerice.com:8080/widget/{{ user_id }}&quot; width=&quot;324&quot;&gt;&lt;/iframe&gt;">
+</p>
+
+<p>
+    If you revoke permission for FriendStream from within Twitter then this embeddable widget will no longer work.  
+</p>
+
+<p>
+    FriendStream never displays protected users tweets, so it is safe to embed on a public site or to 
+    leave running on a giant monitor at work –– hint hint @twoffice ;)
+</p>
+
+{% endblock %}

File templates/index.html

View file
 <p>
     <a href="/logout" id="logout">» Logout from Twitter</a><br>
     <a href="/about">» What is this?</a><br>
+    <a href="/embed/{{ user.id }}">» Embed FriendStream</a><br>
     <a href="http://twitter.com/friendstream">» @friendstream</a>
 </p>
 

File templates/widget_index.html

View file
+{% extends "base.html" %}
+
+
+{% block header %}
+<h1><a href="http://fs.idlerice.com/" target="_blank">
+    <span class="friend">Friend</span> Stream</a></h1>
+{% endblock %}
+
+{% block content %}
+<p>
+    <strong>Screen Name:</strong> {{ user.screen_name }}<br>
+    {% if user.location %}
+    <strong>Location:</strong> {{ user.location }}
+    {% endif %}
+</p>
+
+<div id="timeline">
+    {% include 'timeline.html' %}
+</div>
+
+<script type="text/javascript" src="http://www.google.com/jsapi"></script>
+<script type="text/javascript">
+    google.load("jquery", "1.3");
+</script>
+<script src='http://jquery.offput.ca/js/jquery.timers.js' type='text/javascript'></script>
+<script type="text/javascript">
+    //<![CDATA[
+    $(document).ready(function() {
+        $('#timeline').everyTime({% firstof refresh_rate 60000 %}, function () {
+            $('#timeline').load('/timeline/{{ user.id }}')
+        });
+    });
+    //]]>
+</script>
+{% endblock %}