|
| 1 | +#!/usr/bin/env python3 -tt |
| 2 | +import numpy as np |
| 3 | +import requests |
| 4 | +import json |
| 5 | +from scipy.io import wavfile |
| 6 | +from io import BytesIO |
| 7 | + |
| 8 | +BASE_URL = 'https://stanfordpython.com/row-of-puzzles/audio/' |
| 9 | +def get_url_data(url): |
| 10 | + """Queries the url, parses the data, and returns |
| 11 | + that information in a dict object. |
| 12 | +
|
| 13 | + Arguments: |
| 14 | + url -- The url to query. |
| 15 | + """ |
| 16 | + raise NotImplementedError |
| 17 | + |
| 18 | +def obtain_matrix(textfile_url): |
| 19 | + """ |
| 20 | + This function obtains a NumPy matrix from the url at which it is located. |
| 21 | +
|
| 22 | + We can use the the np.genfromtxt file to generate a NumPy matrix from |
| 23 | + bytes-data obtained from a url. Therefore, this function must access |
| 24 | + https://www.stanfordpython.com/row-of-puzzles/audio/textfile_url.txt, |
| 25 | + and pass the bytes-data from the content of the request into the |
| 26 | + np.genfromtxt function to reconstruct the matrix. |
| 27 | +
|
| 28 | + When encoding matrices as text files (which is what the course staff did |
| 29 | + before hiding those text files throughout the course website), NumPy |
| 30 | + allows the user to specify a matrix delimiter. In this case, we comma-delimited |
| 31 | + our matrices. Look into the np.genfromtxt documentation to explore how |
| 32 | + you might need to modify your np.genfromtxt function call to account for this |
| 33 | + information. |
| 34 | +
|
| 35 | + Arguments: |
| 36 | + textfile_url -- the location of the file on the Stanford Python website. E.g. if |
| 37 | + textfile_url were equal to 123abc.txt, the file would be located at |
| 38 | + https://stanfordpython.com/row-of-puzzles/audio/123abc.txt. |
| 39 | + """ |
| 40 | + |
| 41 | + raise NotImplementedError |
| 42 | + |
| 43 | + |
| 44 | +def parse_site(): |
| 45 | + """ |
| 46 | + This function parses the course site, starting at |
| 47 | + https://stanfordpython.com/row-of-puzzles/audio/audio_start.json, |
| 48 | + and following the chain of JSON files as described in the assignment |
| 49 | + specification. As it goes, it retrieves the audio matrix components |
| 50 | + from each JSON file (by calling obtain_matrix), then |
| 51 | + vertically concatenates them to obtain the decoded Nx2 audio matrix. |
| 52 | + The function returns the decoded Nx2 audio matrix. |
| 53 | +
|
| 54 | + Arguments: N/A |
| 55 | + """ |
| 56 | + raise NotImplementedError |
| 57 | + |
| 58 | +if __name__ == "__main__": |
| 59 | + wavfile.write("out.wav", 44100, parse_site()) |
| 60 | + |
| 61 | + |
0 commit comments