Spotify: Search for Artist and Title at the same time - spotify

I am new to the Spotify API. I am trying to figure out how to search for an artist and a title at the same time.
I'm using an AJAX call with both artist and title. If either is blank, I can simply search for the other, but I can not seem to figure out how to search for the artist and title at the same time. The purpose of this is to narrow down the result to find a specific track.
Here's what I have:
header('Content-Type: application/json');
$search_artist = rawurlencode($_POST['search_artist']);
$search_title = rawurlencode($_POST['search_title']);
if($search_artist && !$search_title) {
//Search for Artist only
$url = "https://api.spotify.com/v1/search?q=*".$search_artist."*&type=artist";
} else if(!$search_artist && $search_title) {
//Search for title only
$url = "https://api.spotify.com/v1/search?q=*".$search_title."*&type=track";
} else if($search_artist && $search_title) {
//Search for Artist AND Title
$url = "https://api.spotify.com/v1/search?q=*".$search_artist."*&type=artist&q=*".$search_title."*&type=track";
}
$contents = file_get_contents($url);
echo json_encode($contents);
As mentioned, if I search for only artist or only title, it works just fine. I just don't know how to search for both at the same time using Spotify's API.

If I rephrase your question it sounds like:
If only search_artist is filled in, you want to the find an artist
If only search_track is filled in, you want to find a track
If both are filled in, you still want to find a track
Then the type should just be track.
The documentation also doesn't mention that you can have q and type parameters twice. https://developer.spotify.com/web-api/search-item/ It does mention you can search for tracks and artist by having them comma-separated, type=track,artist but reading your question this is probably not what you want.
$url = "https://api.spotify.com/v1/search?q=".$search_artist."+".$search_title."&type=track";

Related

Trying to check to see if something exists and if not, assign something different to it with JavaScript

Wondering if someone can help, I'll try to explain the best I can. I am looking at a json file for various steam games. Basically what I want to do is check to see if a certain "key(?)" is present and if not then return "None".
e.g. Check to see if body[id].data.metacritic and if it does then assign score to body[id].data.metacritic.score. The same with URL. I cannot figure this out!
I have tried the following:
if(bulk.metacritic) var { score, url } = bulk.metacritic[0] || "None";
I just can't figure out how to get this right! Basically, body[id].data.metacritic doesn't exist in all json files, along with other parameters so I just want to display some placeholder text if they don't appear.
You can check if the body[id].data.metacritic is an array with a non-zero length and if so, use a value from it, otherwise use default values:
let score, url;
let data = body[id].data;
if (data && Array.isArray(data.metacritic) && data.metacritic.length > 0) {
score = data.metacritic[0].score;
url = data.metacritic[0].url;
} else {
score = url = "None";
}
console.log(score, url);

Get element Id from a found element

I'm using the Chrome driver and Selenium tools in my CodedUI tests. I can find the element I need using the SearchProperties and a Contains operator however I need the full Id for subsequent searches.
For example I need to find an input element with Id "pm_modal_28".
This is easy enough by doing a search where Id contains "pm_modal".
I then need to parse the value "28" out of the Id that was found so I can search for the next nested element which has an Id of "dp_28".
When I use the Id property of HtmlDiv I get a NotSupportedException. Is there anyway I can get all of the Html attributes from an Element or get the Id from an element after it has been found?
Not sure if this what you are after, once the control is identified, you would have all its properties to play around with.
For example
var control = new HtmlDiv ();
control.SearchProperties.Add("Id", "MyDiv_28");
if (!control.TryFind()) return;
var newControl = new HtmlDiv();
newControl.SearchProperties.Add("Id", control.Id.Split('_')[1]);
newControl.TryFind();
HtmlDiv myDiv = new HtmlDiv(browser);
//Add the search logic u want !
myDiv.SearchProperties.Add("class", "ClassName");
string onewayforID = myDiv.Id;
string anotherWay = myDiv.GetProperty(HtmlDiv.PropertyNames.Id).ToString(); // Or u can simpy pass "Id"
See if that Works !

Extracting all text from a website to build a concordance

How can I grab all the text in a website, and I don't just mean ctrl+a/c. I'd like to be able to extract all the text from a website (and all the pages associated) and use it to build a concordance of words from that site. Any ideas?
I was intrigued by this so I've written the first part of a solution to this.
The code is written in PHP because of the convenient strip_tags function. It's also rough and procedural but I feel in demonstrates my ideas.
<?php
$url = "http://www.stackoverflow.com";
//To use this you'll need to get a key for the Readabilty Parser API http://readability.com/developers/api/parser
$token = "";
//I make a HTTP GET request to the readabilty API and then decode the returned JSON
$parserResponse = json_decode(file_get_contents("http://www.readability.com/api/content/v1/parser?url=$url&token=$token"));
//I'm only interested in the content string in the json object
$content = $parserResponse->content;
//I strip the HTML tags for the article content
$wordsOnPage = strip_tags($content);
$wordCounter = array();
$wordSplit = explode(" ", $wordsOnPage);
//I then loop through each word in the article keeping count of how many times I've seen the word
foreach($wordSplit as $word)
{
incrementWordCounter($word);
}
//Then I sort the array so the most frequent words are at the end
asort($wordCounter);
//And dump the array
var_dump($wordCounter);
function incrementWordCounter($word)
{
global $wordCounter;
if(isset($wordCounter[$word]))
{
$wordCounter[$word] = $wordCounter[$word] + 1;
}
else
{
$wordCounter[$word] = 1;
}
}
?>
I needed to do this to configure PHP for the SSL the readability API uses.
The next step in the solution would be too search for links in the page and call this recursively in an intelligent way to hance the associated pages requirement.
Also the code above just gives the raw data of a word-count you would want to process it some more to make it meaningful.

Flickr api doesn't return the estimated value

I am using flickr api in order to count the how many times a tag occur. I want this information in order to calculate the Normalized Google Distance. I am using this query in my java code:
http://api.flickr.com/services/rest/?method=flickr.photos.search&api_key=XXXXXXX&format=json&tags=bank
But i don't get good results. For example when i search "bank" the count value is 357439, when i search "credit" the count value is 59288, but when i am search for "bank credit" the count value is only 2. When i searching with the search box at flickr.com for "bank credit" i get a lot of results. But as far as i can see the query it uses is
http://www.flickr.com/search/?q=bank%20credit
which i am not able to use through my java code. I am trying to pass this
http://api.flickr.com/services/rest/?method=flickr.photos.search&api_key=XXXXXXX&format=json&q=bank
and it says
Parameterless searches have been disabled. Please use flickr.photos.getRecent instead
How can i solve this problem?
Your generated url is incorrect
http://api.flickr.com/services/rest/method=flickr.photos.search&api_key=XXXXXXX&format=json&q=bank
is missing the question mark
http://api.flickr.com/services/rest/?method=flickr.photos.search&api_key=XXXXXXX&format=json&q=bank
UPDATE based on OP comments:
I didn't see you had the question mark on the top url string. Looking at it again, I did realize you are not passing in any valid parameters. "q" isn't one of the listed parameters on the search api page. Try something like below to search photos with "bank" tag
http://api.flickr.com/services/rest/?method=flickr.photos.search&api_key=XXXXXXX&format=json&tags=bank
or one with bank in description/title
http://api.flickr.com/services/rest/?method=flickr.photos.search&api_key=XXXXXXX&format=json&text=bank
I got the same error. but when I added media = photos in the parameters, it got resolved.
e.g. :
baseurl = "https://api.flickr.com/services/rest/"
params_d['api_key'] = 'XXXXXXX'
params_d['method'] = 'flickr.photos.search'
params_d['tag'] = "river,mountains"
params_d['tag_mode'] = 'all'
params_d['per_page'] = 5
params_d['media'] = "photos"
params_d['nojsoncallback'] = 1
params_d['format'] = 'json'
resp = requests.get(baseurl, params = params_d)

url segments based redirect in modx Evo

let's say you have a url like this entered in modx Evo
www.zipit.com.org/reference/toamovie/
if I have a page called toamovie whose parent is called reference
but when someone enters that url I want it to do the equivalent of this
www.zipit.com.org/reference.html?myvar=toamovie
additionally, or more importantly,
I'd like the result to be more like this, where 12 wouls be the id of a document
`www.zipit.com.org/reference.html?myid=12'
I'm wondering if this is at all possible with modx Evolution.I'm thinking that this should be possible to do with some htaccess magic, well the first part anyway. How could I get a value that the document? This would be quite inaccessible with htaccess, so there would need to be another part to it that could plug into the database and get that value.
It should be possible with a custom plugin called on the "OnPageNotFound" event.
<?php
$url = $_SERVER['REQUEST_URI']; //get the url
//split the url into pieces and remove empty values
$pieces = array_filter(explode('/', $url));
//reset the array keys
$temp = array();
foreach ($pieces as $piece) {
$temp[] = $piece;
}
$pieces = $temp;
//basic checking of the url
if (count($pieces) == 2) {
$modx->sendRedirect('http://www.zipit.org/' . $pieces[0] .".html?myid=" . $pieces[1]);
}
?>
remember that plugins aren't wrapped in php tags.
also that this script will pass any incorrect urls to zipit.org. Some error handling is probably desireable.

Resources