-
Notifications
You must be signed in to change notification settings - Fork 0
/
OMDbInfo.cpp
148 lines (129 loc) · 4.03 KB
/
OMDbInfo.cpp
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
#include "OMDbInfo.h"
#include <Windows.h>
#include <tchar.h>
#include <winhttp.h>
OMDbInfo::OMDbInfo(std::string key)
{
apiKey = key;
}
std::string OMDbInfo::GetOmdbInfo(std::string title)
{
char url[256] = { 0 };
sprintf_s(url, BASE_URL_FMT, apiKey.c_str(), title.c_str());
title = url;
std::wstring strUrl(title.begin(), title.end());
return GetWebInfo(L"www.omdbapi.com",strUrl, "");
}
std::string OMDbInfo::DownloadFile(std::string url, std::string fileName)
{
std::wstring str(url.begin(), url.end());
size_t pos = str.find(L"/", 0) + 2;
size_t pos2 = str.find(L"/", pos);
std::wstring server = str.substr(pos, pos2-pos);
std::wstring qry = str.substr(pos2 + 1);
return GetWebInfo(server, qry, fileName);
}
// poster.jpg for covers and posters
std::string OMDbInfo::GetWebInfo(std::wstring server, std::wstring obj, std::string outFile)
{
std::string out;
DWORD dwSize = 0;
DWORD dwDownloaded = 0;
LPSTR pszOutBuffer;
BOOL bResults = FALSE;
HINTERNET hSession = NULL,
hConnect = NULL,
hRequest = NULL;
// Use WinHttpOpen to obtain a session handle.
hSession = WinHttpOpen(L"WinHTTP RWBService/1.0",
WINHTTP_ACCESS_TYPE_DEFAULT_PROXY,
WINHTTP_NO_PROXY_NAME,
WINHTTP_NO_PROXY_BYPASS, 0);
// Specify an HTTP server.
if (hSession)
hConnect = WinHttpConnect(hSession, server.c_str(),
INTERNET_DEFAULT_HTTPS_PORT, 0);
// Create an HTTP request handle.
if (hConnect)
hRequest = WinHttpOpenRequest(hConnect, L"GET", obj.c_str(),
NULL, WINHTTP_NO_REFERER,
WINHTTP_DEFAULT_ACCEPT_TYPES,
WINHTTP_FLAG_SECURE);
// Send a request.
if (hRequest)
bResults = WinHttpSendRequest(hRequest,
WINHTTP_NO_ADDITIONAL_HEADERS, 0,
WINHTTP_NO_REQUEST_DATA, 0,
0, 0);
// End the request.
if (bResults)
bResults = WinHttpReceiveResponse(hRequest, NULL);
// Keep checking for data until there is nothing left.
if (bResults)
{
FILE* f = NULL;
if (outFile != "")
{
if (fopen_s(&f, outFile.c_str(), "wb") != 0)
{
f = NULL;
}
}
do
{
// Check for available data.
dwSize = 0;
if (!WinHttpQueryDataAvailable(hRequest, &dwSize))
printf("Error %u in WinHttpQueryDataAvailable.\n",
GetLastError());
// Allocate space for the buffer.
pszOutBuffer = new char[dwSize + 1];
if (!pszOutBuffer)
{
printf("Out of memory\n");
dwSize = 0;
}
else
{
// Read the data.
ZeroMemory(pszOutBuffer, dwSize + 1);
if (!WinHttpReadData(hRequest, (LPVOID)pszOutBuffer,
dwSize, &dwDownloaded))
{
printf("Error %u in WinHttpReadData.\n", GetLastError());
}
else
{
//printf("%s", pszOutBuffer);
if (f)
{
fwrite(pszOutBuffer, 1, dwSize, f);
out += "1";
}
else
{
out += pszOutBuffer;
}
}
// Free the memory allocated to the buffer.
delete[] pszOutBuffer;
}
} while (dwSize > 0);
if (f)
{
fclose(f);
}
}
// Report any errors.
if (!bResults)
{
//char buf[512] = { 0 };
//sprintf_s(buf, "Error %d has occurred.\n", GetLastError());
//MessageBoxA(NULL, buf, "Web Error", MB_OK);
}
// Close any open handles.
if (hRequest) WinHttpCloseHandle(hRequest);
if (hConnect) WinHttpCloseHandle(hConnect);
if (hSession) WinHttpCloseHandle(hSession);
return out;
}