summaryrefslogtreecommitdiffstats
path: root/amarok/src/refreshimages.cpp
blob: 4098b83d063cdf73b4d653209fc9983e664b5a87 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
// (c) 2005 Ian Monroe <ian@monroe.nu>
// See COPYING file for licensing information.

#define DEBUG_PREFIX "RefreshImages"

#include "amarok.h"
#include "collectiondb.h"
#include "debug.h"
#include "refreshimages.h"
#include "statusbar.h"

#include <tqdom.h>
#include <tqimage.h>
#include <tqmap.h>
#include <tqobject.h>
#include <tqstringlist.h>
#include <tqvaluelist.h>
#include <tqvariant.h>

#include <tdeio/job.h>
#include <tdeio/jobclasses.h>
#include <tdeio/scheduler.h>

#include <klocale.h>


RefreshImages::RefreshImages()
{
    //"SELECT asin, locale, filename FROM amazon WHERE refetchdate > %1 ;"
    const TQStringList staleImages = CollectionDB::instance()->staleImages();
    TQStringList::ConstIterator it = staleImages.begin();
    TQStringList::ConstIterator end = staleImages.end();

    while( it != end )
    {
        TQString asin=*it;
        it++;
        TQString locale = *it;
        it++;
        TQString md5sum = *it;
        if ( asin.isEmpty() || locale.isEmpty() || md5sum.isEmpty() )
        {
            //somehow we have entries without ASIN
            if ( !md5sum.isEmpty() ) //I've never seen this, just to be sure
                CollectionDB::instance()->removeInvalidAmazonInfo(md5sum);
            it++;
            if ( it==end )
                deleteLater();

            continue;
        }

        TQString url =
            TQString("http://webservices.amazon.%1/onca/xml?Service=AWSECommerceService&SubscriptionId=%2&Operation=ItemLookup&ItemId=%3&ResponseGroup=Small,Images")
             .arg(localeToTLD(locale))
             .arg("0RTQSQ0B8CRY7VX2VF3G2") //Ian Monroe
             .arg(asin);

        debug() << url << endl;

        TDEIO::TransferJob* job = TDEIO::storedGet( url, false, false );
        TDEIO::Scheduler::scheduleJob( job );

        //Amarok::StatusBar::instance()->newProgressOperation( job );
        job->setName( md5sum.ascii() );
        it++; //iterate to the next set

        m_jobInfo[md5sum] = JobInfo( asin, locale, it == end );
        connect( job, TQT_SIGNAL( result( TDEIO::Job* ) ), TQT_SLOT( finishedXmlFetch( TDEIO::Job* ) ) );
    }
}

void
RefreshImages::finishedXmlFetch( TDEIO::Job* xmlJob ) //SLOT
{
    if ( xmlJob->error() )
    {
        Amarok::StatusBar::instance()->shortMessage( i18n( "There was an error communicating with Amazon." ) );
        if ( m_jobInfo[ xmlJob->name() ].m_last )
            deleteLater();

        return;
    }

    TDEIO::StoredTransferJob* const storedJob = static_cast<TDEIO::StoredTransferJob*>( xmlJob );
    const TQString xml = TQString::fromUtf8( storedJob->data().data(), storedJob->data().size() );

    TQDomDocument doc;
    if ( !doc.setContent( xml ) )
      return;

    TQStringList imageSizes;
    imageSizes << "LargeImage" << "MediumImage" << "SmallImage";
    TQString imageUrl;
    foreach( imageSizes )
    {
        TQDomNode imageNode = doc.documentElement()
            .namedItem( "Items" )
            .namedItem( "Item" )
            .namedItem( *it );
        if ( !imageNode.isNull() )
        {
            imageUrl = imageNode.namedItem( "URL" ).firstChild().toText().data();
            if( !imageUrl.isEmpty() )
                break;
        }
    }
    debug() << imageUrl << endl;
    KURL testUrl( imageUrl );
    if( !testUrl.isValid() ) //KIO crashs on empty strings!!!
    {
        //Amazon sometimes takes down covers
        CollectionDB::instance()->removeInvalidAmazonInfo(xmlJob->name());
        return;
    }

    TDEIO::TransferJob* imageJob = TDEIO::storedGet( imageUrl, false, false );
    TDEIO::Scheduler::scheduleJob(imageJob);
    //Amarok::StatusBar::instance()->newProgressOperation( imageJob );
    imageJob->setName(xmlJob->name());
    //get the URL of the detail page
    m_jobInfo[xmlJob->name()].m_detailUrl = doc.documentElement()
       .namedItem( "Items" )
       .namedItem( "Item" )
       .namedItem( "DetailPageURL" ).firstChild().toText().data();
    connect( imageJob, TQT_SIGNAL( result(TDEIO::Job*) ), TQT_SLOT( finishedImageFetch(TDEIO::Job*) ) );
}

void RefreshImages::finishedImageFetch(TDEIO::Job* imageJob)
{
   if( imageJob->error() ) {
        Amarok::StatusBar::instance()->shortMessage(i18n("There was an error communicating with Amazon."));
        if(m_jobInfo[imageJob->name()].m_last)
            deleteLater();

        return;
    }
    TQImage img;
    img.loadFromData(static_cast<TDEIO::StoredTransferJob*>(imageJob)->data());
    img.setText( "amazon-url", 0, m_jobInfo[imageJob->name()].m_detailUrl);
    img.save( Amarok::saveLocation("albumcovers/large/") + imageJob->name(), "PNG");

    CollectionDB::instance()->newAmazonReloadDate( m_jobInfo[imageJob->name()].m_asin
        , m_jobInfo[imageJob->name()].m_locale
        , imageJob->name());

    if(m_jobInfo[imageJob->name()].m_last)
        deleteLater();
}

TQString RefreshImages::localeToTLD(const TQString& locale)
{
    if(locale=="us")
        return "com";
    else if(locale=="jp")
        return "co.jp";
    else if(locale=="uk")
        return "co.uk";
    else
        return locale;
}

#include "refreshimages.moc"