Troubleshooting user input for API scraping program
This program is supposed to output lyrics from a given API. This works when the song and artist are manually set at variables before pressing the submit button. However, I realize that it cannot work when taking user input for song because the JSON load is inside setup, which is loaded before the input is even given. How would I go about fixing this? I already tried to create another function for loadJSON, but it still was not working.
The expected output should be when you press submit, the user's song (I will deal with artist input later) lyrics should be produced. Thank you!
var search, button, question, input;
var artist = 'One%20Direction/';
var song = 'Taken';
var url_root = 'http://lyric-api.herokuapp.com/api/find/'
function setup() {
noCanvas();
input = createInput ();
input.position (20, 50);
button = createButton ("Submit");
button.position(input.x + input.width, 50);
button.mousePressed(goSearch);
var url = url_root + artist + song;
loadJSON (url, gotData);
question = createElement ('h2', 'Search a song:');
question.position(20, 5);
}
function gotData(data) {
search = data;
}
function goSearch() {
var thing = search.lyric
var result = createP(thing);
result.position (20, 60);
}
javascript p5.js
add a comment |
This program is supposed to output lyrics from a given API. This works when the song and artist are manually set at variables before pressing the submit button. However, I realize that it cannot work when taking user input for song because the JSON load is inside setup, which is loaded before the input is even given. How would I go about fixing this? I already tried to create another function for loadJSON, but it still was not working.
The expected output should be when you press submit, the user's song (I will deal with artist input later) lyrics should be produced. Thank you!
var search, button, question, input;
var artist = 'One%20Direction/';
var song = 'Taken';
var url_root = 'http://lyric-api.herokuapp.com/api/find/'
function setup() {
noCanvas();
input = createInput ();
input.position (20, 50);
button = createButton ("Submit");
button.position(input.x + input.width, 50);
button.mousePressed(goSearch);
var url = url_root + artist + song;
loadJSON (url, gotData);
question = createElement ('h2', 'Search a song:');
question.position(20, 5);
}
function gotData(data) {
search = data;
}
function goSearch() {
var thing = search.lyric
var result = createP(thing);
result.position (20, 60);
}
javascript p5.js
your not showing the code for loadJson, and its not clear what your asking, perhaps add some more detail
– saj
Jan 1 at 2:58
add a comment |
This program is supposed to output lyrics from a given API. This works when the song and artist are manually set at variables before pressing the submit button. However, I realize that it cannot work when taking user input for song because the JSON load is inside setup, which is loaded before the input is even given. How would I go about fixing this? I already tried to create another function for loadJSON, but it still was not working.
The expected output should be when you press submit, the user's song (I will deal with artist input later) lyrics should be produced. Thank you!
var search, button, question, input;
var artist = 'One%20Direction/';
var song = 'Taken';
var url_root = 'http://lyric-api.herokuapp.com/api/find/'
function setup() {
noCanvas();
input = createInput ();
input.position (20, 50);
button = createButton ("Submit");
button.position(input.x + input.width, 50);
button.mousePressed(goSearch);
var url = url_root + artist + song;
loadJSON (url, gotData);
question = createElement ('h2', 'Search a song:');
question.position(20, 5);
}
function gotData(data) {
search = data;
}
function goSearch() {
var thing = search.lyric
var result = createP(thing);
result.position (20, 60);
}
javascript p5.js
This program is supposed to output lyrics from a given API. This works when the song and artist are manually set at variables before pressing the submit button. However, I realize that it cannot work when taking user input for song because the JSON load is inside setup, which is loaded before the input is even given. How would I go about fixing this? I already tried to create another function for loadJSON, but it still was not working.
The expected output should be when you press submit, the user's song (I will deal with artist input later) lyrics should be produced. Thank you!
var search, button, question, input;
var artist = 'One%20Direction/';
var song = 'Taken';
var url_root = 'http://lyric-api.herokuapp.com/api/find/'
function setup() {
noCanvas();
input = createInput ();
input.position (20, 50);
button = createButton ("Submit");
button.position(input.x + input.width, 50);
button.mousePressed(goSearch);
var url = url_root + artist + song;
loadJSON (url, gotData);
question = createElement ('h2', 'Search a song:');
question.position(20, 5);
}
function gotData(data) {
search = data;
}
function goSearch() {
var thing = search.lyric
var result = createP(thing);
result.position (20, 60);
}
javascript p5.js
javascript p5.js
asked Jan 1 at 2:07
lydanglydang
1
1
your not showing the code for loadJson, and its not clear what your asking, perhaps add some more detail
– saj
Jan 1 at 2:58
add a comment |
your not showing the code for loadJson, and its not clear what your asking, perhaps add some more detail
– saj
Jan 1 at 2:58
your not showing the code for loadJson, and its not clear what your asking, perhaps add some more detail
– saj
Jan 1 at 2:58
your not showing the code for loadJson, and its not clear what your asking, perhaps add some more detail
– saj
Jan 1 at 2:58
add a comment |
1 Answer
1
active
oldest
votes
I believe the following provides what you're after. In summary:
- Addition of
input.value()
to get user's input value - Minor refactoring of functions
var search, button, question, input;
var artist = 'One%20Direction/';
var url_root = 'http://lyric-api.herokuapp.com/api/find/'
// Page setup
function setup() {
noCanvas();
input = createInput ();
input.position (20, 50);
button = createButton ("Submit");
button.position(input.x + input.width, 50);
button.mousePressed(goSearch);
question = createElement ('h2', 'Search a song:');
question.position(20, 5);
}
// When data is returned, lets display it
function gotData(data) {
search = data;
var thing = search.lyric
var result = createP(thing);
result.position (20, 60);
}
// Perform request when user submits search term
function goSearch() {
var song = input.value(); // Get the form field's input value
var url = url_root + artist + song;
loadJSON (url, gotData);
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.7.2/p5.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.7.2/addons/p5.dom.js"></script>
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53992629%2ftroubleshooting-user-input-for-api-scraping-program%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
I believe the following provides what you're after. In summary:
- Addition of
input.value()
to get user's input value - Minor refactoring of functions
var search, button, question, input;
var artist = 'One%20Direction/';
var url_root = 'http://lyric-api.herokuapp.com/api/find/'
// Page setup
function setup() {
noCanvas();
input = createInput ();
input.position (20, 50);
button = createButton ("Submit");
button.position(input.x + input.width, 50);
button.mousePressed(goSearch);
question = createElement ('h2', 'Search a song:');
question.position(20, 5);
}
// When data is returned, lets display it
function gotData(data) {
search = data;
var thing = search.lyric
var result = createP(thing);
result.position (20, 60);
}
// Perform request when user submits search term
function goSearch() {
var song = input.value(); // Get the form field's input value
var url = url_root + artist + song;
loadJSON (url, gotData);
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.7.2/p5.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.7.2/addons/p5.dom.js"></script>
add a comment |
I believe the following provides what you're after. In summary:
- Addition of
input.value()
to get user's input value - Minor refactoring of functions
var search, button, question, input;
var artist = 'One%20Direction/';
var url_root = 'http://lyric-api.herokuapp.com/api/find/'
// Page setup
function setup() {
noCanvas();
input = createInput ();
input.position (20, 50);
button = createButton ("Submit");
button.position(input.x + input.width, 50);
button.mousePressed(goSearch);
question = createElement ('h2', 'Search a song:');
question.position(20, 5);
}
// When data is returned, lets display it
function gotData(data) {
search = data;
var thing = search.lyric
var result = createP(thing);
result.position (20, 60);
}
// Perform request when user submits search term
function goSearch() {
var song = input.value(); // Get the form field's input value
var url = url_root + artist + song;
loadJSON (url, gotData);
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.7.2/p5.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.7.2/addons/p5.dom.js"></script>
add a comment |
I believe the following provides what you're after. In summary:
- Addition of
input.value()
to get user's input value - Minor refactoring of functions
var search, button, question, input;
var artist = 'One%20Direction/';
var url_root = 'http://lyric-api.herokuapp.com/api/find/'
// Page setup
function setup() {
noCanvas();
input = createInput ();
input.position (20, 50);
button = createButton ("Submit");
button.position(input.x + input.width, 50);
button.mousePressed(goSearch);
question = createElement ('h2', 'Search a song:');
question.position(20, 5);
}
// When data is returned, lets display it
function gotData(data) {
search = data;
var thing = search.lyric
var result = createP(thing);
result.position (20, 60);
}
// Perform request when user submits search term
function goSearch() {
var song = input.value(); // Get the form field's input value
var url = url_root + artist + song;
loadJSON (url, gotData);
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.7.2/p5.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.7.2/addons/p5.dom.js"></script>
I believe the following provides what you're after. In summary:
- Addition of
input.value()
to get user's input value - Minor refactoring of functions
var search, button, question, input;
var artist = 'One%20Direction/';
var url_root = 'http://lyric-api.herokuapp.com/api/find/'
// Page setup
function setup() {
noCanvas();
input = createInput ();
input.position (20, 50);
button = createButton ("Submit");
button.position(input.x + input.width, 50);
button.mousePressed(goSearch);
question = createElement ('h2', 'Search a song:');
question.position(20, 5);
}
// When data is returned, lets display it
function gotData(data) {
search = data;
var thing = search.lyric
var result = createP(thing);
result.position (20, 60);
}
// Perform request when user submits search term
function goSearch() {
var song = input.value(); // Get the form field's input value
var url = url_root + artist + song;
loadJSON (url, gotData);
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.7.2/p5.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.7.2/addons/p5.dom.js"></script>
var search, button, question, input;
var artist = 'One%20Direction/';
var url_root = 'http://lyric-api.herokuapp.com/api/find/'
// Page setup
function setup() {
noCanvas();
input = createInput ();
input.position (20, 50);
button = createButton ("Submit");
button.position(input.x + input.width, 50);
button.mousePressed(goSearch);
question = createElement ('h2', 'Search a song:');
question.position(20, 5);
}
// When data is returned, lets display it
function gotData(data) {
search = data;
var thing = search.lyric
var result = createP(thing);
result.position (20, 60);
}
// Perform request when user submits search term
function goSearch() {
var song = input.value(); // Get the form field's input value
var url = url_root + artist + song;
loadJSON (url, gotData);
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.7.2/p5.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.7.2/addons/p5.dom.js"></script>
var search, button, question, input;
var artist = 'One%20Direction/';
var url_root = 'http://lyric-api.herokuapp.com/api/find/'
// Page setup
function setup() {
noCanvas();
input = createInput ();
input.position (20, 50);
button = createButton ("Submit");
button.position(input.x + input.width, 50);
button.mousePressed(goSearch);
question = createElement ('h2', 'Search a song:');
question.position(20, 5);
}
// When data is returned, lets display it
function gotData(data) {
search = data;
var thing = search.lyric
var result = createP(thing);
result.position (20, 60);
}
// Perform request when user submits search term
function goSearch() {
var song = input.value(); // Get the form field's input value
var url = url_root + artist + song;
loadJSON (url, gotData);
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.7.2/p5.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.7.2/addons/p5.dom.js"></script>
answered Jan 3 at 14:20
almcdalmcd
93121325
93121325
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53992629%2ftroubleshooting-user-input-for-api-scraping-program%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
your not showing the code for loadJson, and its not clear what your asking, perhaps add some more detail
– saj
Jan 1 at 2:58