def timeline(request): # this should occur only one time ttl = twitter.Twitter(auth=twitter.OAuth("607817327-PQcljjGheGvAeGxqpngFdo80FXgm0RDjAHXYPVjT", "PxyS6Mr3iUuNp4LdsA796V2a23l8VTFnK6nQxEGaPR0", "xllraX45N5CmMCTQwEDfQ", "gB07DLx9V6xWUpceunMQ40OuuOANOlDuhZHRnl2RlY")) clas = Classificator() clas.gender_train("library/dataset/gender.csv") bayes = Bayes("library/dataset/training-sentiment/") # back to normal from here ht = ttl.statuses.home_timeline() result = [] for tweet in ht: result.append(dict(name=tweet['user']['name'], timestamp=time.strftime('%Y-%m-%d %H:%M:%S', time.strptime(tweet['created_at'],'%a %b %d %H:%M:%S +0000 %Y')), img=tweet['user']['profile_image_url'], text=tweet['text'], screenname=tweet['user']['screen_name'], gender=clas.gender_classify(tweet['user']['name']), sentiment=bayes.classify(tweet['text']), ) ) t = loader.get_template('tweets.html') c = Context({ 'result' : result, }) return HttpResponse(t.render(c))
def search(request, keyword, numresult=3): tws = twitter.Twitter(domain="search.twitter.com") twa = twitter.Twitter(domain="api.twitter.com") sr = tws.search(q=keyword,rpp=numresult) clas = Classificator() clas.gender_train("library/dataset/gender.csv") bayes = Bayes("library/dataset/training-sentiment/") # searchresult = [res['text'] for res in searchresult['results']] result = [] for tweet in sr['results']: namefromid = twa.users.show(user_id=tweet['from_user_id']) result.append(dict(name=namefromid['name'], timestamp=time.strftime('%Y-%m-%d %H:%M:%S', time.strptime(tweet['created_at'],'%a, %d %b %Y %H:%M:%S +0000')), img=tweet['profile_image_url'], text=tweet['text'], screenname=tweet['from_user'], gender=clas.gender_classify(namefromid['name']), sentiment=bayes.classify(tweet['text']), ) ) t = loader.get_template('tweets.html') c = Context({ 'result' : result, }) return HttpResponse(t.render(c))