Allow a list as argument

0

I have a REST API in Python that gets 2 arguments, a Url list and a word. I am a beginner in Python and would like to know if it is possible to split the URl string to accept more than one URL when requesting the body from the URL. For example, I can currently make the request at link

I just want to include more than one url in the body like link

Follow the code:

from flask import Flask
from flask_restful import Resource, Api, reqparse, abort
import requests

app = Flask(__name__)
api = Api(app)

parser = reqparse.RequestParser()
parser.add_argument('urls', action='append')
parser.add_argument('word')
parser.add_argument('ignorecase')

# Função que faz um GET para a URL e retorna quantas vezes a palavra word aparece no conteudo


def count_words_in(url, word, ignore_case):
    try:
        r = requests.get(url)
        data = str(r.text)
        if (str(ignore_case).lower() == 'true'):
            return data.lower().count(word.lower())
        else:
            return data.count(word)
    except Exception as e:
        raise e

# Função que inclui 'http://' na url e retorna a URL valida


def validate_url(url):
    if not(url.startswith('http')):
        url = 'http://' + url
    return url


class UrlCrawlerAPI(Resource):
    def get(self):
        try:
            args = parser.parse_args()
            valid_urls = [validate_url(url)  for url in args['urls']]
            lista = []
            for valid_url in valid_urls:
                lista.append({valid_url: {args['word']: count_words_in(valid_url, args['word'], args['ignorecase'])}})
                # return {valid_url: {args['word']: count_words_in(valid_url, args['word'], args['ignorecase'])}}
            return lista    
        except AttributeError:
            return {'message': 'Please provide URL and WORD arguments'}
        except Exception as e:
            return {'message': 'Unhandled Exception: ' + str(e)}


api.add_resource(UrlCrawlerAPI, "/")

if __name__ == '__main__':
    app.run(debug=True) 
    
asked by anonymous 01.10.2018 / 15:21

2 answers

1

According to Flask Restful documentation simply set the URL correctly for the framework to make append of the parameters repeated, instead of overwriting the existing ones:

parser.add_argument('urls', action='append')

And then to get the value:

args = parser.parse_args()
args['urls']    # seria uma lista com todas as urls passados por parâmetro.
# aplicando 'validate_url' para todas as URLs recebidas
valid_urls = [validate_url(url) for url in args['url']]
# ou
valid_urls = map(validate_url, args['url'])
    
01.10.2018 / 16:41
1

If what is returned by parser.parse_args () is a dictionary, it is impossible. Otherwise, you can pass the urls all together ...

For example: urls = globo.com, terra.com, uol.com

And separate them using str.split ()

urls = args.split(',')

or something like that

    
01.10.2018 / 15:49