如何解决Flask和python不允许递归函数Twitch API
我有一个功能可以在Twitch上生成某个用户的所有视频URL
def get_videos(cursor=None): # functiont to retrieve all vod URLs possible,kinda slow for Now
params_get_videos = {('user_id',userid_var)} # params for request.get
if cursor is not None: # check if there was a cursor value passed
params_get_videos = list(params_get_videos) + list({('after',cursor)}) # add another param for pagination
url_get_videos = 'https://api.twitch.tv/helix/videos' # URL to request data
response_get_videos = session.get(url_get_videos,params=params_get_videos,headers=headers) # get the data
reponse_get_videos_json = response_get_videos.json() # parse and interpret data
file = open(MyUsername+" videos.txt","w")
for i in range(0,len(reponse_get_videos_json['data'])): # parse and interpret data
file.write(reponse_get_videos_json['data'][i]['url'] +'\n') # parse and interpret data
if 'cursor' in reponse_get_videos_json['pagination']: # check if there are more pages
get_videos(reponse_get_videos_json['pagination']['cursor']) # iterate the function until there are no more pages
这可以单独(和其他功能)很好地工作,但是每当我尝试从这样的虚拟烧瓶服务器调用它时
from flask import Flask
from flask import render_template
from flask import request
from main import *
app = Flask(__name__)
@app.route('/')
def hello_world():
return render_template("hello.html")
@app.route('/magic',methods=['POST','GET'])
def get_username():
username = request.form.get('username')
get_videos()
return ("Success")
它不再执行递归操作,仅输出前20个值。我在做什么错了?
解决方法
我也是新来的,所以我没有足够的声誉来发表评论,我必须发表一个答案。我对您的get_username方法感到困惑,一旦从表单中获取了用户名,就不会将其发送到任何地方?看起来在您的get_videos方法上,您可能通过在此方法之外保存一个名为MyUsername的变量来对用户名进行硬编码吗?
您应该执行的imo是发送从表单中获取的用户名,执行您的get_videos方法。
get_videos(用户名)
然后将其他方法更改为此 def get_videos(MyUsername,cursor = None):
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。