def test_non_content(self): """Test that the tweet API returns an empty array when the time range is wrong""" data = tweets.tweets('GOOG', start=time.time(), end=time.time()-864000) self.assertEqual(len(data['Google Inc.']), 0)
def test_content_no_params(self): """ Test that the tweets API returns something when start and end are not specified """ data = tweets.tweets('GOOG') self.assertTrue(len(data['Google Inc.']) > 0)
def dump(method='afinn'): """ Do sentiment analysis on all tweets for all companies and write the results to a file. """ sentimentanalysis = Sentimentanalysis() sent_method = None if method == 'afinn': sent_method = sentimentanalysis.afinnsentiment elif method == 'labmt': sent_method = sentimentanalysis.labmtsentiment for stock_symbol, company in settings.STOCK_SYMBOL_MAPPINGS.items(): print 'Company: ' + str(company) data = tweets.tweets(stock_symbol) sentiment = {} filename = filename_for_company(method, company) print 'Filename: ' + str(filename) try: sentiment = json.loads(open(filename).read()) except: print 'No file data' for key in data: docs = data[key] first = True count = 0 for doc in docs: count += 1 print 'Iterating %d of %d' % (count, len(docs)) tweetdate = datetime.datetime.fromtimestamp(doc['timestamp']).strftime('%Y-%m-%d %H:%M:%S').split(' ')[0] if first: sentiment[tweetdate] = 0 first = False tweet = doc['tweet'] isenglish = sentimentanalysis.evaluatetweet(tweet) if isenglish > 0.8: sentval = sent_method(tweet) if sentiment.get(tweetdate, 0) == 0: sentiment[tweetdate] = sentval else: sentiment[tweetdate] = (sentiment.get(tweetdate, 0) + sentval)/2 with io.open(filename, 'wb') as outfile: json.dump(sentiment, outfile) print 'Saved file: ' + filename
def query_company(request, stock_symbol): """ Return tweets for company matching a stock symbol. It is possible to define URL parameters to filter on start and end Epoch timestamps. """ try: start = request.GET.get('start') if start: start = int(start) end = request.GET.get('end') if end: end = int(end) except Exception: error = _error_message('Start and end parameters must be \ in valid UNIX timestamp format') return HttpResponse(error, status=400, mimetype='application/json') # return HttpResponse('hell') data = tweets.tweets(stock_symbol, start=start, end=end) return HttpResponse(json.dumps(data))
def test_content(self): """Test that the tweet API returns something""" data = tweets.tweets('GOOG', start=time.time()-864000, end=time.time()) self.assertTrue(len(data['Google Inc.']) > 0)