-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
134 lines (110 loc) · 3.48 KB
/
Copy pathmain.cpp
File metadata and controls
134 lines (110 loc) · 3.48 KB
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
#include <QApplication>
#include <QCommandLineParser>
#include "googleimagescraper.h"
bool setProxy(const QString &proxyStr){
QNetworkProxy::ProxyType type;
QString hostname;
qint32 port;
QStringList list1 = proxyStr.split(' ');
if(list1.size() != 2){
return false;
}
QString typeStr = list1.at(0);
if(typeStr.compare("HTTP", Qt::CaseInsensitive) == 0
|| typeStr.compare("HTTPS", Qt::CaseInsensitive) == 0){
type = QNetworkProxy::HttpProxy;
}
else if(typeStr.compare("SOCKS5", Qt::CaseInsensitive) == 0){
type = QNetworkProxy::Socks5Proxy;
}
else{
return false;
}
QStringList list2 = list1.at(1).split(':');
if(list2.size() != 2){
return false;
}
hostname = list2.at(0);
port = list2.at(1).toInt();
QNetworkProxy proxy;
proxy.setType(type);
proxy.setHostName(hostname);
proxy.setPort(port);
QNetworkProxy::setApplicationProxy(proxy);
fprintf(stderr, "Using proxy:\t%s %s:%d\n",
typeStr.toLocal8Bit().constData(),
hostname.toLocal8Bit().constData(),
port);
return true;
}
void showUsage(){
fprintf(stderr, R"usage(
Usage: ./google_image_scraper [options] -k <keywords>
Options:
-h, --help Displays this help.
-v, --version Displays version information.
-p, --proxy <type address> Set application proxy, example: "socks5 127.0.0.1:1080"
-n, --number <num of imgs> Output at most number of images
-f, --face_only Search human face of keywords
-S, --safe_mode Turn on google search safe mode
-k, --keywords <keywords> The keywords of images to scrape, example: "Yao Ming"
)usage");
exit(-1);
}
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QApplication::setApplicationName("Google Image Scraper");
QApplication::setApplicationVersion("v0.2");
QCommandLineParser parser;
parser.addVersionOption();
QCommandLineOption op_help(QStringList() << "h" << "help");
parser.addOption(op_help);
QCommandLineOption op_proxy(QStringList() << "p" << "proxy", " ", " ");
parser.addOption(op_proxy);
QCommandLineOption op_number(QStringList() << "n" << "number", " ", " ");
parser.addOption(op_number);
QCommandLineOption op_faceOnly(QStringList() << "f" << "face_only");
parser.addOption(op_faceOnly);
QCommandLineOption op_safeMode(QStringList() << "S" << "safe_mode");
parser.addOption(op_safeMode);
QCommandLineOption op_keywords(QStringList() << "k" << "keywords", " ", " ");
parser.addOption(op_keywords);
GoogleImageScraper scraper;
parser.process(app);
if(parser.isSet(op_help)){
showUsage();
}
if(parser.isSet(op_proxy)){
QString proxyStr = parser.value(op_proxy);
if(!setProxy(proxyStr)){
fprintf(stderr, "Error format of proxy: %s\n", proxyStr.toLocal8Bit().constData());
showUsage();
}
}
if(parser.isSet(op_number)){
qint32 number = parser.value(op_number).toInt();
if(number <= 0){
fprintf(stderr, "Error format of number: %d\n", number);
showUsage();
}
scraper.setNumber(number);
}
scraper.setFaceOnly(parser.isSet(op_faceOnly));
scraper.setSafeModeOn(parser.isSet(op_safeMode));
QString keywords = parser.value(op_keywords);
if(keywords.isEmpty()){
fprintf(stderr, "Keywords is empty!\n");
showUsage();
}
QStringList imgurlList = scraper.scrapeImageOfKeyWord(keywords);
for(int i = 0; i < imgurlList.size(); i++){
fprintf(stdout, "%s\n", imgurlList.at(i).toLocal8Bit().constData());
fflush(stdout);
if((i % 20) == 0){
QTest::qWait(100);
}
}
fprintf(stderr, "\nTotal %d images been scraped.\n", imgurlList.length());
return 0;
}