private const string eansearch_token = "xxxx"; //Register and get token here: https://www.ean-search.org/ean-database-api.html public EANInfo GetEANInfo(string path) { string format = string.Empty; string code = string.Empty; Zebra.GetBarcodeFromImageEmgu(path, out format, out code); string jsondata = string.Empty; EANInfo[] searchresults = null; // new EANInfo(); EANInfo root = new EANInfo(); if (code != string.Empty) { try { using (WebClient client = new WebClient()) { client.Encoding = Encoding.UTF8; jsondata = client.DownloadString("https://api.ean-search.org/api?token=" + eansearch_token + "&op=barcode-lookup&format=json&ean=" + code); //jsondata = "[{\"ean\":\"7391772321138\",\"name\":\"Mamma Mu & Kråkan (Import)\"}]\n"; //jsondata = "[{\"error\": \"Invalid barcode\"}]\n"; } JavaScriptSerializer serializer = new JavaScriptSerializer(); searchresults = serializer.Deserialize <EANInfo[]>(jsondata); if (searchresults.Length > 0) { root = searchresults[0]; root.ErrorMessage = ""; } else { root.ErrorMessage = "Could not find EAN information (no searchresults)"; } if (!string.IsNullOrEmpty(root.error)) { root.ErrorMessage = "Could not find EAN information. " + root.error; } } catch (Exception ex) { root.ErrorMessage = "Could not find EAN information. " + ex.Message; } } else { root.ErrorMessage = "Could not find barcode"; } return(root); }
public BookInfo GetBookInfo(string path) { string format = string.Empty; string code = string.Empty; Zebra.GetBarcodeFromImageEmgu(path, out format, out code); string jsondata = string.Empty; LibrisInfo root = null; BookInfo book = new BookInfo(); if (code != string.Empty) { try { using (WebClient client = new WebClient()) { client.Encoding = Encoding.UTF8; jsondata = client.DownloadString("http://libris.kb.se/xsearch?query=isbn:" + code + "&format=json"); } JavaScriptSerializer serializer = new JavaScriptSerializer(); root = serializer.Deserialize <LibrisInfo>(jsondata); if (root != null) { if (root.xsearch.list.Count > 0) { book.Title = root.xsearch.list[0].title; book.Author = root.xsearch.list[0].creator; book.Date = root.xsearch.list[0].date; book.Barcode = code; } } } catch (Exception ex) { book.ErrorMessage = "Could not find book information"; } } else { book.ErrorMessage = "Could not find barcode"; } book.ProductProperties = BookProperties(book); return(book); }
private const string discogs_token = "nruimgFCEQZXfqmqyFVXMrjKiIjoaPQwFvDNYaqe"; //Register and get token here: https://www.discogs.com/settings/developers public CDInfo GetCDInfo(string path) { string format = string.Empty; string code = string.Empty; Zebra.GetBarcodeFromImageEmgu(path, out format, out code); string jsondata = string.Empty; DiscogsInfo root = null; CDInfo cd = new CDInfo(); if (code != string.Empty) { try { using (WebClient client = new WebClient()) { client.Headers["User-Agent"] = "DitatUserAgent"; //If this header is omitted discogs throws protocol violation exception client.Encoding = Encoding.UTF8; jsondata = client.DownloadString("https://api.discogs.com/database/search?token=" + discogs_token + "&barcode=" + code); } JavaScriptSerializer serializer = new JavaScriptSerializer(); root = serializer.Deserialize <DiscogsInfo>(jsondata); if ((root != null) && (root.results.Count > 0)) { cd.Title = root.results[0].title; cd.Year = root.results[0].year; cd.Barcode = code; } } catch (Exception ex) { cd.ErrorMessage = "Could not find CD information"; } } else { cd.ErrorMessage = "Could not find barcode"; } cd.ProductProperties = CDProperties(cd); return(cd); }