July 2017
Intermediate to advanced
374 pages
8h
English
This method lists all the tweets from all the users.
Add the following code to app.py to add the route for the GET method:
@app.route('/api/v2/tweets', methods=['GET'])
def get_tweets():
return list_tweets()
Let's define list_tweets() function which connects to database and get us all the tweets and respond back with tweets list
def list_tweets():
conn = sqlite3.connect('mydb.db')
print ("Opened database successfully");
api_list=[]
cursor = conn.execute("SELECT username, body, tweet_time, id from tweets") data = cursor.fetchall() if data != 0: for row in cursor: tweets = {} tweets['Tweet By'] = row[0] tweets['Body'] = row[1] tweets['Timestamp'] = row[2] tweets['id'] = row[3] api_list.append(tweets) else: return api_list ...Read now
Unlock full access