I am very new to GCP and was not sure if Cloud Functions is the way to go for this.
- I have a python script which makes a call to the twitter api using tweepy and generates a csv file with a list of tweets for that particular username.
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import tweepy
import datetime
import csv
def fetchTweets(username):
# credentials from https://apps.twitter.com/
consumerKey = "" # hidden for security reasons
consumerSecret = "" # hidden for security reasons
accessToken = "" # hidden for security reasons
accessTokenSecret = "" # hidden for security reasons
auth = tweepy.OAuthHandler(consumerKey, consumerSecret)
auth.set_access_token(accessToken, accessTokenSecret)
api = tweepy.API(auth)
startDate = datetime.datetime(2019, 1, 1, 0, 0, 0)
endDate = datetime.datetime.now()
print (endDate)
tweets = []
tmpTweets = api.user_timeline(username)
for tweet in tmpTweets:
if tweet.created_at < endDate and tweet.created_at > startDate:
tweets.append(tweet)
lastid = ""
while (tmpTweets[-1].created_at > startDate and tmpTweets[-1].id != lastid):
print("Last Tweet @", tmpTweets[-1].created_at, " - fetching some more")
lastid = tmpTweets[-1].id
tmpTweets = api.user_timeline(username, max_id = tmpTweets[-1].id)
for tweet in tmpTweets:
if tweet.created_at < endDate and tweet.created_at > startDate:
tweets.append(tweet)
# # for CSV
#transform the tweepy tweets into a 2D array that will populate the csv
outtweets = [[tweet.id_str, tweet.created_at, tweet.text.encode("utf-8")] for tweet in tweets]
#write the csv
with open('%s_tweets.csv' % username, 'w', newline='') as f:
writer = csv.writer(f)
writer.writerow(["id","created","text"])
writer.writerows(outtweets)
pass
f = open('%s_tweets.csv' % username, "r")
contents = f.read()
return contents
fetchTweets('usernameofusertoretrieve') # this will be set manually in production
- I wanted to run this script and retrieve the results (either as the csv file or as the
return contents
) over an http request for e.g. using javascript. The script only needs to be run once a day. But the data generated (csv) should be available as required.
My question therefore is
a. is GCP Cloud Functions the correct tool for the job? or will this require something more extensive and therefore a GCP VM instance?
b. What would need to be changed in the code to make it run on GCP?
Any help/advice about the direction is also appreciated.