python - 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 的变量来对用户名进行硬编码?
你应该做的是发送你从表单中获取的用户名,像这样执行你的 get_videos 方法。
get_videos(用户名)
并将您的其他方法更改为此 def get_videos(MyUsername, cursor=None):
推荐阅读
- javascript - 为映射的 true 或 false 值的集合返回 true/false
- migration - CircleCI 错误将配置迁移到版本 2
- regex - 如何在没有查询的情况下剥离 href 属性?
- mysql - SQL 事件 - 更新其他表后删除和更新表上的行
- vue.js - Vue:仅在以编程方式重定向到时才允许访问页面
- python - 均值方差优化 + Python + 调整约束
- c# - xamarin listview 获取所选项目
- c# - 在 CSHMTL 页面中使用 Razor 在 FOR 中嵌套 IF
- mysql - 查询帮助 - 如何获取所有在前 20 家商店购物过的顾客?
- ios - 检查用户是否已登录 iTunes Store