Procházet zdrojové kódy

Merged DrkSephy/django-hackathon-starter into default

Wan Kim Mok před 10 roky
rodič
revize
7065469201

+ 37 - 5
hackathon_starter/hackathon/scripts/twitter.py

@@ -111,19 +111,51 @@ class TwitterOauthClient(object):
         self.is_authorized = True
 
 
+    def get_tweets(self):
+        '''
+        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'}
+
+        oauthParameters = getOauthParameters(
+            self.consumer_key,
+            self.access_token
+        )
+
+        oauthParameters['oauth_signature'] = generateSignature(
+            method,
+            link,
+            linkParameters,
+            oauthParameters,
+            self.consumer_secret,
+            self.access_token_secret
+        )
+
+        headers = {'Authorization': createAuthHeader(oauthParameters)}
+
+        link += '?' + urllib.urlencode(linkParameters)
+
+        req = requests.get(link, headers=headers)
+
+        if int(req.status_code) != 200:
+            raise Exception('Invalid response %s' %req.status_code)
+
+        content = json2.loads(req.content)
+
+        return content['statuses']
+
+
     def get_trends_available(self, yahooConsumerKey):
         '''
         Get the locations that Twitter has trending topic information for.
 
         '''
 
-        method = "get"
+        method = 'get'
         link = 'https://api.twitter.com/1.1/trends/available.json'
         linkParameters = {}
-        #link = 'https://api.twitter.com/1.1/trends/closest.json'
-        #link_parameters = {'lat':'40.782032', 'long':'-73.9717188'}
-        #link = 'https://api.twitter.com/1.1/trends/place.json'
-        #link_parameters = {'id': '1'}
 
         oauthParameters = getOauthParameters(
             self.consumer_key,

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

@@ -13,6 +13,7 @@
 		<div class="col-sm-4"><a href="http://127.0.0.1:8000/hackathon/twilio/">Twilio Example</a></div>
 		<div class="col-sm-4"><a href="http://127.0.0.1:8000/hackathon/instagram/">Instagram Example</a></div>
 		<div class="col-sm-4"><a href="http://127.0.0.1:8000/hackathon/twitter/">Twitter Example</a></div>
+		<div class="col-sm-4"><a href="http://127.0.0.1:8000/hackathon/twitterTweets/">Twitter Tweet Example</a></div>
 		<div class="col-sm-4"><a href="http://localhost:8000/hackathon/quandlstocks/">Quandl Example</a></div>
 		<div class="col-sm-4"><a href="http://localhost:8000/hackathon/meetupUser/">Meetup</a></div>
 

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

@@ -0,0 +1,31 @@
+<!DOCTYPE html>
+<html>
+<body>
+    {% include 'hackathon/base.html' %}
+    <h1 class="text-center"> {{ title }}</h1> 
+    <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>
+            </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>
+
+</html>

+ 1 - 0
hackathon_starter/hackathon/urls.py

@@ -23,6 +23,7 @@ urlpatterns = patterns('',
     url(r'^instagramMediaByLocation/$', views.instagramMediaByLocation, name='instagramMediaByLocation'),#
     url(r'^instagramUserMedia/$', views.instagramUserMedia, name='instagramUserMedia'),
     url(r'^twitter/$', views.twitter, name='twitter'),
+    url(r'^twitterTweets/$', views.twitterTweets, name='twitterTweets'),
     url(r'^tumblr_login/$', views.tumblr_login, name='tumblr_login'),
     url(r'^twitter_login/$', views.twitter_login, name='twitter_login'),
     url(r'^facebook/$', views.facebook, name='facebook'),

+ 10 - 0
hackathon_starter/hackathon/views.py

@@ -393,6 +393,16 @@ def twitter(request):
     context ={'title': 'twitter', 'value': value}
     return render(request, 'hackathon/twitter.html', context)
 
+def twitterTweets(request):
+    print getTwitter.is_authorized
+    if getTwitter.is_authorized:
+        content = getTwitter.get_tweets()
+    else:
+        twitter_url = getTwitter.get_authorize_url()
+        return HttpResponseRedirect(twitter_url)
+
+    context ={'title': 'twitter tweet', 'content': content}
+    return render(request, 'hackathon/twitter_tweet.html', context)    
 
 ##################
 #  LINKED IN API #