Jump to content

Getting images from Amazon.com


GMK
 Share

Recommended Posts

My ultimate goal here is to get the cover image from a digital (or physical) album.  When zoomed in to the fullest, the image is broken down into 16 pieces (four rows of four).  I'd eventually like to get all of those pieces and combine them into one large 1400x1400 image.  I've used a Firefox addon called Linky to open all images in a tab, which allows me to see the images and right-click and save them individually.  Of course, in order to do this, I have to drag the mouse through the different parts of the zoomed in image to see more pieces.  It's a tedious process that I'm trying to automate.

Here is my code so far:

#include <IE.au3>
Global $sURL = 'http://www.amazon.com/gp/product/images/B00QVZ1VXI/ref=dp_image_z_0'
Global $oIE = _IECreate($sURL)
$sHTML = _IEBodyReadHTML($oIE)
Global $sA = StringRegExpReplace($sHTML, "(?s).*ue_pti='(.*?)';.*", "$1")
Global $sC = StringRegExpReplace($sHTML, '(?s).*DynAPI\.addZoomViewer\("http://z2-ec2.images-amazon.com/images/R/.*?\.01\-(.*?)".*', '$1')
Global $sVE = StringRegExpReplace($sHTML, '(?s).*?(\d+),"amztile".*', '$1')
Global $sNewHTML = '    <table>' & @CRLF
Global $sImg
For $iRow = 0 To 3
    $sNewHTML &= '      <tr>' & @CRLF
    For $iCol = 0 To 3
        $sImg = 'http://z2-ec2.images-amazon.com/R/1/a=' & $Sa & '+c=' & $sC & '=_SCR(3,' & $iCol & ',' & $iRow & ')_+o=01+s=RMTILE+va=MAIN+ve=' & $sVE & '+e=.jpg'
        $sNewHTML &= '        <td><img src="' & $sImg & '" /></td>' & @CRLF
    Next
    $sNewHTML &= '      </tr>' & @CRLF
Next
$sNewHTML &= '    </table>' & @CRLF
Global $iWrite = _IEBodyWriteHTML($oIE, $sNewHTML)

Unfortunately, it doesn't show the images like I hoped it would.

If anyone has any insight as to how I can gather these images, using IE, FF, WinHttp or any other tools, I would be grateful.  (Also, if anyone has any experience stitching those type of pieces together into an image, I would appreciate some direction there as well.)  Thank you!

Edited by GMK
Link to comment
Share on other sites

Thank you for your reply.  I'll certainly be able to use that code for some album covers.  However, this still doesn't solve my problem of wanting the highest resolution of other album covers.  I've found that for some albums, the digital version has a higher resolution cover than the CD version.  I guess it's the digital version's high resolution cover I'm after in most cases.

Example:

CD = 1048x1050

Digital Version = 1400x1400 (If I can save the image)

Edited by GMK
Link to comment
Share on other sites

Here was my solution:

#Region ; **** Includes ****
#include <IE.au3>
#include <File.au3>
#include <GDIPlus.au3>
#include <ID3_v3.4.au3>
#EndRegion ; **** Includes ****
 
#Region ; **** Options ****
Opt('TrayIconDebug', 1)
Opt('MustDeclareVars', 1)
#EndRegion ; **** Options ****
 
#Region ; **** Get MP3 List ****
Global $sDir = FileSelectFolder('Select a folder:', @UserProfileDir & '\Music\')
If @error Then Exit
Global $aFiles = _FileListToArrayRec($sDir, '*.mp3', $FLTAR_FILES, $FLTAR_RECUR)
_ArrayDelete($aFiles, 0)
Global $iFiles = UBound($aFiles)
Global $aTitles[$iFiles][2]
Global $sAlbumArtFile
For $iFile = $iFiles - 1 To 0 Step -1
    _ID3ReadTag($sDir & $aFiles[$iFile])
    $sAlbumArtFile = _ID3GetTagField("APIC")
    If @extended > 0 Then
        _ArrayDelete($aFiles, $iFile)
        _ArrayDelete($aTitles, $iFile)
    Else
        $aTitles[$iFile][0] = _ID3GetTagField('TPE1')
        $aTitles[$iFile][1] = _ID3GetTagField('TALB')
    EndIf
Next
_ArraySort($aTitles, 0, 0, 0, 1)
Global $iTitles = UBound($aTitles)
For $iTitle = $iTitles - 1 To 0 Step -1
    If $iTitle - 1 >= 0 And $aTitles[$iTitle][1] = $aTitles[$iTitle - 1][1] Then _ArrayDelete($aTitles, $iTitle)
Next
$iTitles = UBound($aTitles)
#EndRegion ; **** Get MP3 List ****
 
#Region ; **** Get Covers ****
Global $oIE = _IECreate('', 0, 0)
Global $sArtist, $sTitle
Global $sSearchURL = "http://www.amazon.com/s/ref=nb_sb_noss_2?url=search-alias%3Ddigital-music&field-keywords="
Global $sHTML, $aLinks
Global $sTempFolder = @TempDir & '\Amazon_Cover\'
Global $sFolder = @DesktopDir & '\Covers\'
Global $sImageURL, $sFile
For $iTitle = 0 To $iTitles - 1
    $sArtist = $aTitles[$iTitle][0]
    $sTitle = $aTitles[$iTitle][1]
    ConsoleWrite($sArtist & ' - ' & $sTitle & @CRLF)
    $sFile = $sFolder
    $sFile &= StringRegExpReplace($sTitle, "[[:punct:]]", "_")
    $sFile &= '.jpg'
    If Not FileExists($sFile) And $sTitle <> "" Then
        $sSearchURL &= StringReplace($sArtist, " ", "+")
        $sSearchURL &= '+' & StringReplace($sTitle, " ", "+")
        _IENavigate($oIE, $sSearchURL)
        $sHTML = _IEDocReadHTML($oIE)
        $aLinks = StringRegExp($sHTML, '<a href="(http://www.amazon.com/.*?/dp/\w+/).*?">', 3)
        If Not IsArray($aLinks) Then
            $sSearchURL = "http://www.amazon.com/s/ref=nb_sb_noss_2?url=search-alias%3Ddigital-music&field-keywords="
            ContinueLoop
        EndIf
        $sImageURL = StringRegExpReplace($aLinks[0], 'http://www.amazon.com/.*?/dp/(.*?)/', 'http://www.amazon.com/gp/product/images/$1/ref=dp_image_z_0')
        _GetPieces($sImageURL, $sTempFolder)
        _AssemblePieces($sTempFolder, $sFile)
    EndIf
    $sHTML = ""
    $sSearchURL = "http://www.amazon.com/s/ref=nb_sb_noss_2?url=search-alias%3Ddigital-music&field-keywords="
Next
_IEQuit($oIE)
#EndRegion ; **** Get Covers ****
 
#Region ; **** Functions ****
Func _GetPieces($sURL, $sTempFolder = @TempDir)
    _IENavigate($oIE, $sURL)
    Local $sHTML = _IEDocReadHTML($oIE)
    If Not StringRegExp($sHTML, "(?s).*ue_pti='.*?';.*") Then Return SetError(1, 0, 0)
    Local $sA = StringRegExpReplace($sHTML, "(?s).*ue_pti='(.*?)';.*", "$1")
    If Not StringRegExp($sHTML, '(?s).*DynAPI\.addZoomViewer\("http://z2-ec2.images-amazon.com/images/R/.*?\.01\-.*?".*') Then   Return SetError(2, 0, 0)
    Local $sC = StringRegExpReplace($sHTML, '(?s).*DynAPI\.addZoomViewer\("http://z2-ec2.images-amazon.com/images/R/.*?\.01\-(.*?)".*', '$1')
    If Not StringRegExp($sHTML, '(?s).*?\d+,"amztile".*', '$1') Then Return SetError(3, 0, 0)
    Local $sVE = StringRegExpReplace($sHTML, '(?s).*?(\d+),"amztile".*', '$1')
    If Not FileExists($sTempFolder) Then DirCreate($sTempFolder)
    Local $sImg, $sDestFile, $iInetGet
    For $iRow = 0 To 4
        For $iCol = 0 To 4
            $sImg = 'http://z2-ec2.images-amazon.com/R/1/a=' & $sA & '+c=' & $sC & '+d=_SCR(3,' & $iCol & ',' & $iRow & ')_+o=01+s=RMTILE+va=MAIN+ve=' & $sVE & '+e=.jpg'
            $sDestFile = $sTempFolder & $iRow + 1 & Chr($iCol + 65) & '.jpg'
            $iInetGet = InetGet($sImg, $sDestFile, 9)
        Next
    Next
EndFunc
 
Func _AssemblePieces($sTempFolder, $sFile)
    _GDIPlus_Startup()
    Local $aFiles = _FileListToArray($sTempFolder, '*.jpg')
    If Not IsArray($aFiles) Then Return SetError(1, 0, 0)
    _ArrayDelete($aFiles, 0)
    Local $iLastFile = UBound($aFiles) - 1
    For $i = 1 To 2
        _ArrayColInsert($aFiles, 1)
    Next
    Local $hImage, $iWidth = 0, $iHeight = 0
    For $iFile = 0 To $iLastFile
        $hImage = _GDIPlus_ImageLoadFromFile($sTempFolder & $aFiles[$iFile][0])
        $aFiles[$iFile][1] = _GDIPlus_ImageGetWidth($hImage)
        $aFiles[$iFile][2] = _GDIPlus_ImageGetHeight($hImage)
        _GDIPlus_ImageDispose($hImage)
        If StringInStr($aFiles[$iFile][0], '1') Then $iWidth += $aFiles[$iFile][1]
        If StringInStr($aFiles[$iFile][0], 'A') Then $iHeight += $aFiles[$iFile][2]
    Next
    Local Const $hBitmap = _GDIPlus_BitmapCreateFromScan0($iWidth, $iHeight)
    Local Const $hGfx = _GDIPlus_ImageGetGraphicsContext($hBitmap)
    Local $x = 0, $y = 0, $sLetter
    For $iFile = 0 To $iLastFile
        $hImage = _GDIPlus_ImageLoadFromFile($sTempFolder & $aFiles[$iFile][0])
        _GDIPlus_GraphicsDrawImageRect($hGfx, $hImage, $x, $y, $aFiles[$iFile][1], $aFiles[$iFile][2])
        If $iFile < $iLastFile Then
            $sLetter = StringMid($aFiles[$iFile + 1][0], 2, 1)
            Switch $sLetter
                Case 'A'
                    $x = 0
                    $y += $aFiles[$iFile][2]
                Case Else
                    $x += $aFiles[$iFile][1]
            EndSwitch
        EndIf
        _GDIPlus_ImageDispose($hImage)
    Next
    _GDIPlus_ImageSaveToFile($hBitmap, $sFile)
    _GDIPlus_ImageDispose($hBitmap)
    _GDIPlus_Shutdown()
    FileDelete($sTempFolder & "*.jpg")
EndFunc
#EndRegion ; **** Functions ****

Any idea why it wouldn't always put the .jpg extension on the end of the variable/file name?

Edited by GMK
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...