|
4 | 4 | */
|
5 | 5 |
|
6 | 6 |
|
| 7 | +#include "covers.h" |
| 8 | + |
7 | 9 | #include <Application.h>
|
8 | 10 | #include <Bitmap.h>
|
9 | 11 | #include <DataIO.h>
|
|
14 | 16 | #include <stdio.h>
|
15 | 17 |
|
16 | 18 | #include "CoverWindow.h"
|
| 19 | +#include "Utility.h" |
17 | 20 |
|
18 | 21 |
|
19 |
| -#define MAX_GROUPS 10 |
20 |
| -#define MAX_MATCHES 10 |
21 |
| - |
22 |
| -#define COUNTRY_GERMANY 3 |
23 |
| - |
24 |
| -#define TYPE_ORIGINAL "SCRM" |
25 |
| -#define TYPE_LARGE "SCL" |
26 |
| -#define TYPE_THUMB "THUMB" |
27 |
| - |
28 |
| - |
29 |
| -class DataListener : public BUrlProtocolListener { |
| 22 | +class CoversApplication : public BApplication { |
30 | 23 | public:
|
31 |
| - DataListener() |
32 |
| - { |
33 |
| - } |
| 24 | + CoversApplication(); |
| 25 | + virtual ~CoversApplication(); |
34 | 26 |
|
35 |
| - virtual ~DataListener() |
36 |
| - { |
37 |
| - } |
38 |
| - |
39 |
| - virtual void DataReceived(BUrlRequest* request, const char* data, |
40 |
| - off_t offset, ssize_t size) |
41 |
| - { |
42 |
| - fResult.Write(data, size); |
43 |
| - } |
44 |
| - |
45 |
| - BMallocIO& IO() { return fResult; } |
46 |
| - |
47 |
| -private: |
48 |
| - BMallocIO fResult; |
| 27 | + virtual void MessageReceived(BMessage* message); |
49 | 28 | };
|
50 | 29 |
|
51 | 30 |
|
52 |
| -BBitmap* |
53 |
| -loadImage(int countryCode, const BString& id, const char* type) |
| 31 | +CoversApplication::CoversApplication() |
| 32 | + : |
| 33 | + BApplication("application/x-vnd.pinc-covers") |
54 | 34 | {
|
55 |
| - BString urlString; |
56 |
| - urlString.SetToFormat("http://ecx.images-amazon.com/images/P/%s.%02d._%s_", |
57 |
| - id.String(), countryCode, type); |
58 |
| - |
59 |
| - BUrl url(urlString.String()); |
60 |
| - BHttpRequest request(url); |
61 |
| - |
62 |
| - DataListener listener; |
63 |
| - request.SetListener(&listener); |
64 |
| - request.Run(); |
65 |
| - while (request.IsRunning()) { |
66 |
| - snooze(50000); |
67 |
| - putchar('.'); |
68 |
| - fflush(stdout); |
69 |
| - } |
| 35 | +} |
70 | 36 |
|
71 |
| - const BHttpResult& result = dynamic_cast<const BHttpResult&>( |
72 |
| - request.Result()); |
73 |
| - printf("Status: %" B_PRId32 " d - %s\n", result.StatusCode(), |
74 |
| - result.StatusText().String()); |
75 | 37 |
|
76 |
| - listener.IO().Seek(0, SEEK_SET); |
77 |
| - return BTranslationUtils::GetBitmap(&listener.IO()); |
| 38 | +CoversApplication::~CoversApplication() |
| 39 | +{ |
78 | 40 | }
|
79 | 41 |
|
80 | 42 |
|
81 | 43 | void
|
82 |
| -getImageFromAmazon(const char* country, int countryCode, const BString& artist, |
83 |
| - const BString& title) |
| 44 | +CoversApplication::MessageReceived(BMessage* message) |
84 | 45 | {
|
85 |
| - BString pattern("<a[^>]*title\\s*=\\s*\"([^\"]+)\"[^>]*" |
86 |
| - "href\\s*=\\s*\"([^\"]+?/dp/([^/]+)/)[^>]+><h2.*?>([^<]+)(</a>)?</span></div></div>"); |
87 |
| - //"); |
88 |
| - regex_t patternCompiled; |
89 |
| - regmatch_t groups[MAX_GROUPS]; |
90 |
| - int patternResult = regcomp(&patternCompiled, pattern.String(), |
91 |
| - REG_EXTENDED | REG_ICASE); |
92 |
| - if (patternResult != 0) { |
93 |
| - printf("bad pattern: %d!\n", patternResult); |
94 |
| - char text[256]; |
95 |
| - regerror(patternResult, &patternCompiled, text, sizeof(text)); |
96 |
| - printf(" --> %s\n", text); |
97 |
| - return; |
98 |
| - } |
99 |
| - |
100 |
| - BString urlString("http://www.amazon.%COUNTRY%/gp/search/" |
101 |
| - "?search-alias=popular&sort=relevancerank"); |
102 |
| - if (!artist.IsEmpty()) |
103 |
| - urlString += "&field-artist=%ARTIST%"; |
104 |
| - if (!title.IsEmpty()) |
105 |
| - urlString += "&field-title=%TITLE%"; |
106 |
| - urlString.ReplaceAll("%COUNTRY%", country); |
107 |
| - urlString.ReplaceAll("%ARTIST%", BUrl::UrlEncode(artist)); |
108 |
| - urlString.ReplaceAll("%TITLE%", BUrl::UrlEncode(title)); |
109 |
| - printf("URL: %s\n", urlString.String()); |
110 |
| - |
111 |
| - BUrl url(urlString); |
112 |
| - BHttpRequest request(url); |
113 |
| - |
114 |
| - DataListener listener; |
115 |
| - request.SetListener(&listener); |
116 |
| - request.Run(); |
117 |
| - while (request.IsRunning()) { |
118 |
| - snooze(50000); |
119 |
| - putchar('.'); |
120 |
| - fflush(stdout); |
121 |
| - } |
122 |
| - |
123 |
| - const BHttpResult& result = dynamic_cast<const BHttpResult&>( |
124 |
| - request.Result()); |
125 |
| - printf("Status: %" B_PRId32 " - %s\n", result.StatusCode(), |
126 |
| - result.StatusText().String()); |
127 |
| - |
128 |
| - BString data((const char*)listener.IO().Buffer(), |
129 |
| - listener.IO().BufferLength()); |
130 |
| - printf("length: %d\n", (int)listener.IO().BufferLength()); |
131 |
| - const char* cursor = data.String(); |
132 |
| - int offset = 0; |
133 |
| - |
134 |
| - for (int index = 0; index < MAX_MATCHES; index++) { |
135 |
| - if (regexec(&patternCompiled, cursor, MAX_GROUPS, groups, 0)) |
| 46 | + switch (message->what) { |
| 47 | + case kMsgSelected: |
| 48 | + case kMsgSkip: |
| 49 | + message->PrintToStream(); |
| 50 | + break; |
| 51 | + default: |
| 52 | + BApplication::MessageReceived(message); |
136 | 53 | break;
|
137 |
| - |
138 |
| - BString id; |
139 |
| - |
140 |
| - for (int groupIndex = 0; groupIndex < MAX_GROUPS; groupIndex++) { |
141 |
| - if (groups[groupIndex].rm_so == -1) |
142 |
| - break; |
143 |
| - if (groupIndex == 0) |
144 |
| - offset = groups[0].rm_eo; |
145 |
| - |
146 |
| - int length = groups[groupIndex].rm_eo - groups[groupIndex].rm_so; |
147 |
| - BString match(cursor + groups[groupIndex].rm_so, length); |
148 |
| - if (groupIndex) |
149 |
| - printf("%d: match? %s\n", groupIndex, match.String()); |
150 |
| - if (groupIndex == 3) |
151 |
| - id = match; |
152 |
| - } |
153 |
| - |
154 |
| - if (!id.IsEmpty()) { |
155 |
| - BBitmap* bitmap = loadImage(COUNTRY_GERMANY, id, TYPE_ORIGINAL); |
156 |
| - if (bitmap != NULL) |
157 |
| - puts("got image!"); |
158 |
| - else |
159 |
| - puts("nay!"); |
160 |
| - } |
161 |
| - |
162 |
| -printf("offset = %d\n", offset); |
163 |
| - cursor += offset; |
164 | 54 | }
|
165 |
| - regfree(&patternCompiled); |
166 | 55 | }
|
167 | 56 |
|
168 | 57 |
|
| 58 | +// #pragma mark - |
| 59 | + |
| 60 | + |
169 | 61 | int
|
170 | 62 | main()
|
171 | 63 | {
|
172 |
| - BApplication application("application/x-vnd.pinc-covers"); |
173 |
| -// getImageFromAmazon("de", COUNTRY_GERMANY, "toundra", ""); |
| 64 | + CoversApplication application; |
174 | 65 |
|
175 | 66 | BWindow* window = new CoverWindow();
|
176 | 67 | window->CenterOnScreen();
|
|
0 commit comments