Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes Issue #215 #316

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ sphinx_settings.json
venv/
env/
.gitignore

.venv

# Byte-compiled / optimized / DLL files
__pycache__/
Expand Down
2 changes: 1 addition & 1 deletion _sources/lectures/TWP45/TWP45_2.rst
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ En este ejercicio vamos a acceder a Reddit para obetener datos como los de la si
import json

# La url de Reddit a la que accederemos
url = "https://cors.bridged.cc/http://www.reddit.com/r/Python/.json"
url = "https://api.allorigins.win/raw?url=http://www.reddit.com/r/Python/.json"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

CORS error still exists

resp = urllib.request.urlopen(url).read()

# La respuesta se da en formato json, se debe transformar a
Expand Down
2 changes: 1 addition & 1 deletion _sources/lectures/TWP45/TWP45_2_en.rst
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ In this exercise we are going to access Reddit to obtain data like the one in th
import json

# The Reddit URL we'll access
url = "https://cors.bridged.cc/http://www.reddit.com/r/Python/.json"
url = "https://api.allorigins.win/raw?url=http://www.reddit.com/r/Python/.json"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

CORS error still exits and lecture not working

resp = urllib.request.urlopen(url).read()

# The response is in JSON format, it needs to be transformed
Expand Down
42 changes: 25 additions & 17 deletions _sources/lectures/TWP45/TWP45_3.rst
Original file line number Diff line number Diff line change
Expand Up @@ -13,41 +13,47 @@ La documentación de la API de `TasteDive <https://tastedive.com/read/api>`_.

.. activecode:: ac_l45_3a
:nocodelens:
:language: python
:language: python3
:python3_interpreter: brython

En este caso, utilizaremos la librería ``requests`` para hacer la solicitud a la API. La url base
es ``"https://tastedive.com/api/similar"``. A esta url se le va a pasar un parámetro ``q`` con el
valor de la artista Ariana Grande. Al final la url se va a ver de la siguiente forma: ``"https://tastedive.com/api/similar?q=ariana+grande"``.
Note que después de la url base se escribe un ``?`` para indicar que siguen los parámetros.

~~~~
import requests
import urllib.request
import urllib.parse
import json

api_url = "https://tastedive.com/api/similar"
proxy = "https://cors.bridged.cc/"
proxy = "https://api.allorigins.win/raw?url="
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

CORS still not working


# Los parámetros que se pasarán a la url se escriben dentro de un diccionario.
parámetros = {"q": "ariana grande"}

# Los parámetros que se le pasaran a la url los escribimos dentro de un diccionario
parametros = {"q": "ariana grande"}
#Codificamos los parámetros
parámetros = urllib.parse.urlencode(parámetros)

# Solicitamos a la api los datos
respuesta = requests.get(proxy + api_url, params=parametros)
# Solicitamos los datos de la api
respuesta = urllib.request.urlopen(proxy + api_url + '?' + parámetros)

# Ahora imprimimos la url
print(respuesta.url)
print()
# Leemos la respuesta
datos = respuesta.leer()

# Transformamos los datos de formato json a Python
datos = json.loads(respuesta.text)
# Analizamos los datos JSON
json_data = json.loads(datos)

print(datos)
# Ahora imprimimos los datos
print(json.dumps(json_data, indent=4))


En el ejemplo anterior pudo apreciar que la API regresa un texto, que si lo pasamos por ``json.loads``
se transforma a un diccionario de Python. Sin embargo, no es del todo legible. Esto se puede solucionar con
``json.dumps``.

.. activecode:: ac_l45_3b
:nocodelens:
:language: python3
:python3_interpreter: brython

Expand All @@ -62,7 +68,8 @@ se transforma a un diccionario de Python. Sin embargo, no es del todo legible. E
import json

api_url = "https://tastedive.com/api/similar?"
proxy = "https://cors.bridged.cc/"
proxy = "https://api.allorigins.win/raw?url="
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

here too, CORS


# La siguiente línea es para los parámetros de la url.
parametros = urllib.parse.urlencode({"q": "coldplay"})

Expand Down Expand Up @@ -111,7 +118,8 @@ El siguiente ejercicio viene con calificación automática.
import json

api_url = "https://tastedive.com/api/similar"
proxy = "https://cors.bridged.cc/"
proxy = "https://api.allorigins.win/raw?url="


# Agregue los parámetros
parametros = {}
Expand Down Expand Up @@ -144,8 +152,8 @@ El siguiente ejercicio viene con calificación automática.
def testOne(self):
self.assertEqual(
solicitud_url,
"https://cors.bridged.cc/https://tastedive.com/api/similar?q=Coco&limit=5&info=1",
"Probando que la url sea: https://cors.bridged.cc/https://tastedive.com/api/similar?q=Coco&limit=5&info=1",
"https://api.allorigins.win/raw?url=https://tastedive.com/api/similar?q=Coco&limit=5&info=1",
"Probando que la url sea: https://api.allorigins.win/raw?url=https://tastedive.com/api/similar?q=Coco&limit=5&info=1",
)
self.assertEqual(resultados, 5, "Probando que resultados esté asignado correctamente.")
self.assertEqual(len(peliculas_similares), 5, "Probando que peliculas_similares sean: 5")
Expand Down
35 changes: 21 additions & 14 deletions _sources/lectures/TWP45/TWP45_3_en.rst
Original file line number Diff line number Diff line change
Expand Up @@ -12,41 +12,47 @@ The documentation for the `TasteDive API <https://tastedive.com/read/api>`_.

.. activecode:: ac_l45_3a_en
:nocodelens:
:language: python
:language: python3
:python3_interpreter: brython

In this case, we will use the ``requests`` library to make the API request. The base url
is ``"https://tastedive.com/api/similar"``. To this url, a parameter ``q`` with the
value of the artist Ariana Grande will be passed. Finally, the url will look like this: ``"https://tastedive.com/api/similar?q=ariana+grande"``.
Note that after the base url, a ``?`` is written to indicate that the parameters follow.

~~~~
import requests
import urllib.request
import urllib.parse
import json

api_url = "https://tastedive.com/api/similar"
proxy = "https://cors.bridged.cc/"
proxy = "https://api.allorigins.win/raw?url="

# The parameters that will be passed to the url are written inside a dictionary
parameters = {"q": "ariana grande"}

# We encode the parameters
params = urllib.parse.urlencode(parameters)

# We request the data from the api
response = requests.get(proxy + api_url, params=parameters)
response = urllib.request.urlopen(proxy + api_url + '?' + params)

# Now we print the url
print(response.url)
print()
# We read the response
data = response.read()

# We transform the json-formatted data to Python data
data = json.loads(response.text)
# We parse the JSON data
json_data = json.loads(data)

print(data)
# Now we print the data
print(json.dumps(json_data, indent=4))


In the previous example, you could see that the API returns text, which if passed through ``json.loads``
transforms into a Python dictionary. However, it is not entirely readable. This can be solved with
``json.dumps``.

.. activecode:: ac_l45_3b_en
:nocodelens:
:language: python3
:python3_interpreter: brython

Expand All @@ -60,7 +66,8 @@ transforms into a Python dictionary. However, it is not entirely readable. This
import json

api_url = "https://tastedive.com/api/similar?"
proxy = "https://cors.bridged.cc/"
proxy = "https://api.allorigins.win/raw?url="

# The following line is for the url parameters
parameters = urllib.parse.urlencode({"q": "coldplay"})

Expand Down Expand Up @@ -107,7 +114,7 @@ The following exercise comes with automatic grading.
import json

api_url = "https://tastedive.com/api/similar"
proxy = "https://cors.bridged.cc/"
proxy = "https://api.allorigins.win/raw?url="

# Add the parameters
parameters = {}
Expand Down Expand Up @@ -140,8 +147,8 @@ The following exercise comes with automatic grading.
def testOne(self):
self.assertEqual(
request_url,
"https://cors.bridged.cc/https://tastedive.com/api/similar?q=Coco&limit=5&info=1",
"Testing that the url is: https://cors.bridged.cc/https://tastedive.com/api/similar?q=Coco&limit=5&info=1",
"https://api.allorigins.win/raw?url=https://tastedive.com/api/similar?q=Coco&limit=5&info=1",
"Testing that the url is: https://api.allorigins.win/raw?url=https://tastedive.com/api/similar?q=Coco&limit=5&info=1",
)
self.assertEqual(results, 5, "Testing that results is assigned correctly.")
self.assertEqual(len(similar_movies), 5, "Testing that similar_movies are: 5")
Expand Down
5 changes: 3 additions & 2 deletions _sources/lectures/TWP45/TWP45_4.rst
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ Probando la API de University Domains
contiene dominios, nombres y países de la mayoría de las universidades del mundo.

.. activecode:: ac_l45_4a
:nocodelens:
:language: python3
:python3_interpreter: brython

Expand All @@ -16,7 +17,7 @@ contiene dominios, nombres y países de la mayoría de las universidades del mu
import urllib.parse
import json

api_url = "https://cors.bridged.cc/http://universities.hipolabs.com/search?"
api_url = "http://universities.hipolabs.com/search?"
parametros = urllib.parse.urlencode({"name": "middle", "country": "turkey"})

solicitud = urllib.request.urlopen(api_url + parametros)
Expand All @@ -35,7 +36,7 @@ contiene dominios, nombres y países de la mayoría de las universidades del mu
import requests
import json

api_url = "https://cors.bridged.cc/http://universities.hipolabs.com/search"
api_url = "http://universities.hipolabs.com/search?"
parametros = {"country": "colombia"}

solicitud = requests.get(api_url, params=parametros)
Expand Down
5 changes: 3 additions & 2 deletions _sources/lectures/TWP45/TWP45_4_en.rst
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ Testing the University Domains API
domains, names, and countries of most universities around the world.

.. activecode:: ac_l45_4a_en
:nocodelens:
:language: python3
:python3_interpreter: brython

Expand All @@ -16,7 +17,7 @@ domains, names, and countries of most universities around the world.
import urllib.parse
import json

api_url = "https://cors.bridged.cc/http://universities.hipolabs.com/search?"
api_url = "http://universities.hipolabs.com/search?"
params = urllib.parse.urlencode({"name": "middle", "country": "turkey"})

request = urllib.request.urlopen(api_url + params)
Expand All @@ -35,7 +36,7 @@ domains, names, and countries of most universities around the world.
import requests
import json

api_url = "https://cors.bridged.cc/http://universities.hipolabs.com/search"
api_url = "http://universities.hipolabs.com/search?"
params = {"country": "colombia"}

request = requests.get(api_url, params=params)
Expand Down
Loading