This repository has been archived by the owner on Aug 4, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Bitly.cpp
209 lines (157 loc) · 6.74 KB
/
Bitly.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
// Bitly.cpp
//
// Copyright 2010 Alexis Morin <alexis@alexis-laptop>
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
// MA 02110-1301, USA.
#include "Bitly.h"
Bitly::Bitly(){
userlogin = "guys";
apiKey = "R_86305ef3a8a76de670cd9663b9c15637";
needsExtension = false;
if (userlogin.length() == 0 || apiKey.length() == 0){
std::cout << "you can not use Bitly, no API data specified" << std::endl;
return;
}
curl = curl_easy_init();
}
// This is the writer call back function used by curl
static int bitlyWriter(char *data, size_t size, size_t nmemb, std::string *buffer){
// What we will return
int result = 0;
// Is there anything in the buffer?
if (buffer != NULL){
// Append the data to the buffer
buffer->append(data, size * nmemb);
// How much did we write?
result = size * nmemb;
}
return result;
}
std::string Bitly::checkForUrl(std::string fullString){
int urlPos;
std::string stringWithShortenedUrl;
//std::string urlContains[] = {".com", ".net", ".org", ".co.uk", ".ca", ".tv"};
//std::cout << "got passed:==== " << fullString << std::endl;
//find the position of the URL the 1st time
urlPos = hasUrl(fullString, 0);
//std::cout << "message inside bitly: " << urlPos << std::endl;
if ( urlPos == -1 ) {
return fullString;
}
while(((size_t)urlPos != std::string::npos)){
//std::cout << "checking for " << urlStarts[n] << " in string " << fullString << std::endl;
size_t valuePositionBegin = urlPos;
size_t valuePositionEnd;
valuePositionEnd = fullString.find_first_of(' ', valuePositionBegin);
//if the delimeter is not found, assume that the URL is at the end of the string
if (valuePositionEnd == std::string::npos){
valuePositionEnd = fullString.size();
}
std::string shortUrl;
if(needsExtension)
shortUrl = shorten("http://" + fullString.substr(valuePositionBegin, valuePositionEnd-valuePositionBegin));
else
shortUrl = shorten(fullString.substr(valuePositionBegin, valuePositionEnd-valuePositionBegin));
std::string beforeUrl = fullString.substr(0, valuePositionBegin);
std::string afterUrl = fullString.substr(valuePositionEnd, fullString.size()-valuePositionEnd);
stringWithShortenedUrl = beforeUrl + shortUrl + afterUrl;
fullString = stringWithShortenedUrl;
urlPos = hasUrl(fullString, 0);
}
return stringWithShortenedUrl;
}
int Bitly::hasUrl(std::string fullString, size_t startPosition){
size_t wwwPos;
size_t httpPos;
std::string urlStarts[2] = {"http://", "www."};
std::string notUrlStarts[1] = {"http://bit.ly/"};
needsExtension = false;
httpPos = fullString.find(urlStarts[0], startPosition);
wwwPos = fullString.find(urlStarts[1], startPosition);
if(fullString.find(notUrlStarts[0], startPosition) != std::string::npos){
return hasUrl(fullString, httpPos+1);
}
if(wwwPos != std::string::npos && httpPos != std::string::npos){//found both in the same URL
if((int)wwwPos - (int)httpPos == (int)urlStarts[0].length()){
return httpPos;
}
}
if(wwwPos != std::string::npos){
needsExtension = true;
return wwwPos;
}
if(httpPos != std::string::npos){
return httpPos;
}
return (int)std::string::npos;
}
std::string Bitly::shorten(std::string uri){
if(uri.length() == 0){
//URI for shortening is empty
//std::cout << "URI is empty" << std::endl;
}else{
//make the full URL to pass to the API
apiPageBuffer.clear();
fullUrl = "http://api.bit.ly/v3/shorten?login="+userlogin+"&apiKey="+apiKey+"&format=xml&uri="+uri;
//std::cout << "shortening: " << uri << std::endl;
//std::cout << "calling: " << fullUrl << std::endl;
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, bitlyWriter);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &apiPageBuffer);
curl_easy_setopt(curl, CURLOPT_HTTPGET, 1);
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1);
//set the URL to go to
curl_easy_setopt(curl, CURLOPT_URL, fullUrl.c_str() );
//doesn't seem to be performing too hot
bitlyResult = curl_easy_perform(curl);
//prints out the full response XML
//std::cout << apiPageBuffer << std::endl;
TiXmlDocument doc("shorten");
bool loadOkay = doc.Parse(apiPageBuffer.c_str(), 0, TIXML_ENCODING_UTF8);
if (!loadOkay) {
//std::cout << "umm...nope" << std::endl;
}else{
//std::cout << "so far, so good" << std::endl;
TiXmlHandle docHandle(&doc);
TiXmlElement *status_code = docHandle.FirstChild("response").Child("status_code", 0).Element();
//error checking
int statusCheck = atoi(status_code->GetText());
bool shortenIsValid = 0;
if(200 == statusCheck ){
//std::cout << "status 200" << std::endl;
shortenIsValid = 1;
}/*else if(403 == statusCheck){
std::cout << "RATE_LIMIT_EXCEEDED" << std::endl;
//shortenIsValid = 0;
}else if(500 == statusCheck){
std::cout << "INVALID_URI or MISSING_ARG_LOGIN" << std::endl;
//shortenIsValid = 0;
}else if(503 == statusCheck){
std::cout << "UNKNOWN_ERROR" << std::endl;
//shortenIsValid = 0;
}*/
//std::cout << uri << std::endl;
if(shortenIsValid){
TiXmlElement *shortUrl = docHandle.FirstChild("response").Child("data", 0).Child("url",0).Element();
//std::cout << shortUrl->GetText() << std::endl;
return (std::string) shortUrl->GetText();
}else{
//std::cout << "encountered an error, returning original uri" << uri << std::endl;
return uri;
}
}
}
return "";
}