namespace CoverSources import System import System.Xml import System.Drawing import System.Text import System.Text.RegularExpressions import util class Discogs: static ThumbSize = Size(150, 150) static SourceName as string: get: return "Discogs" static SourceVersion as decimal: get: return 0.1 static def GetThumbs(coverart,artist,album): query as string = artist + " " + album query.Replace(' ','+') obidResults = GetPage(String.Format("http://www.discogs.com/search?type=all&q={0}", EncodeUrl(query))) //Get obids obidRegex = Regex("\\d+)\">]+>[^-]+- (?:|(?[^<-]+))+", RegexOptions.Multiline) obidMatches = obidRegex.Matches(obidResults) coverart.SetCountEstimate(obidMatches.Count) //Probably more than this, as some releases might have multiple images for obidMatch as Match in obidMatches: //Construct the release name by joining up all the captures of the "name" group releaseNameBuilder = StringBuilder() for namePart in obidMatch.Groups["name"].Captures: releaseNameBuilder.Append(namePart) releaseName = releaseNameBuilder.ToString() //Get the image results imageResults = GetPage(String.Format("http://www.discogs.com/viewimages?what=R&obid={0}&showpending=1", obidMatch.Groups["obid"].Value)) imageRegex = Regex("http://www\\.discogs\\.com/image/R-\\d+-\\d+.jpe?g)\" width=\"(?\\d+)\" height=\"(?\\d+)\"") imageMatches = imageRegex.Matches(imageResults) for imageMatch as Match in imageMatches: large = System.Drawing.Bitmap.FromStream(GetPageStream(imageMatch.Groups["url"].Value)) caption = System.String.Format("{0} x {1}", large.Width, large.Height) thumb = Bitmap(large, ThumbSize) large.Dispose() g = Graphics.FromImage(thumb) f = Font(SystemFonts.DefaultFont, FontStyle.Bold) g.DrawString(caption, f, Brushes.White, 1,1 ) g.DrawString(caption, f, Brushes.Black, 0,0 ) f.Dispose() g.Dispose() coverart.AddThumb(thumb, releaseName, Int32.Parse(imageMatch.Groups["width"].Value), Int32.Parse(imageMatch.Groups["height"].Value), imageMatch.Groups["url"].Value) static def GetResult(param): return param