0

I have a function that generates all video URLs of some user on Twitch

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

this works perfectly fine on its own (with other functions) but whenever i try to call it from a dummy flask server like this

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")

It no longer acts recursive and only prints first 20 values. What am I doing wrong?

btjmga
  • 13
  • 2

1 Answers1

0

I'm new also so I don't have enough reputation to make a comment, I have to post an answer. I'm confused by your get_username method, once you grab the username from the form you're not sending it anywhere? It looks like on your get_videos method you are maybe hard coding the username by saving a variable called MyUsername outside of this method?

What you should do imo is send the username you grab from the form, do your get_videos method like this.

get_videos(username)

And change your other method to this def get_videos(MyUsername, cursor=None):

Andrew Clark
  • 850
  • 7
  • 13
  • yes, once im getting the username I'm not sending it anywhere (for now) because it doesn't matter since i can't get get_videos to work + username is for now storred locally in main.py + get_videos method works for first 20 URLs – btjmga Oct 28 '20 at 20:36
  • Oh I think I know what could be wrong. in your get_videos method, after you loop through your response_get_videos_json data you then have an if statement that recursively begins your method again if there are more pages. The problem w/ this is that you are telling your program to open a new file each time. You should just open the file once, and close it once you are done. So you need to open the file before calling this method, and send the file to this method as an argument, if you want to be able to call it recursively. – Andrew Clark Oct 28 '20 at 21:40
  • @btjmga I followed you on github. If you share repo w/ me I'm 100% confident I can help you fix tonight when I get home from work. – Andrew Clark Oct 28 '20 at 21:47
  • 1
    that's a very good point, i looked so hard i overlooked a silly mistake, thank you! – btjmga Oct 29 '20 at 12:20