debug python script - Flask api

В роботі Опубліковано %project.relative_time Оплачується при отриманні
В роботі Оплачується при отриманні

Hello I have a very simple Flask app for fetching tweets from twitter...

[login to view URL] outputs the word "hello" and nothing else,

[login to view URL] text

returns 100 tweets containg 'any text' or 100 tweets containing whatever text you insert as a query string.

It runs great for a number of hours then crashes.

The / endpoint still outputs hello but the api for fetcing tweets is completly broken after the crash.

The app works fine again for a few hours after restarting the dynos manually then it crashes again.

I thought maybe it was because the dyno was going to sleep so I upgraded to 'hobby' service., but it doesn't help matters except it seems to run for a little longer before crashing.

There is nothing in the logs at the moment because the logs don't go back far enough to the time of the error.

Below is the entire code for the app

import tweepy

import json

import sys

from objdict import ObjDict

from flask import Flask

from flask import request

from [login to view URL] import SentimentIntensityAnalyzer

from flask_cors import CORS

app = Flask(__name__)

CORS(app)

@[login to view URL]("/")

def helloWorld():

return "Hello"

# Enter authorisations

consumer_key = "my-consumer-key"

consumer_secret = "my-consumer-secret"

access_key = "my-access-key"

access_secret = "my-access-secret"

# Set up your authorisations

auth = [login to view URL](consumer_key, consumer_secret)

auth.set_access_token(access_key, access_secret)

# Set up API call

api = [login to view URL](auth, parser = [login to view URL]())

#Make another [login to view URL]() decorator here that takes in an integer id in the

@[login to view URL]('/subject-tweets/<string:query>')

def getTweetsofQuery(query):

if [login to view URL] == 'GET':

# Set search query

searchquery = query + '-filter:retweets'

sentences = []

data = [login to view URL](q = searchquery, count = 100, lang = 'en', result_type = 'mixed')

print 'size of data is ' + str([login to view URL](data))

emptyData = False

#gotdata = None

try:

#gotdata = [login to view URL]()[1][10]['text']

for x in range(1, 99):

[login to view URL]([login to view URL]()[1][x]['text'])

except IndexError:

[login to view URL]('no tweets for ' + query + ' - sorry')

emptyData = True

except Exception as e:

# do something with your exception

print(str(e))

analyzer = SentimentIntensityAnalyzer()

compoundScores = []

for sentence in sentences:

vs = analyzer.polarity_scores(sentence)

[login to view URL](vs['compound'])

#print(str(vs['compound']))

def mean(compoundScores ):

return float(sum(compoundScores )) / max(len(compoundScores ), 1)

data = ObjDict()

[login to view URL] = query

if emptyData == True:

[login to view URL] = 'sorry we dont have a score for ' + query

[login to view URL] = "It was impossible to measure sentiment for " + query + " this time because no one tweeted that phrase in the last 7 days"

[login to view URL] = ["Sorry we don't have any tweets for " + query ]

else :

[login to view URL] = mean(compoundScores)

[login to view URL] = ' The sentiment score for ' + query + ' is ' + str(mean(compoundScores))

if [login to view URL] >= 0.5:

#[login to view URL] = 'The sentiment rating for '+ query + ' is positive'

[login to view URL] = 'Positive'

elif [login to view URL] > -0.5 and [login to view URL] < 0.5 :

#[login to view URL] = 'The sentiment rating for '+ query + ' is neutral'

[login to view URL] = 'Neutral'

elif [login to view URL] <= -0.5 :

#[login to view URL] = 'The sentiment rating for '+ query + ' is negative'

[login to view URL] = 'Negative'

[login to view URL] = sentences

json_data = [login to view URL]()

return [login to view URL](data)

if __name__ == '__main__':

[login to view URL] = True

[login to view URL](host='0.0.0.0', port=5000)

Here is the error message:

2018-01-24T15:25:11.180259+00:00 app[web.1]: File "/app/hello.py", line 45, in getTweetsofQuery
2018-01-24T15:25:11.180260+00:00 app[web.1]: gotdata = data.values()[1][10]['text']


The specific problem here is gotdata = data.values()[1][10]['text']. You are calling the [] method on a dict, with the key of 10, and the key doesn't exist, so a KeyError is thrown.

Python

ID Проекту: #16161685

Про проект

1 заявка Дистанційний проект Остання активність 6 роки(ів) тому

1 фрілансер у середньому готовий виконати цю роботу за £20

maurusergio

A proposal has not yet been provided

£20 GBP за 5 дні(-в)
(0 відгуків(и))
0.0