通过PHP获得iTunes Store的封面图

之前呢,因为只有中国区的Apple ID,所以抓取了CD之后,需要自己去找CD封面图,可是网上的封面图绝大部分是靠上传者扫描而来的,效果不是很理想。

我记得在iTunes Store里面可以提供1200x1200的封面大图,而且音乐提供商一般会上传数字版的封面到iTunes Store,效果自然比扫描的好多了。

接下来就开始考虑如何获得1200x1200的大图。

首先呢,在网页版的iTunes Store中有封面图,查看网页的源代码之后,发现格式如下

http://a2.mzstatic.com/us/r30/Music/v4/1b/ae/07/1bae0761-e8de-48cd-b0c4-d61c5afa389b/4933032005384_cover.170x170-75.jpg

可以很清楚的看出网页版iTunes Store使用的封面图大小是170x170,那么直接把170x170替换成1200x1200后,

http://a2.mzstatic.com/us/r30/Music/v4/1b/ae/07/1bae0761-e8de-48cd-b0c4-d61c5afa389b/4933032005384_cover.1200x1200-75.jpg

粘贴到浏览器中打开,果然成功了!

在我以为完成时,却发现有一些网页版iTunes Store的专辑页面找不到170x170的字样,取而代之的是255x255。

这个还是很简单的,于是干脆写了一个PHP脚本来批量获取封面图。

Code

<?php
        print"Input iTunes URL text file:";
        $path = @fgets(fopen('php://stdin', 'r'), 1000) or die('Can not open stdin.');
        $input = fopen(trim($path), 'r');
        #获取储存了网页版地址的文件
#e.g /Users/YOUR_USERNAME/Desktop/Music/1.txt

        print"Input cover save folder path:";
        $coverPath = @fgets(fopen('php://stdin', 'r'), 1000) or die('Can not open stdin.');
        #e.g /Users/YOUR_USERNAME/Desktop/Music/
        if (isset($coverPath)) {
                $coverPath = trim($coverPath);
                if (substr($coverPath,-1) != '/') {
                        $coverPath .= '/';
                }
        }
        #确保这是一个文件夹的路径
        $count = 0;
        while (!feof($input)) {
                $buffer = strtolower(trim(fgets($input, 4096)));
                #去掉无关的字符
                if (ereg('^(http|https)://itunes.apple.com/', $buffer) || ereg('^itunes.apple.com/', $buffer)) {
                        #我们把URL的开头部分和http://itunes.apple.com/或者itunes.apple.com/做比较
                        #遵守上面这个模式的字符串是我们需要的
                        $count++;
                        #处理的封面数
                        print"n  正在处理第[$count]个封面n";
                        print"   |  抓取iTunes Store网页...n";
                        $content = file_get_contents($buffer);
                        #获取网页内容
                        if (isset($content)) {
                                print"   |  已获得第[$count]个封面的iTunes Store网页内容n";
                                preg_match('(http://...mzstatic.com[^"]*(170|255)x(170|255)-75.jpg)', $content, $match);
                                #第一次正则提取,得到网页版缩略图地址
                                #上面得到的类似于http://a3.mzstatic.com/us/r30/Music/f7/ec/63/mzi.yclyevyf.255x255-75.                                

                                $coverURL = preg_replace(array('/255/','/170/'), array('1200','1200'), $match[0]);

                                #第二次用正则替换,将255x255或者170x170的缩略图换成1200x1200的大图
                                print"   |  正在下载第[$count]个的封面n";
                                ob_start();
                                readfile($coverURL);
                                $img = ob_get_contents();
                                ob_end_clean();
                                $size = strlen($img);
                                #获取1200x1200的大图
                                if ($size > 0) {
                                        print"   |  下载成功!正在保存...n";
                                        $filename = $coverPath.end(split('/',$coverURL));
                                        $fp2=@fopen($filename, "a");
                                        fwrite($fp2,$img);
                                        fclose($fp2);
                                        #保存图片
                                }else {
                                        print"   |  处理失败,暂时不能下载第[$count]个封面n";
                                }
                        }
                        else{
                                print"   |  处理失败,暂时不能获取第[$count]个文件的iTunes Store网页内容n";
                        }
                }
        }
?>

Leave a Reply

Your email address will not be published. Required fields are marked *

three + 3 =