[用于Python输出数据文件的GCP云功能

问题描述 投票:0回答:1

我是GCP的新手,不确定是否要使用Cloud Functions实现此功能。

  1. 我有一个python脚本,该脚本使用tweepy调用twitter api,并生成一个带有该特定用户名推文列表的csv文件。
#!/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
  1. 我想运行此脚本并通过http请求(例如,csv文件或return contents)检索结果。使用javascript。该脚本只需要每天运行一次。但是生成的数据(csv)应该可以按需提供。

因此,我的问题是

a。 GCP Cloud Functions是适合该工作的工具吗?还是需要一些更广泛的内容,因此是否需要GCP VM实例?

b。要使其在GCP上运行,需要在代码中进行哪些更改?

也欢迎您提供有关方向的任何帮助/建议。

python google-cloud-platform google-cloud-functions tweepy twitterapi-python
1个回答
2
投票

GCP Cloud Functions是执行此任务的正确工具吗?还是需要一些更广泛的内容,因此是否需要GCP VM实例?

取决于。 1个CPU的处理时间是否少于9分钟?并且您的进程是否需要少于2Gb的内存(应用程序内存占用量+文件大小+ tweets数组大小)?

为什么文件大小?因为只有/tmp目录是可写的,并且它是内存中的文件系统。

如果需要最多15分钟的超时,可以查看Cloud Run,与Cloud Function和I personally prefer非常相似。 Cloud Function和Cloud Run在CPU和内存上的限制是相同的(但随着CPU和内存的增加,它在2020年应该会改变)

要使其在GCP上运行,需要在代码中进行哪些更改?

[首先从/tmp目录写入和读取。最后,如果您希望文件全天可用,请将其存储在Cloud Storage(https://cloud.google.com/storage/docs)中并在函数开始时进行检索。如果不存在,则为当天生成,否则获取现有的一天。

然后,将功能def fetchTweets(username):的签名替换为def fetchTweets(request):,并在请求参数中获取用户名

最终,如果每天需要生成一个Cloud Scheduler,则将其设置为。

您没有谈论安全性。我建议您在private mode

中部署功能
因此,此答案中有很多GCP无服务器概念,我不知道您对GCP的了解。如果您想要某些零件的精度,请随时询问!
© www.soinside.com 2019 - 2024. All rights reserved.