Sosavpm
Sosavpm

Reputation: 693

Python Flask - Responding with a redirected website

(unsure of how to phrase this question)

Essentially I'm working with Flask + Soundcloud and what I want to do is to request an http site (which i know will redirect me to a new site) and then i want to return that site (with the same headers and info i originally got). Maybe this explains it better:

@app.route('/play')
def SongURL2():
    stream_url="https://api.soundcloud.com/tracks/91941888/stream?client_id=MYCLIENTID"
    // newurl = HTTP_REQUEST(stream_url) <- This will redirect me to the actual song streaming link (which only lives for a little bit)
    // return newurl;

This is because soundcloud's song's streaming url only live for a short period of time and the device I am using to call my RESTful api will not allow me to do a simple redirect to the newlink. So I need to somehow act like a proxy.

Upvotes: 0

Views: 63

Answers (2)

Sosavpm
Sosavpm

Reputation: 693

Found an interesting way to proxy through Flask, similar to what @Dauros was aiming at. http://flask.pocoo.org/snippets/118/ In the end bare in mind that this puts extra strain on the server.

Upvotes: 0

Dauros
Dauros

Reputation: 10557

You can achieve this using the Request module:

import requests

@app.route('/play')
def SongURL2():
    stream_url="https://api.soundcloud.com/tracks/91941888/stream?client_id=MYCLIENTID"

    # Get the redirected url        
    r = request.get(stream_url)

    return r.url

Upvotes: 1

Related Questions