瀏覽代碼

convert twitter tweet example to a form

mk200789 10 年之前
父節點
當前提交
e5f327245c

+ 2 - 2
hackathon_starter/hackathon/scripts/twitter.py

@@ -114,13 +114,13 @@ class TwitterOauthClient(object):
         self.is_authorized = True
 
 
-    def get_tweets(self):
+    def get_tweets(self, tweet):
         '''
         Get tweets of relevant search query.
         '''
         method = 'get'
         link = 'https://api.twitter.com/1.1/search/tweets.json'
-        linkParameters = {'q':'obama', 'count': '100', 'result_type': 'popular'}
+        linkParameters = {'q': tweet, 'count': '100', 'result_type': 'popular'}
 
         oauthParameters = getOauthParameters(
             self.consumer_key,

+ 1 - 0
hackathon_starter/hackathon/templates/hackathon/instagram.html

@@ -13,6 +13,7 @@
 		  <button type="submit" class="btn btn-default">Search</button>
 		</form>
 	</div>
+	<br>
 
 	{% if tagged_media %}
 	<div class="col-lg-12">

+ 31 - 17
hackathon_starter/hackathon/templates/hackathon/twitter_tweet.html

@@ -2,30 +2,44 @@
 <html>
 <body>
     {% include 'hackathon/base.html' %}
-    <h1 class="text-center"> {{ title }}</h1> 
+    <h1 class="text-center"> {{ title }}</h1>
+    <br>
+
+    <div class="container text-center">
+        <form class="form-inline" method='get' action="/hackathon/twitterTweets/">
+          <div class="form-group">
+            <input type="text" name="tweets" class="form-control" placeholder="Search a tagged media">
+          </div>
+          <button type="submit" class="btn btn-default">Search</button>
+        </form>
+    </div>
+    <br>
+
+    {% if content %}
     <div class="col-lg-12">      
         <div class="table-responsive">
             <table class="table table-bordered table-hover table-striped tablesorter">
                 <thead>
-                <tr>
-                <th class="header"> Who <i class="icon-sort"></i></th>
-                <th class="header"> Tweet <i class="icon-sort"></i></th>
-                <th class="header"> Retweets <i class="icon-sort"></i></th>
-                <th class="header"> Created <i class="icon-sort"></i></th>
+                    <tr>
+                    <th class="header"> Who <i class="icon-sort"></i></th>
+                    <th class="header"> Tweet <i class="icon-sort"></i></th>
+                    <th class="header"> Retweets <i class="icon-sort"></i></th>
+                    <th class="header"> Created <i class="icon-sort"></i></th>
 
-                </tr>
-            </thead>
-            <tbody>
-            {% for item in content %}
-                <tr>
-                    <td>{{ item.user.screen_name }}</td> 
-                    <td>{{ item.text }}</td>
-                    <td>{{ item.retweet_count}}</td>
-                    <td>{{ item.created_at}}</td>
-            {% endfor %}
-            </tbody>
+                    </tr>
+                </thead>
+                <tbody>
+                {% for item in content %}
+                    <tr>
+                        <td>{{ item.user.screen_name }}</td> 
+                        <td>{{ item.text }}</td>
+                        <td>{{ item.retweet_count}}</td>
+                        <td>{{ item.created_at}}</td>
+                {% endfor %}
+                </tbody>
             </table>
         </div>
     </div>
+    {% endif %}
 </body>
 </html>

+ 7 - 1
hackathon_starter/hackathon/views.py

@@ -440,7 +440,13 @@ def twitter(request):
 def twitterTweets(request):
     print getTwitter.is_authorized
     if getTwitter.is_authorized:
-        content = getTwitter.get_tweets()
+        #content = getTwitter.get_tweets()
+        if request.method == 'GET':
+            if request.GET.items():
+                tweets = request.GET.get('tweets')
+                content = getTwitter.get_tweets(tweets)
+            else:
+                content = ''
     else:
         global profile_track
         profile_track = 'twitter'