I've been trying to make a small app using the Discogs API for personal use in order to search individual artists and their albums, which worked so far using an unofficial Discogs C# app. Now the issue is that the only way I can extract the tracklist of an album is using the resource URL i get from each album request (for instance, https://api.discogs.com/releases/2890373).
I tried to extract the .json from each URL and I keep getting a ResponseLine error even with appropriate headers.
Added an Authorization header with my consumer key and secret key, lke this:
httpWebRequest.Headers.Add("Authorization", "Discogs key=xxx, secret=yyy");
...added a UserAgent and it still does not work.
I've tried the same in Python and it worked perfectly, but I don't want to run a Python app each time I want info about an album.
private void scrape_button_Click(object sender, EventArgs e) {
HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create("https://api.discogs.com/releases/2890373");
httpWebRequest.Method = WebRequestMethods.Http.Get;
httpWebRequest.Timeout = 12000;
httpWebRequest.ContentType = "application/vnd.discogs.v2.html+json";
httpWebRequest.Headers.Add("UserAgent", "matija_search/0.1");
string file;
var response = (HttpWebResponse)httpWebRequest.GetResponse();
using(var sr = new StreamReader(response.GetResponseStream())) {
file = sr.ReadToEnd();
}
}
This is the button that tries to get the data.
import requests
import json
info = requests.get('https://api.discogs.com/releases/2890373')
data = info.json()
with open('data.json', 'w') as f:
json.dump(data, f)
This is a python equivalent which actually works...
Alright, solved the issue. I did not add the UserAgent in the correct way. Fixed code is as follows:
private void scrape_button_Click(object sender, EventArgs e) {
HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create("https://api.discogs.com/releases/2890373");
httpWebRequest.Method = WebRequestMethods.Http.Get;
httpWebRequest.Accept = "application/vnd.discogs.v2.html+json";
httpWebRequest.UserAgent = "matija_search/1";
string file;
var response = (HttpWebResponse)httpWebRequest.GetResponse();
using(var sr = new StreamReader(response.GetResponseStream())) {
file = sr.ReadToEnd();
}
var contentsToWriteToFile = JsonConvert.SerializeObject(file);
TextWriter writer = new StreamWriter("test.json", false);
writer.Write(contentsToWriteToFile);
}