Advertisement
Guest User

buy-com.boo

a guest
Sep 10th, 2011
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
BOO 5.23 KB | None | 0 0
  1. import System
  2. import System.IO
  3. import System.Drawing
  4. import System.Drawing.Imaging
  5. import System.Text.RegularExpressions
  6. import System.Threading
  7. import AlbumArtDownloader.Scripts
  8. import util
  9.  
  10. class Result:
  11.     public ImageStream as Stream
  12.     public Name as string
  13.     public InfoUri as string
  14.     public Size as int
  15.  
  16. class BuyDotCom(AlbumArtDownloader.Scripts.IScript):
  17.     Name as string:
  18.         get: return "Buy.com"
  19.     Version as string:
  20.         get: return "0.8"
  21.     Author as string:
  22.         get: return "alsaan, DRata, Alex Vallat"
  23.        
  24.     m_Results as Queue[of Result]
  25.     m_ItemMatches as Queue[of Match]
  26.     m_NumberOfThreads = 4
  27.  
  28.     def Search(artist as string, album as string, results as IScriptResults):
  29.    
  30.         m_Results = Queue[of Result]()
  31.         m_ItemMatches = Queue[of Match]()
  32.    
  33.         artist = StripCharacters("&.'\";:?!", artist)
  34.         album = StripCharacters("&.'\";:?!", album)
  35.        
  36.         searchParameter = EncodeUrl(artist + " " + album)
  37.              
  38.         //Retrieve the search results page
  39.         searchResultsHtml as string = GetPage("http://www.buy.com/sr/srajax.aspx?qu=${searchParameter}&from=2&sid=6&page=1&pv=1")
  40.        
  41.         //Check whether we actually got any relevant result or not
  42.         if(searchResultsHtml.IndexOf("did not return an exact match") > -1):
  43.             return
  44.  
  45.         //Remove "Results ... in All Categories"
  46.         allCategories = searchResultsHtml.IndexOf(" in all categories ")
  47.         if(allCategories > -1):
  48.             searchResultsHtml = searchResultsHtml.Substring(0, allCategories)
  49.  
  50.         //Remove "Similar Products in General"
  51.         similar = searchResultsHtml.IndexOf(">Similar Products in <span class=\"searchHeaderTerm\">general<")
  52.         if(similar > -1):
  53.             searchResultsHtml = searchResultsHtml.Substring(0, similar)
  54.        
  55.         //Extract all the thumbnails and the links to product pages
  56.         itemsRegex = Regex("<a href=\"(?<productPageUrl>[^\"]*/(?<sku>[^\"]*)\\.html)\"[^>]*><img[^>]*src=\"(?<thumbnailUrl>[^\"]*)\"[^>]*title=\"(?<title>[^\"]*)\"[^>]")
  57.         itemsMatches = itemsRegex.Matches(searchResultsHtml)
  58.        
  59.         //Set the estimated number of covers available (approx. 1 cover per product page)
  60.         results.EstimatedCount = itemsMatches.Count
  61.        
  62.         for itemMatch in itemsMatches:
  63.             m_ItemMatches.Enqueue(itemMatch)
  64.        
  65.         for i in range(0, m_NumberOfThreads):
  66.             AddThumbnails.BeginInvoke()
  67.                            
  68.         i = 0
  69.         while i < itemsMatches.Count:
  70.        
  71.             Monitor.Enter(m_Results)
  72.            
  73.             if m_Results.Count == 0:
  74.                 Monitor.Wait(m_Results)
  75.                
  76.             r = m_Results.Dequeue()        
  77.             if r != null:
  78.                 results.Add(r.ImageStream, r.Name, r.InfoUri, r.Size, r.Size, null, CoverType.Front)
  79.            
  80.             Monitor.Exit(m_Results)
  81.            
  82.             i++
  83.        
  84.     def AddThumbnails():
  85.        
  86.         while true:
  87.        
  88.             Monitor.Enter(m_ItemMatches)
  89.            
  90.             if m_ItemMatches.Count == 0:
  91.                 return             
  92.             itemMatch = m_ItemMatches.Dequeue()
  93.            
  94.             Monitor.Exit(m_ItemMatches)
  95.            
  96.             AddThumbnail(itemMatch)
  97.                                    
  98.     def AddThumbnail(itemMatch as Match):
  99.        
  100.         title = itemMatch.Groups["title"].Value
  101.         productPageUrl = "http://www.buy.com" + itemMatch.Groups["productPageUrl"].Value
  102.         productNumber = itemMatch.Groups["sku"].Value
  103.         thumbnailUrl = itemMatch.Groups["thumbnailUrl"].Value
  104.        
  105.         if thumbnailUrl.EndsWith(".gif"):
  106.        
  107.             //Any gif file is a placeholder for when there is no image available
  108.             r = null
  109.            
  110.         else:
  111.        
  112.             imageUrl = String.Format("http://ak.buy.com/PI/0/500/{0}.jpg", productNumber)
  113.            
  114.             image = DownloadBitmap(imageUrl)
  115.                            
  116.             size = 500
  117.             frameSize = GetFrameSize(image)
  118.             if frameSize > 125:
  119.                 size = 125
  120.                 image = CropBitmap(image, 125)         
  121.             elif frameSize > 0:
  122.                 size = 250
  123.                 image = CropBitmap(image, 250)     
  124.            
  125.             r = Result()
  126.             r.ImageStream = ConvertImageToStream(image)
  127.             r.Name = title
  128.             r.InfoUri = productPageUrl
  129.             r.Size = size
  130.        
  131.         Monitor.Enter(m_Results)
  132.        
  133.         m_Results.Enqueue(r)
  134.         Monitor.Pulse(m_Results)
  135.        
  136.         Monitor.Exit(m_Results)
  137.        
  138.     def GetFrameSize(bitmap):
  139.    
  140.         imageSize = bitmap.Width
  141.         imageMiddle as int = imageSize / 2
  142.         frameSize = 0
  143.         whiteArgb = Color.White.ToArgb()
  144.        
  145.         for i in range(0, 130):
  146.        
  147.             frameSize = i
  148.            
  149.             color = bitmap.GetPixel(i, imageMiddle)
  150.             if color.ToArgb() != whiteArgb:
  151.                 break
  152.                
  153.             color = bitmap.GetPixel(imageMiddle,i)
  154.             if color.ToArgb() != whiteArgb:
  155.                 break
  156.                
  157.             color = bitmap.GetPixel(imageSize - i - 1,imageMiddle)
  158.             if color.ToArgb() != whiteArgb:
  159.                 break
  160.                
  161.             color = bitmap.GetPixel(imageMiddle, imageSize - i - 1)
  162.             if color.ToArgb() != whiteArgb:
  163.                 break
  164.  
  165.         return frameSize
  166.        
  167.     def CropBitmap(bitmap, size):
  168.        
  169.         x as int = ((bitmap.Width / 2) - (size / 2))
  170.         y as int = ((bitmap.Width / 2) - (size / 2))
  171.         w = size
  172.         h = size
  173.    
  174.         croppedBitmap = bitmap.Clone(Rectangle(x, y, w, h), bitmap.PixelFormat)
  175.         bitmap.Dispose()
  176.        
  177.         return croppedBitmap
  178.        
  179.     def ConvertImageToStream(image):
  180.    
  181.         stream = System.IO.MemoryStream()
  182.         image.Save(stream, ImageFormat.Jpeg)
  183.        
  184.         stream.Seek(0, SeekOrigin.Begin)       
  185.         return stream
  186.        
  187.     def DownloadBitmap(url) as Bitmap:
  188.    
  189.         sourceStream = null
  190.        
  191.         try:
  192.    
  193.             request = System.Net.HttpWebRequest.Create(url) as System.Net.HttpWebRequest
  194.             sourceStream = request.GetResponse().GetResponseStream()
  195.            
  196.             return Bitmap(sourceStream as Stream)
  197.                        
  198.         ensure:
  199.        
  200.             if sourceStream != null:
  201.                 sourceStream.Dispose()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement