~ubuntu-branches/debian/squeeze/f-spot/squeeze

« back to all changes in this revision

Viewing changes to extensions/Exporters/SmugMugExport/SmugMugNet/SmugMugApi.cs

  • Committer: Bazaar Package Importer
  • Author(s): Iain Lane, Mirco Bauer, Iain Lane
  • Date: 2009-02-07 20:23:32 UTC
  • mfrom: (1.1.18 upstream)
  • Revision ID: james.westby@ubuntu.com-20090207202332-oc93rfjo1st0571s
Tags: 0.5.0.3-2
[ Mirco Bauer]
* Upload to unstable.
* debian/control:
  + Lowered GNOME# build-deps to 2.0 ABI as that transition didn't happen
    yet in unstable.

[ Iain Lane ]
* debian/patches/svn-r4545_locales-import.dpatch: Patch backported from SVN
  trunk revision 4545 - initialize the translation catalog earlier (LP: #293305)
  (Closes: #514457). Thanks to Florian Heinle for finding the patch and to
  Chris Coulson for preparing the update.
* debian/control: Build-depend on libmono-dev (>= 1.2.4) to match configure
  checks.
* debian/rules: Pass CSC=/usr/bin/csc to configure for gio-sharp to fix FTBFS

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * SmugMugApi.cs
 
3
 *
 
4
 * Authors:
 
5
 *   Thomas Van Machelen <thomas.vanmachelen@gmail.com>
 
6
 *
 
7
 * Copyright (C) 2006 Thomas Van Machelen
 
8
 * This is free software. See COPYING for details.
 
9
 *
 
10
 */
 
11
 
 
12
using System;
 
13
using System.Collections.Generic;
 
14
using System.Text;
 
15
using System.Net;
 
16
using System.IO;
 
17
using System.Text.RegularExpressions;
 
18
using System.Xml;
 
19
using System.Collections.Specialized;
 
20
 
 
21
namespace SmugMugNet
 
22
{
 
23
        public struct Credentials
 
24
        {
 
25
                public string session_id;
 
26
                public int user_id;
 
27
                public string password_hash;
 
28
 
 
29
                public string SessionID {
 
30
                        get { return session_id; }
 
31
                        set { session_id = value; }
 
32
                }
 
33
 
 
34
                public int UserID {
 
35
                        get { return user_id; }
 
36
                }
 
37
 
 
38
                public string PasswordHash {
 
39
                        get { return password_hash; }
 
40
                }
 
41
 
 
42
                public Credentials(string session_id, int user_id, string password_hash)
 
43
                {
 
44
                        this.session_id = session_id;
 
45
                        this.user_id = user_id;
 
46
                        this.password_hash = password_hash;
 
47
                }
 
48
        }
 
49
 
 
50
        public struct Category
 
51
        {
 
52
                public Category( string title, int id)
 
53
                {
 
54
                        this.title = title;
 
55
                        this.category_id = id;
 
56
                }
 
57
 
 
58
                private int category_id;
 
59
                public int CategoryID
 
60
                {
 
61
                        get { return category_id; }
 
62
                        set { category_id = value; }
 
63
                }
 
64
 
 
65
                private string title;
 
66
                public string Title
 
67
                {
 
68
                        get { return title; }
 
69
                        set { title = value; }
 
70
                }
 
71
        }
 
72
 
 
73
        public struct Album
 
74
        {
 
75
                public Album(string title, int id)
 
76
                {
 
77
                        this.album_id = id;
 
78
                        this.title = title;
 
79
                }
 
80
 
 
81
                private int album_id;
 
82
                public int AlbumID
 
83
                {
 
84
                        get { return album_id; }
 
85
                        set { album_id = value; }
 
86
                }
 
87
 
 
88
                private string title;
 
89
                public string Title
 
90
                {
 
91
                        get { return title; }
 
92
                        set { title = value; }
 
93
                }
 
94
        }
 
95
 
 
96
        public class SmugMugApi
 
97
        {
 
98
                private string username = String.Empty;
 
99
                private string password = String.Empty;
 
100
                private bool connected = false;
 
101
 
 
102
                private Credentials credentials;
 
103
                private const string VERSION = "1.1.1";
 
104
                private Category[] categories;
 
105
 
 
106
                public bool Connected
 
107
                {
 
108
                        get { return connected; }
 
109
                }
 
110
 
 
111
                public SmugMugApi (string email_address, string password)
 
112
                {
 
113
                        this.username = email_address;
 
114
                        this.password = password;
 
115
                }
 
116
 
 
117
                public bool Login ()
 
118
                {
 
119
                        if (this.username.Length == 0 | this.password.Length == 0)
 
120
                        {
 
121
                                throw new SmugMugException("There is no username or password.");
 
122
                        }
 
123
 
 
124
                        if (this.connected == false && this.credentials.UserID == 0)
 
125
                        {
 
126
                                try
 
127
                                {
 
128
                                        this.credentials = SmugMugProxy.LoginWithPassword (this.username, this.password);
 
129
                                        this.connected = true;
 
130
                                }
 
131
                                catch
 
132
                                {
 
133
                                        return false;
 
134
                                }
 
135
                        }
 
136
                        else
 
137
                        {
 
138
                                LoginWithHash ();
 
139
                        }
 
140
 
 
141
                        return true;
 
142
                }
 
143
 
 
144
                private void LoginWithHash ()
 
145
                {
 
146
                        try {
 
147
                                string session_id = SmugMugProxy.LoginWithHash (this.credentials.UserID, this.credentials.PasswordHash);
 
148
 
 
149
                                if (session_id != null && session_id.Length > 0)
 
150
                                {
 
151
                                        this.credentials.SessionID = session_id;
 
152
                                }
 
153
                                else
 
154
                                {
 
155
                                        throw new SmugMugException ("SessionID was empty");
 
156
                                }
 
157
                        }
 
158
                        catch (Exception ex) {
 
159
                                throw new SmugMugException ("A login error occured, SessionID may be invalid.", ex.InnerException);
 
160
                        }
 
161
                }
 
162
 
 
163
                public void Logout ()
 
164
                {
 
165
                        if (!connected)
 
166
                                return;
 
167
 
 
168
                        if (this.credentials.SessionID == null && this.credentials.SessionID.Length == 0)
 
169
                                return;
 
170
 
 
171
                        SmugMugProxy.Logout (this.credentials.SessionID);
 
172
                        connected = false;
 
173
                        this.credentials = new Credentials (null, 0, null);
 
174
                }
 
175
 
 
176
                public Category[] GetCategories ()
 
177
                {
 
178
                        if (this.categories == null)
 
179
                        {
 
180
                                try {
 
181
                                        this.categories = SmugMugProxy.GetCategories (credentials.SessionID);
 
182
                                }
 
183
                                catch (Exception ex) {
 
184
                                        throw new SmugMugException ("Could not retrieve Categories", ex.InnerException);
 
185
                                }
 
186
                        }
 
187
                        return this.categories;
 
188
                }
 
189
 
 
190
                public Album CreateAlbum (string title, int category_id, bool is_public)
 
191
                {
 
192
                        try {
 
193
                                return SmugMugProxy.CreateAlbum (title, category_id, credentials.SessionID, is_public);
 
194
                        }
 
195
                        catch (Exception ex) {
 
196
                                throw new SmugMugException ("Could not create album", ex.InnerException);
 
197
                        }
 
198
                }
 
199
 
 
200
                public Album[] GetAlbums ()
 
201
                {
 
202
                        try {
 
203
                                return SmugMugProxy.GetAlbums(credentials.SessionID);
 
204
                        }
 
205
                        catch (Exception ex) {
 
206
                                throw new SmugMugException ("Could not get albums", ex.InnerException);
 
207
                        }
 
208
                }
 
209
 
 
210
                public Uri GetAlbumUrl (int image_id)
 
211
                {
 
212
                        try {
 
213
                                return SmugMugProxy.GetAlbumUrl (image_id, credentials.SessionID);
 
214
                        }
 
215
                        catch (Exception ex) {
 
216
                                throw new SmugMugException ("Could not get album url", ex.InnerException);
 
217
                        }
 
218
                }
 
219
 
 
220
                public int Upload (string path, int album_id)
 
221
                {
 
222
                        try {
 
223
                                return SmugMugProxy.Upload (path, album_id, credentials.SessionID);
 
224
                        }
 
225
                        catch (Exception ex) {
 
226
                                throw new SmugMugException ("Could not upload file", ex.InnerException);
 
227
                        }
 
228
                }
 
229
        }
 
230
 
 
231
        public class SmugMugProxy
 
232
        {
 
233
                // FIXME: this getting should be done over https
 
234
                private const string GET_URL = "https://api.SmugMug.com/hack/rest/";
 
235
                private const string POST_URL = "https://upload.SmugMug.com/hack/rest/";
 
236
                // key from massis
 
237
                private const string APIKEY = "umtr0zB2wzwTZDhF2BySidg0hY0le3K6";
 
238
                private const string VERSION = "1.1.1";
 
239
 
 
240
                // rest methods
 
241
                private const string LOGIN_WITHPASS_METHOD = "smugmug.login.withPassword";
 
242
                private const string LOGIN_WITHHASH_METHOD = "smugmug.login.withHash";
 
243
                private const string LOGOUT_METHOD = "smugmug.logout";
 
244
                private const string ALBUMS_CREATE_METHOD = "smugmug.albums.create";
 
245
                private const string ALBUMS_GET_URLS_METHOD = "smugmug.images.getURLs";
 
246
                private const string ALBUMS_GET_METHOD = "smugmug.albums.get";
 
247
                private const string CATEGORIES_GET_METHOD = "smugmug.categories.get";
 
248
 
 
249
                // parameter constants
 
250
                private const string EMAIL = "EmailAddress";
 
251
                private const string PASSWORD = "Password";
 
252
                private const string USER_ID = "UserID";
 
253
                private const string PASSWORD_HASH = "PasswordHash";
 
254
                private const string SESSION_ID = "SessionID";
 
255
                private const string CATEGORY_ID = "CategoryID";
 
256
                private const string IMAGE_ID = "ImageID";
 
257
                private const string TITLE = "Title";
 
258
                private const string ID = "id";
 
259
 
 
260
                public static Credentials LoginWithPassword (string username, string password)
 
261
                {
 
262
                        string url = FormatGetUrl (LOGIN_WITHPASS_METHOD, new SmugMugParam (EMAIL, username), new SmugMugParam (PASSWORD, password));
 
263
                        XmlDocument doc = GetResponseXml (url);
 
264
 
 
265
                        string sessionId = doc.SelectSingleNode ("/rsp/Login/SessionID").InnerText;
 
266
                        int userId = int.Parse (doc.SelectSingleNode ("/rsp/Login/UserID").InnerText);
 
267
                        string passwordHash = doc.SelectSingleNode ("/rsp/Login/PasswordHash").InnerText;
 
268
 
 
269
                        return new Credentials (sessionId, userId, passwordHash);
 
270
                }
 
271
 
 
272
                public static string LoginWithHash (int user_id, string password_hash)
 
273
                {
 
274
                        string url = FormatGetUrl (LOGIN_WITHHASH_METHOD, new SmugMugParam (USER_ID, user_id), new SmugMugParam (PASSWORD_HASH, password_hash));
 
275
                        XmlDocument doc = GetResponseXml(url);
 
276
 
 
277
                        return doc.SelectSingleNode ("/rsp/Login/SessionID").InnerText;
 
278
                }
 
279
 
 
280
                public static void Logout (string session_id)
 
281
                {
 
282
                        string url = FormatGetUrl (LOGOUT_METHOD, new SmugMugParam (SESSION_ID, session_id));
 
283
                        GetResponseXml (url);
 
284
                }
 
285
 
 
286
                public static Album[] GetAlbums (string session_id)
 
287
                {
 
288
                        string url = FormatGetUrl (ALBUMS_GET_METHOD, new SmugMugParam(SESSION_ID, session_id));
 
289
                        XmlDocument doc = GetResponseXml (url);
 
290
                        XmlNodeList albumNodes = doc.SelectNodes ("/rsp/Albums/Album");
 
291
 
 
292
                        Album[] albums = new Album[albumNodes.Count];
 
293
 
 
294
                        for (int i = 0; i < albumNodes.Count; i++)
 
295
                        {
 
296
                                XmlNode current = albumNodes[i];
 
297
                                albums[i] = new Album (current.SelectSingleNode (TITLE).InnerText, int.Parse (current.Attributes[ID].Value));
 
298
                        }
 
299
                        return albums;
 
300
                }
 
301
 
 
302
                public static Uri GetAlbumUrl (int image_id, string session_id)
 
303
                {
 
304
                        string url = FormatGetUrl(ALBUMS_GET_URLS_METHOD, new SmugMugParam(IMAGE_ID, image_id), new SmugMugParam(SESSION_ID, session_id));
 
305
                        XmlDocument doc = GetResponseXml(url);
 
306
 
 
307
                        string album_url = doc.SelectSingleNode("/rsp/ImageURLs/Image/AlbumURL").InnerText;
 
308
 
 
309
                        return new Uri(album_url);
 
310
                }
 
311
 
 
312
                public static Category[] GetCategories (string session_id)
 
313
                {
 
314
                        string url = FormatGetUrl(CATEGORIES_GET_METHOD, new SmugMugParam (SESSION_ID, session_id));
 
315
                        XmlDocument doc = GetResponseXml (url);
 
316
 
 
317
                        XmlNodeList categoryNodes = doc.SelectNodes ("/rsp/Categories/Category");
 
318
                        Category[] categories = new Category[categoryNodes.Count];
 
319
 
 
320
                        for (int i = 0; i < categoryNodes.Count; i++)
 
321
                        {
 
322
                                XmlNode current = categoryNodes[i];
 
323
                                categories[i] = new Category (current.SelectSingleNode (TITLE).InnerText, int.Parse (current.Attributes[ID].Value));
 
324
                        }
 
325
                        return categories;
 
326
                }
 
327
 
 
328
                public static Album CreateAlbum (string title, int category_id, string session_id)
 
329
                {
 
330
                        return CreateAlbum (title, category_id, session_id, true);
 
331
                }
 
332
 
 
333
                public static Album CreateAlbum (string title, int category_id, string session_id, bool is_public)
 
334
                {
 
335
                        int public_int = is_public ? 1 : 0;
 
336
                        string url = FormatGetUrl (ALBUMS_CREATE_METHOD, new SmugMugParam (TITLE, title), new SmugMugParam (CATEGORY_ID, category_id), new SmugMugParam (SESSION_ID, session_id), new SmugMugParam ("Public", public_int));
 
337
                        XmlDocument doc = GetResponseXml (url);
 
338
 
 
339
                        int id = int.Parse(doc.SelectSingleNode("/rsp/Create/Album").Attributes[ID].Value);
 
340
 
 
341
                        return new Album(title, id);
 
342
                }
 
343
 
 
344
                public static int Upload (string path, int album_id, string session_id)
 
345
                {
 
346
                        FileInfo file = new FileInfo(path);
 
347
 
 
348
                        if (!file.Exists)
 
349
                                throw new ArgumentException("Image does not exist: " + file.FullName);
 
350
 
 
351
                        try
 
352
                        {
 
353
                                WebClient client = new WebClient ();
 
354
                                client.BaseAddress = "http://upload.smugmug.com";
 
355
                                client.Headers.Add ("Cookie:SMSESS=" + session_id);
 
356
 
 
357
                                NameValueCollection queryStringCollection = new NameValueCollection ();
 
358
                                queryStringCollection.Add ("AlbumID", album_id.ToString());
 
359
                                // Temporarily disabled because rest doesn't seem to return the ImageID anymore
 
360
                                // queryStringCollection.Add ("ResponseType", "REST");
 
361
                                // luckily JSON still holds it
 
362
                                queryStringCollection.Add ("ResponseType", "JSON");
 
363
                                client.QueryString = queryStringCollection;
 
364
 
 
365
                                byte[] responseArray = client.UploadFile ("http://upload.smugmug.com/photos/xmladd.mg", "POST", file.FullName);
 
366
                                string response = Encoding.ASCII.GetString (responseArray);
 
367
 
 
368
                                // JSon approach
 
369
                                Regex id_regex = new Regex ("\\\"id\\\":( )?(?<image_id>\\d+),");
 
370
                                Match m  = id_regex.Match (response);
 
371
 
 
372
                                int id = -1;
 
373
 
 
374
                                if (m.Success)
 
375
                                        id = int.Parse (m.Groups["image_id"].Value);
 
376
 
 
377
                                return id;
 
378
 
 
379
                                // REST approach, disabled for now
 
380
                                //XmlDocument doc = new XmlDocument ();
 
381
                                //doc.LoadXml (response);
 
382
                                // return int.Parse (doc.SelectSingleNode ("/rsp/ImageID").InnerText);
 
383
 
 
384
                        }
 
385
                        catch (Exception ex)
 
386
                        {
 
387
                                throw new SmugMugUploadException ("Error uploading image: " + file.FullName, ex.InnerException);
 
388
                        }
 
389
                }
 
390
 
 
391
                private static string FormatGetUrl(string method_name, params SmugMugParam[] parameters)
 
392
                {
 
393
                        StringBuilder builder = new StringBuilder (string.Format ("{0}{1}/?method={2}", GET_URL, VERSION, method_name));
 
394
 
 
395
                        foreach (SmugMugParam param in parameters)
 
396
                                builder.Append (param.ToString ());
 
397
 
 
398
                        builder.Append (new SmugMugParam ("APIKey", APIKEY));
 
399
                        return builder.ToString();
 
400
                }
 
401
 
 
402
                private static XmlDocument GetResponseXml (string url)
 
403
                {
 
404
                        HttpWebRequest request = HttpWebRequest.Create (url) as HttpWebRequest;
 
405
                        request.Credentials = CredentialCache.DefaultCredentials;
 
406
                        WebResponse response = request.GetResponse ();
 
407
 
 
408
                        XmlDocument doc = new XmlDocument ();
 
409
                        doc.LoadXml (new StreamReader (response.GetResponseStream ()).ReadToEnd ());
 
410
                        CheckResponseXml (doc);
 
411
 
 
412
                        response.Close ();
 
413
                        return doc;
 
414
                }
 
415
 
 
416
                private static void CheckResponseXml (XmlDocument doc)
 
417
                {
 
418
                        if (doc.SelectSingleNode("/rsp").Attributes["stat"].Value == "ok")
 
419
                                return;
 
420
 
 
421
                        string message = doc.SelectSingleNode ("/rsp/err").Attributes["msg"].Value;
 
422
                        throw new SmugMugException (message);
 
423
                }
 
424
 
 
425
                private class SmugMugParam
 
426
                {
 
427
                        string name;
 
428
                        object value;
 
429
 
 
430
                        public SmugMugParam (string name, object value)
 
431
                        {
 
432
                                this.name = name;
 
433
                                this.value = value;
 
434
                        }
 
435
 
 
436
                        public string Name
 
437
                        {
 
438
                                get {return name;}
 
439
                        }
 
440
 
 
441
                        public object Value
 
442
                        {
 
443
                                get {return value;}
 
444
                        }
 
445
 
 
446
                        public override string ToString()
 
447
                        {
 
448
                                return string.Format("&{0}={1}", Name, Value);
 
449
                        }
 
450
                }
 
451
        }
 
452
 
 
453
        public class SmugMugException : ApplicationException
 
454
        {
 
455
                public SmugMugException(string message) : base (message)
 
456
                {
 
457
                        Console.WriteLine (message);
 
458
                }
 
459
 
 
460
                public SmugMugException (string message, Exception innerException) : base (message, innerException)
 
461
                {
 
462
                        Console.WriteLine (message, innerException.ToString());
 
463
                }
 
464
        }
 
465
 
 
466
        public sealed class SmugMugUploadException : ApplicationException
 
467
        {
 
468
                public SmugMugUploadException (string message, Exception innerException) : base (message, innerException)
 
469
                {
 
470
                        Console.WriteLine (message, innerException.ToString ());
 
471
                }
 
472
        }
 
473
}