Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import System
- import System.IO
- import System.Drawing
- import System.Drawing.Imaging
- import System.Text.RegularExpressions
- import System.Threading
- import AlbumArtDownloader.Scripts
- import util
- class Result:
- public ImageStream as Stream
- public Name as string
- public InfoUri as string
- public Size as int
- class BuyDotCom(AlbumArtDownloader.Scripts.IScript):
- Name as string:
- get: return "Buy.com"
- Version as string:
- get: return "0.8"
- Author as string:
- get: return "alsaan, DRata, Alex Vallat"
- m_Results as Queue[of Result]
- m_ItemMatches as Queue[of Match]
- m_NumberOfThreads = 4
- def Search(artist as string, album as string, results as IScriptResults):
- m_Results = Queue[of Result]()
- m_ItemMatches = Queue[of Match]()
- artist = StripCharacters("&.'\";:?!", artist)
- album = StripCharacters("&.'\";:?!", album)
- searchParameter = EncodeUrl(artist + " " + album)
- //Retrieve the search results page
- searchResultsHtml as string = GetPage("http://www.buy.com/sr/srajax.aspx?qu=${searchParameter}&from=2&sid=6&page=1&pv=1")
- //Check whether we actually got any relevant result or not
- if(searchResultsHtml.IndexOf("did not return an exact match") > -1):
- return
- //Remove "Results ... in All Categories"
- allCategories = searchResultsHtml.IndexOf(" in all categories ")
- if(allCategories > -1):
- searchResultsHtml = searchResultsHtml.Substring(0, allCategories)
- //Remove "Similar Products in General"
- similar = searchResultsHtml.IndexOf(">Similar Products in <span class=\"searchHeaderTerm\">general<")
- if(similar > -1):
- searchResultsHtml = searchResultsHtml.Substring(0, similar)
- //Extract all the thumbnails and the links to product pages
- itemsRegex = Regex("<a href=\"(?<productPageUrl>[^\"]*/(?<sku>[^\"]*)\\.html)\"[^>]*><img[^>]*src=\"(?<thumbnailUrl>[^\"]*)\"[^>]*title=\"(?<title>[^\"]*)\"[^>]")
- itemsMatches = itemsRegex.Matches(searchResultsHtml)
- //Set the estimated number of covers available (approx. 1 cover per product page)
- results.EstimatedCount = itemsMatches.Count
- for itemMatch in itemsMatches:
- m_ItemMatches.Enqueue(itemMatch)
- for i in range(0, m_NumberOfThreads):
- AddThumbnails.BeginInvoke()
- i = 0
- while i < itemsMatches.Count:
- Monitor.Enter(m_Results)
- if m_Results.Count == 0:
- Monitor.Wait(m_Results)
- r = m_Results.Dequeue()
- if r != null:
- results.Add(r.ImageStream, r.Name, r.InfoUri, r.Size, r.Size, null, CoverType.Front)
- Monitor.Exit(m_Results)
- i++
- def AddThumbnails():
- while true:
- Monitor.Enter(m_ItemMatches)
- if m_ItemMatches.Count == 0:
- return
- itemMatch = m_ItemMatches.Dequeue()
- Monitor.Exit(m_ItemMatches)
- AddThumbnail(itemMatch)
- def AddThumbnail(itemMatch as Match):
- title = itemMatch.Groups["title"].Value
- productPageUrl = "http://www.buy.com" + itemMatch.Groups["productPageUrl"].Value
- productNumber = itemMatch.Groups["sku"].Value
- thumbnailUrl = itemMatch.Groups["thumbnailUrl"].Value
- if thumbnailUrl.EndsWith(".gif"):
- //Any gif file is a placeholder for when there is no image available
- r = null
- else:
- imageUrl = String.Format("http://ak.buy.com/PI/0/500/{0}.jpg", productNumber)
- image = DownloadBitmap(imageUrl)
- size = 500
- frameSize = GetFrameSize(image)
- if frameSize > 125:
- size = 125
- image = CropBitmap(image, 125)
- elif frameSize > 0:
- size = 250
- image = CropBitmap(image, 250)
- r = Result()
- r.ImageStream = ConvertImageToStream(image)
- r.Name = title
- r.InfoUri = productPageUrl
- r.Size = size
- Monitor.Enter(m_Results)
- m_Results.Enqueue(r)
- Monitor.Pulse(m_Results)
- Monitor.Exit(m_Results)
- def GetFrameSize(bitmap):
- imageSize = bitmap.Width
- imageMiddle as int = imageSize / 2
- frameSize = 0
- whiteArgb = Color.White.ToArgb()
- for i in range(0, 130):
- frameSize = i
- color = bitmap.GetPixel(i, imageMiddle)
- if color.ToArgb() != whiteArgb:
- break
- color = bitmap.GetPixel(imageMiddle,i)
- if color.ToArgb() != whiteArgb:
- break
- color = bitmap.GetPixel(imageSize - i - 1,imageMiddle)
- if color.ToArgb() != whiteArgb:
- break
- color = bitmap.GetPixel(imageMiddle, imageSize - i - 1)
- if color.ToArgb() != whiteArgb:
- break
- return frameSize
- def CropBitmap(bitmap, size):
- x as int = ((bitmap.Width / 2) - (size / 2))
- y as int = ((bitmap.Width / 2) - (size / 2))
- w = size
- h = size
- croppedBitmap = bitmap.Clone(Rectangle(x, y, w, h), bitmap.PixelFormat)
- bitmap.Dispose()
- return croppedBitmap
- def ConvertImageToStream(image):
- stream = System.IO.MemoryStream()
- image.Save(stream, ImageFormat.Jpeg)
- stream.Seek(0, SeekOrigin.Begin)
- return stream
- def DownloadBitmap(url) as Bitmap:
- sourceStream = null
- try:
- request = System.Net.HttpWebRequest.Create(url) as System.Net.HttpWebRequest
- sourceStream = request.GetResponse().GetResponseStream()
- return Bitmap(sourceStream as Stream)
- ensure:
- if sourceStream != null:
- sourceStream.Dispose()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement