| 1 |
/* |
|---|
| 2 |
Copyright (c) 2007, Carlos Santander Bernal |
|---|
| 3 |
|
|---|
| 4 |
This software is provided 'as/is', without any express or implied warranty. In |
|---|
| 5 |
no event will the authors be held liable for any damages arising from the use |
|---|
| 6 |
of this software. |
|---|
| 7 |
|
|---|
| 8 |
Permission is granted to anyone to use this software for any purpose, including |
|---|
| 9 |
commercial applications, and to alter it and redistribute it freely, subject to |
|---|
| 10 |
the following restrictions: |
|---|
| 11 |
|
|---|
| 12 |
1. The origin of this software must not be misrepresented; you must not |
|---|
| 13 |
claim that you wrote the original software. If you use this software in |
|---|
| 14 |
a product, an acknowledgment in the product documentation would be |
|---|
| 15 |
appreciated but is not required. |
|---|
| 16 |
|
|---|
| 17 |
2. Altered source versions must be plainly marked as such, and must not be |
|---|
| 18 |
misrepresented as being the original software. |
|---|
| 19 |
|
|---|
| 20 |
3. This notice may not be removed or altered from any source distribution. |
|---|
| 21 |
*/ |
|---|
| 22 |
module apufba.Main; |
|---|
| 23 |
|
|---|
| 24 |
import apufba.platf.LaunchBrowser; |
|---|
| 25 |
|
|---|
| 26 |
import fbd.FacebookClient : FacebookClient, PropertyInfo; |
|---|
| 27 |
import fbd.FacebookError : FacebookError; |
|---|
| 28 |
import fbd.FacebookUser : FacebookUser; |
|---|
| 29 |
|
|---|
| 30 |
import tango.io.FileConduit : FileConduit; |
|---|
| 31 |
import tango.math.Random : Random; |
|---|
| 32 |
import tango.text.Util : join, locate, trim; |
|---|
| 33 |
import tango.text.convert.Integer : toUtf8; |
|---|
| 34 |
import tango.text.stream.LineIterator : LineIterator; |
|---|
| 35 |
import tango.util.log.ConsoleAppender : ConsoleAppender; |
|---|
| 36 |
import tango.util.log.Log : Log, Logger; |
|---|
| 37 |
import tango.util.time.DateTime : DateTime; |
|---|
| 38 |
|
|---|
| 39 |
int main (char[][] args) |
|---|
| 40 |
{ |
|---|
| 41 |
Logger logger = Log.getLogger ("apufba"); |
|---|
| 42 |
logger.addAppender (new ConsoleAppender ()); |
|---|
| 43 |
|
|---|
| 44 |
/* |
|---|
| 45 |
'account' should be: |
|---|
| 46 |
API Key: your-api-key |
|---|
| 47 |
Secret: your-secret |
|---|
| 48 |
*/ |
|---|
| 49 |
auto account = new FileConduit ("account"); |
|---|
| 50 |
auto it = new LineIterator!(char) (account); |
|---|
| 51 |
|
|---|
| 52 |
char[] tmp = it.next (); |
|---|
| 53 |
auto pos = locate (tmp, ':'); |
|---|
| 54 |
char[] apikey = trim (tmp[pos + 1 .. $]); |
|---|
| 55 |
|
|---|
| 56 |
tmp = it.next (); |
|---|
| 57 |
pos = locate (tmp, ':'); |
|---|
| 58 |
char[] secret = trim (tmp[pos + 1 .. $]); |
|---|
| 59 |
|
|---|
| 60 |
logger.info ("API Key: " ~ apikey); |
|---|
| 61 |
logger.info ("Secret: " ~ secret); |
|---|
| 62 |
|
|---|
| 63 |
auto fbclient = new FacebookClient (apikey, secret); |
|---|
| 64 |
fbclient.authenticate (); |
|---|
| 65 |
|
|---|
| 66 |
auto loginurl = fbclient.loginURL (); |
|---|
| 67 |
logger.info ("Login URL: " ~ loginurl); |
|---|
| 68 |
|
|---|
| 69 |
logInToFacebook (loginurl); |
|---|
| 70 |
|
|---|
| 71 |
fbclient.getSession (); |
|---|
| 72 |
|
|---|
| 73 |
auto fields_to_request = [cast(char[]) FacebookUser.Fields.NAME, |
|---|
| 74 |
FacebookUser.Fields.NOTES_COUNT, FacebookUser.Fields.WALL_COUNT, |
|---|
| 75 |
FacebookUser.Fields.FIRST_NAME, FacebookUser.Fields.STATUS, |
|---|
| 76 |
/+ |
|---|
| 77 |
FacebookUser.Fields.PROFILE_UPDATE_TIME, FacebookUser.Fields.WORK_HISTORY, |
|---|
| 78 |
FacebookUser.Fields.MEETING_FOR, FacebookUser.Fields.MEETING_SEX, |
|---|
| 79 |
FacebookUser.Fields.EDUCATION_HISTORY, |
|---|
| 80 |
FacebookUser.Fields.BIRTHDAY, |
|---|
| 81 |
FacebookUser.Fields.POLITICAL, FacebookUser.Fields.TIMEZONE, |
|---|
| 82 |
FacebookUser.Fields.AFFILIATIONS, |
|---|
| 83 |
FacebookUser.Fields.RELATIONSHIP_STATUS, FacebookUser.Fields.RELIGION, |
|---|
| 84 |
FacebookUser.Fields.SEX, FacebookUser.Fields.SIGNIFICANT_OTHER_ID, |
|---|
| 85 |
FacebookUser.Fields.CURRENT_LOCATION, |
|---|
| 86 |
FacebookUser.Fields.HOMETOWN_LOCATION, FacebookUser.Fields.ACTIVITIES, |
|---|
| 87 |
FacebookUser.Fields.HS_INFO, FacebookUser.Fields.TV, |
|---|
| 88 |
FacebookUser.Fields.HAS_ADDED_APP, FacebookUser.Fields.IS_APP_USER, |
|---|
| 89 |
FacebookUser.Fields.INTERESTS, FacebookUser.Fields.MUSIC, |
|---|
| 90 |
FacebookUser.Fields.MOVIES, FacebookUser.Fields.PIC_BIG, |
|---|
| 91 |
FacebookUser.Fields.ABOUT_ME, FacebookUser.Fields.BOOKS, |
|---|
| 92 |
FacebookUser.Fields.LAST_NAME, FacebookUser.Fields.PIC, |
|---|
| 93 |
FacebookUser.Fields.PIC_SMALL, FacebookUser.Fields.PIC_SQUARE, |
|---|
| 94 |
FacebookUser.Fields.QUOTES, |
|---|
| 95 |
// +/ |
|---|
| 96 |
]; |
|---|
| 97 |
|
|---|
| 98 |
auto user = fbclient.getThisUserInfo (fields_to_request); |
|---|
| 99 |
|
|---|
| 100 |
with (logger) |
|---|
| 101 |
with (user) |
|---|
| 102 |
{ |
|---|
| 103 |
info ("User ID: " ~ userId); |
|---|
| 104 |
info (" | name: " ~ name); |
|---|
| 105 |
info (" | notes count: " ~ .toUtf8 (notesCount)); |
|---|
| 106 |
info (" | wall count: " ~ .toUtf8 (wallCount)); |
|---|
| 107 |
info (" | status: " ~ firstName ~ " is " ~ status.message); |
|---|
| 108 |
info (" | set on " ~ dateTimeToUtf8 (status.time)); |
|---|
| 109 |
/+ |
|---|
| 110 |
info (" | work history:"); |
|---|
| 111 |
foreach (winfo; workHistory) |
|---|
| 112 |
with (winfo) |
|---|
| 113 |
info ( |
|---|
| 114 |
" | " ~ companyName ~ ", " ~ description ~ ", " ~ position ~ ", " ~ startDate ~ ", " ~ endDate ~ ", " ~ location.city ~ ", " ~ location.country); |
|---|
| 115 |
info (" | profile update time: " ~ dateTimeToUtf8 ( |
|---|
| 116 |
profileUpdateTime)); |
|---|
| 117 |
info (" | meeting for: " ~ join (meetingFor, "+")); |
|---|
| 118 |
info (" | meeting sex: " ~ join (meetingSex, "+")); |
|---|
| 119 |
info (" | birthday: " ~ birthday); |
|---|
| 120 |
info (" | political: " ~ political); |
|---|
| 121 |
info (" | timezone: " ~ timezone); |
|---|
| 122 |
info (" | relationship status: " ~ relationshipStatus); |
|---|
| 123 |
info (" | religion: " ~ religion); |
|---|
| 124 |
info (" | sex: " ~ sex); |
|---|
| 125 |
info (" | significant other id: " ~ significantOtherId); |
|---|
| 126 |
info (" | activities: " ~ activities); |
|---|
| 127 |
info (" | tv: " ~ tv); |
|---|
| 128 |
info (" | interests: " ~ interests); |
|---|
| 129 |
info (" | music: " ~ music); |
|---|
| 130 |
info (" | movies: " ~ movies); |
|---|
| 131 |
info (" | big picture URL: " ~ picBig); |
|---|
| 132 |
info (" | about me: " ~ aboutMe); |
|---|
| 133 |
info (" | books: " ~ books); |
|---|
| 134 |
info (" | first name: " ~ firstName); |
|---|
| 135 |
info (" | last name: " ~ lastName); |
|---|
| 136 |
info (" | pic URL: " ~ pic); |
|---|
| 137 |
info (" | small pic URL: " ~ picSmall); |
|---|
| 138 |
info (" | square pic URL: " ~ picSquare); |
|---|
| 139 |
info (" | quotes: " ~ quotes); |
|---|
| 140 |
info (" | has added application: " ~ .toUtf8 (hasAddedApp)); |
|---|
| 141 |
info (" | is application user: " ~ .toUtf8 (isAppUser)); |
|---|
| 142 |
with (hometownLocation) |
|---|
| 143 |
info ( |
|---|
| 144 |
" | hometown location: " ~ city ~ ", " ~ state ~ ", " ~ country ~ ", " ~ .toUtf8 ( |
|---|
| 145 |
zip)); |
|---|
| 146 |
with (currentLocation) |
|---|
| 147 |
info ( |
|---|
| 148 |
" | current location: " ~ city ~ ", " ~ state ~ ", " ~ country ~ ", " ~ .toUtf8 ( |
|---|
| 149 |
zip)); |
|---|
| 150 |
info (" | affiliations:"); |
|---|
| 151 |
foreach (aff; affiliations) |
|---|
| 152 |
with (aff) |
|---|
| 153 |
info ( |
|---|
| 154 |
" | " ~ name ~ ", " ~ .toUtf8 (nid) ~ ", " ~ type ~ ", " ~ status ~ ", " ~ .toUtf8 ( |
|---|
| 155 |
year)); |
|---|
| 156 |
info (" | education history:"); |
|---|
| 157 |
foreach (einfo; educationHistory) |
|---|
| 158 |
with (einfo) |
|---|
| 159 |
{ |
|---|
| 160 |
info (" | " ~ name ~ ", " ~ .toUtf8 (year)); |
|---|
| 161 |
foreach (conc; concentrations) |
|---|
| 162 |
info (" | " ~ conc); |
|---|
| 163 |
} |
|---|
| 164 |
// +/ |
|---|
| 165 |
} |
|---|
| 166 |
|
|---|
| 167 |
//launchBrowser (user.picBig); |
|---|
| 168 |
|
|---|
| 169 |
version (none) |
|---|
| 170 |
{ |
|---|
| 171 |
logger.info ("getting friends"); |
|---|
| 172 |
|
|---|
| 173 |
auto friendList = fbclient.getFriends (); |
|---|
| 174 |
|
|---|
| 175 |
logger.info (toUtf8 (friendList.length) ~ " friends"); |
|---|
| 176 |
|
|---|
| 177 |
logger.info ("getting friends details"); |
|---|
| 178 |
|
|---|
| 179 |
auto start = (new Random ()).next (friendList.length - 5); |
|---|
| 180 |
auto friendsDetails = fbclient.getUsersInfo ( |
|---|
| 181 |
friendList[start .. start + 5], [FacebookUser.Fields.NAME]); |
|---|
| 182 |
|
|---|
| 183 |
logger.info ("5 random friends:"); |
|---|
| 184 |
foreach (friend; friendsDetails) |
|---|
| 185 |
logger.info (" | " ~ friend.name); |
|---|
| 186 |
|
|---|
| 187 |
logger.info ("is app added? " ~ toUtf8 (fbclient.isAppAdded ())); |
|---|
| 188 |
|
|---|
| 189 |
logger.info ("publishing story"); |
|---|
| 190 |
if (fbclient.publishStory ("apufba story", |
|---|
| 191 |
"this is just a test... please delete it...")) |
|---|
| 192 |
logger.info ("story published!"); |
|---|
| 193 |
else |
|---|
| 194 |
logger.info ("couldn't publish story :("); |
|---|
| 195 |
|
|---|
| 196 |
logger.info ("getting friends"); |
|---|
| 197 |
auto friendsUsers = fbclient.getFriendsAppUsers (); |
|---|
| 198 |
logger.info ( |
|---|
| 199 |
toUtf8 (friendsUsers.length) ~ " friends who are app users"); |
|---|
| 200 |
|
|---|
| 201 |
logger.info ("publishing action"); |
|---|
| 202 |
if (fbclient.publishAction ("apufba action", |
|---|
| 203 |
"this is just a test... please delete it...")) |
|---|
| 204 |
logger.info ("action published!"); |
|---|
| 205 |
else |
|---|
| 206 |
logger.info ("couldn't publish action :("); |
|---|
| 207 |
|
|---|
| 208 |
logger.info ("getting notifications"); |
|---|
| 209 |
auto notifications = fbclient.getNotifications (); |
|---|
| 210 |
logger.info ( |
|---|
| 211 |
" | " ~ toUtf8 (notifications.messages.unread) ~ " unread messages (" ~ dateTimeToUtf8 ( |
|---|
| 212 |
notifications.messages.mostRecent) ~ ")"); |
|---|
| 213 |
logger.info ( |
|---|
| 214 |
" | " ~ toUtf8 (notifications.pokes.unread) ~ " unread pokes (" ~ dateTimeToUtf8 ( |
|---|
| 215 |
notifications.pokes.mostRecent) ~ ")"); |
|---|
| 216 |
logger.info ( |
|---|
| 217 |
" | " ~ toUtf8 (notifications.shares.unread) ~ " unread shares (" ~ dateTimeToUtf8 ( |
|---|
| 218 |
notifications.shares.mostRecent) ~ ")"); |
|---|
| 219 |
logger.info ( |
|---|
| 220 |
" | " ~ toUtf8 (notifications.friendRequests.length) ~ " friend requests"); |
|---|
| 221 |
auto groups = notifications.groupInvites; |
|---|
| 222 |
logger.info (" | " ~ toUtf8 (groups.length) ~ " group invites"); |
|---|
| 223 |
foreach (g; groups) |
|---|
| 224 |
logger.info (" | " ~ toUtf8 (g)); |
|---|
| 225 |
auto events = notifications.eventInvites; |
|---|
| 226 |
logger.info (" | " ~ toUtf8 (events.length) ~ " event invites"); |
|---|
| 227 |
foreach (e; events) |
|---|
| 228 |
logger.info (" | " ~ toUtf8 (e)); |
|---|
| 229 |
} |
|---|
| 230 |
|
|---|
| 231 |
logger.info ("getting albums"); |
|---|
| 232 |
auto albums = fbclient.getThisUserAlbums (); |
|---|
| 233 |
foreach (alb; albums) |
|---|
| 234 |
with (alb) |
|---|
| 235 |
logger.info ( |
|---|
| 236 |
" | " ~ id ~ ": " ~ name ~ " (" ~ description ~ ") @ " ~ location ~ ", " ~ dateTimeToUtf8 ( |
|---|
| 237 |
created) ~ " (" ~ dateTimeToUtf8 (modified) ~ ") by " ~ ownerId ~ " - cover: " ~ coverPhotoId ~ " photos: " ~ .toUtf8 ( |
|---|
| 238 |
size) ~ " URL: " ~ link); |
|---|
| 239 |
|
|---|
| 240 |
if (albums.length > 0) |
|---|
| 241 |
{ |
|---|
| 242 |
logger.info ("getting one photo"); |
|---|
| 243 |
auto photos = fbclient.getPhotosById (albums[0].coverPhotoId); |
|---|
| 244 |
foreach (p; photos) |
|---|
| 245 |
with (p) |
|---|
| 246 |
logger.info ( |
|---|
| 247 |
" | " ~ id ~ ": " ~ caption ~ " by " ~ ownerId ~ " in " ~ albumId ~ ", " ~ dateTimeToUtf8 ( |
|---|
| 248 |
created) ~ " # " ~ src ~ " # " ~ bigSrc ~ " # " ~ smallSrc ~ " # " ~ link); |
|---|
| 249 |
} |
|---|
| 250 |
|
|---|
| 251 |
version (none) |
|---|
| 252 |
{ |
|---|
| 253 |
logger.info ("setting test preference"); |
|---|
| 254 |
fbclient.setUserPreference (0, "foo"); |
|---|
| 255 |
logger.info ("getting test preference"); |
|---|
| 256 |
auto pref = fbclient.getUserPreference (1); |
|---|
| 257 |
logger.info (" | test preference is: " ~ pref); |
|---|
| 258 |
|
|---|
| 259 |
logger.info ("setting test preferences"); |
|---|
| 260 |
fbclient.setUserPreferences ([1, 2, 3], ["bar", "baz", "bat"], false); |
|---|
| 261 |
|
|---|
| 262 |
logger.info ("getting test preferences"); |
|---|
| 263 |
auto prefs = fbclient.getUserPreferences (); |
|---|
| 264 |
} |
|---|
| 265 |
|
|---|
| 266 |
try |
|---|
| 267 |
{ |
|---|
| 268 |
logger.info ("creating object type"); |
|---|
| 269 |
fbclient.createObjectType ("test"); |
|---|
| 270 |
|
|---|
| 271 |
logger.info ("renaming object type"); |
|---|
| 272 |
fbclient.renameObjectType ("test", "foo"); |
|---|
| 273 |
|
|---|
| 274 |
logger.info ("defining object property"); |
|---|
| 275 |
fbclient.defineObjectProperty ("foo", "bar", |
|---|
| 276 |
PropertyInfo.PropertyType.STRING); |
|---|
| 277 |
|
|---|
| 278 |
logger.info ("renaming object property"); |
|---|
| 279 |
fbclient.renameObjectProperty ("foo", "bar", "baz"); |
|---|
| 280 |
|
|---|
| 281 |
logger.info ("getting object type"); |
|---|
| 282 |
auto props = fbclient.getObjectType ("foo"); |
|---|
| 283 |
foreach (pi; props) |
|---|
| 284 |
logger.info (" | " ~ pi.name ~ "," ~ .toUtf8 (pi.dataType)); |
|---|
| 285 |
|
|---|
| 286 |
logger.info ("creating object"); |
|---|
| 287 |
auto id = fbclient.createObject ("foo", ["baz"], ["abc"]); |
|---|
| 288 |
|
|---|
| 289 |
logger.info ("setting object property"); |
|---|
| 290 |
fbclient.setObjectProperty (id, "baz", "xyz"); |
|---|
| 291 |
|
|---|
| 292 |
logger.info ("getting object property"); |
|---|
| 293 |
auto prop = fbclient.getObjectProperty (id, "baz"); |
|---|
| 294 |
logger.info (" | baz=" ~ prop); |
|---|
| 295 |
} |
|---|
| 296 |
finally |
|---|
| 297 |
{ |
|---|
| 298 |
logger.info ("getting object types"); |
|---|
| 299 |
auto otypes = fbclient.getObjectTypes (); |
|---|
| 300 |
logger.info (" | " ~ join (otypes, ",")); |
|---|
| 301 |
logger.info ("dropping object type"); |
|---|
| 302 |
foreach (t; otypes) |
|---|
| 303 |
fbclient.dropObjectType (t); |
|---|
| 304 |
} |
|---|
| 305 |
|
|---|
| 306 |
return 0; |
|---|
| 307 |
} |
|---|
| 308 |
|
|---|
| 309 |
char[] dateTimeToUtf8 (DateTime dt) |
|---|
| 310 |
{ |
|---|
| 311 |
return .toUtf8 (dt.year) ~ "-" ~ .toUtf8 (dt.month) ~ "-" ~ .toUtf8 (dt.day); |
|---|
| 312 |
} |
|---|