如何将代码转换为在AWS中工作?

2021-05-13 13:42:49 发布

您现在位置:Python中文网/ 问答频道 /正文

我在努力让Lambda函数工作。我有一个python脚本来访问twitter API,提取信息,并将这些信息导出到excel表中。我试图将python脚本转移到AWS/Lambda,但遇到了很多麻烦。在

到目前为止,我做了些什么:创建了AWS帐户,设置了S3来拥有一个bucket,并尝试着让它正常工作。在

我认为我正在努力解决的主要问题是如何从通过本地CLI执行的python脚本转换成支持lambda的代码。我不确定我是否理解lambda_handler函数的工作原理,event或{}参数的实际含义(尽管观看了六个不同的教程视频),也不知道如何在lambda_handler的上下文中将现有函数集成到Lambda中,我只是非常困惑,希望有人能帮我弄清楚一些!在

我用来提取twitter数据的代码(只是一个示例):

import time
import datetime 
import keys
import pandas as pd
from twython import Twython, TwythonError
import pymysql


def lambda_handler(event, context):

def oauth_authenticate():
    twitter_oauth = Twython(keys.APP_KEY, keys.APP_SECRET, oauth_version=2)
    ACCESS_TOKEN = twitter_oauth.obtain_access_token()
    twitter = Twython(keys.APP_KEY, access_token = ACCESS_TOKEN)
    return twitter

def get_username():
    """
   Prompts for the screen name of targetted account
    """
    username = input("Enter the Twitter screenname you'd like information on.  Do not include '@':")
    return username

def get_user_followers(username):
    """
    Returns data on all accounts following the targetted user.
    WARNING: The number of followers can be huge, and the data isn't very valuable
    """
    #username = get_username()
    #import pdb; pdb.set_trace()
   twitter = oauth_authenticate()
    datestamp = str(datetime.datetime.now().strftime("%Y-%m-%d"))
    target = twitter.lookup_user(screen_name = username)
    for y in target:
       target_id = y['id_str']
    next_cursor = -1
    index = 0
    followersdata = {}
    while next_cursor:
        try:
            get_followers = twitter.get_followers_list(screen_name = username,
                                                       count = 200,
                                                       cursor = next_cursor)
            for x in get_followers['users']:
                followersdata[index] = {}
                followersdata[index]['screen_name'] = x['screen_name']
                followersdata[index]['id_str'] = x['id_str']
                followersdata[index]['name'] = x['name']
                followersdata[index]['description'] = x['description']
                followersdata[index]['date_checked'] = datestamp
                followersdata[index]['targeted_account_id'] = target_id
                index = index + 1
                next_cursor = get_followers["next_cursor"]
        except TwythonError as e:
            print(e)
            remainder = (float(twitter.get_lastfunction_header(header = 'x-rate-limit-reset')) \
                         - time.time())+1
            print("Rate limit exceeded.  Waiting for:", remainder/60, "minutes")
            print("Current Time is:", time.strftime("%I:%M:%S"))
            del twitter
            time.sleep(remainder)
            twitter = oauth_authenticate()
            continue
    followersDF = pd.DataFrame.from_dict(followersdata, orient = "index")
    followersDF.to_excel("%s-%s-follower list.xlsx" % (username, datestamp),
                         index = False, encoding = 'utf-8')