forked from Entonsky/GooglePhotoOrganizer
/
PicasaClient.cs
328 lines (262 loc) · 10.5 KB
/
PicasaClient.cs
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
using System;
using System.Collections.Generic;
using Google.GData.Photos;
using Google.GData.Client;
using Google.Picasa;
namespace GooglePhotoOrganizer
{
class PicasaClient
{
public PicasaClient()
{
GetPicasaService();
}
PicasaService _picasaService = null;
public PicasaService GetPicasaService(bool recreate = false)
{
if (recreate)
{
//Recreate service
_picasaService = null;
//Get google drive about to sync autorization
//Because unknown problems in picasa autorization
var drive = new GoogleDriveClient();
drive.GetAbout();
}
if (_picasaService == null)
{
var creditals = GoogleDriveClient.GetCreditals();
var requestFactory = new GDataRequestFactory(null);
requestFactory.CustomHeaders.Add("Authorization: Bearer " + creditals.Token.AccessToken);
requestFactory.CustomHeaders.Add("Gdata-version: 2");
_picasaService = new PicasaService("api-project");
_picasaService.RequestFactory = requestFactory;
}
return _picasaService;
}
public void Test()
{
RequestSettings settings = new RequestSettings("yourApp");
settings.PageSize = 50000;
settings.AutoPaging = true;
PicasaRequest pr = new PicasaRequest(settings);
pr.Service = GetPicasaService();
Feed<Photo> feed = pr.GetPhotos();
int cnt = 0;
Photo x = null;
foreach (Photo p in feed.Entries)
{
if (p.Title.ToLower() == "2005-12-16Kovalev_Zachet.avi".ToLower())
{
x = p;
break;
}
cnt++;
}
var longTime = (DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1))).TotalMilliseconds;
x.Timestamp = Convert.ToUInt64(longTime);
pr.Update(x);
Console.WriteLine(cnt);
}
private List<PicasaEntry> GetItems(string uri, PicasaQuery.Kinds kind, string searchText = null)
{
var service = GetPicasaService();
KindQuery query;
switch (kind)
{
case PicasaQuery.Kinds.album:
query = new AlbumQuery(uri);
break;
case PicasaQuery.Kinds.photo:
query = new PhotoQuery(uri);
if (!String.IsNullOrWhiteSpace(searchText))
query.Query = searchText;
break;
default:
throw new NotImplementedException("Unknown kind");
}
query.NumberToRetrieve = 1000;
PicasaFeed feed;
try
{
feed = service.Query(query);
}
catch
{
//If a first error. Try recreate service
service = GetPicasaService(true);
feed = service.Query(query);
}
var result = new List<PicasaEntry>();
while (feed.Entries!=null)
{
foreach (PicasaEntry entry in feed.Entries)
result.Add(entry);
if (String.IsNullOrWhiteSpace(feed.NextChunk))
break;
switch (kind)
{
case PicasaQuery.Kinds.album:
query = new AlbumQuery(feed.NextChunk);
break;
case PicasaQuery.Kinds.photo:
query = new PhotoQuery(feed.NextChunk);
break;
default:
throw new NotImplementedException("Unknown kind");
}
try
{
feed = service.Query(query);
}
catch
{
//If a first error. Try recreate service
service = GetPicasaService(true);
feed = service.Query(query);
}
}
return result;
}
public List<AlbumAccessor> GetAlbums()
{
string uri = PicasaQuery.CreatePicasaUri("default");
var data = GetItems(uri, PicasaQuery.Kinds.album);
var result = new List<AlbumAccessor>();
foreach (PicasaEntry entry in data)
{
var album = new AlbumAccessor(entry);
result.Add(album);
}
return result;
}
public List<PicasaEntry> GetPhotos(string albumId = null, string searchText = null)
{
string uri;
if (albumId == null)
uri = PicasaQuery.CreatePicasaUri("default");
else
uri = PicasaQuery.CreatePicasaUri("default", albumId);
var data = GetItems(uri, PicasaQuery.Kinds.photo, searchText);
var result = new List<PicasaEntry>();
foreach (var item in data)
result.Add(item);
return result;
}
public AlbumAccessor CreateAlbum(string title, string description = null, bool isPublic = false)
{
var service = GetPicasaService();
AlbumEntry newEntry = new AlbumEntry();
newEntry.Title.Text = title;
if (description!=null)
newEntry.Summary.Text = description;
AlbumAccessor ac = new AlbumAccessor(newEntry);
//set to "private" for a private album
if (isPublic)
ac.Access = "public";
else
ac.Access = "unlisted";
Uri feedUri = new Uri(PicasaQuery.CreatePicasaUri("default"));
PicasaEntry createdEntry;
try
{
createdEntry = (PicasaEntry)service.Insert(feedUri, newEntry);
}
catch
{
//If a first error. Try recreate service
service = GetPicasaService(true);
createdEntry = (PicasaEntry)service.Insert(feedUri, newEntry);
}
return new AlbumAccessor(createdEntry);
}
public void MovePhotoToAlbum(PicasaEntry photo, string newAlbumId)
{
PhotoAccessor ac = new PhotoAccessor(photo);
ac.AlbumId = newAlbumId;
try
{
PicasaEntry updatedEntry = (PicasaEntry)photo.Update();
}
catch
{
var service = GetPicasaService(true);
photo.Service = service;
PicasaEntry updatedEntry = (PicasaEntry)photo.Update();
}
}
public void SetPhotoCreationDate(PicasaEntry photo, DateTime creationDate)
{
//PhotoAccessor ac = new PhotoAccessor(photo);
var longTime = (creationDate.ToUniversalTime().Subtract(new DateTime(1970, 1, 1))).TotalMilliseconds;
//ac.Timestamp = Convert.ToUInt64(longTime);
//photo.Published = creationDate.ToUniversalTime();
photo.SetPhotoExtensionValue("timestamp", Convert.ToUInt64(longTime).ToString());
try
{
PicasaEntry updatedEntry = (PicasaEntry)photo.Update();
}
catch
{
var service = GetPicasaService(true);
photo.Service = service;
PicasaEntry updatedEntry = (PicasaEntry)photo.Update();
}
}
public void DeletePhoto(PicasaEntry photo)
{
var service = GetPicasaService();
try
{
photo.Delete();
}
catch
{
//If a first error. Try recreate service
service = GetPicasaService(true);
photo.Delete();
}
}
public PicasaEntry UploadPhoto(string filePath, string albumId = "default")
{
var service = GetPicasaService();
Uri postUri = new Uri(PicasaQuery.CreatePicasaUri("default", albumId));
//postUri = new Uri("https://www.googleapis.com/resumable/upload/plus/v1whitelisted/mediasets/me.cinstant/mediaBackground?uploadType=resumable&imageSize=1024&mediaType=photo&storage=**standard**&remainingMediaCount=111");
System.IO.FileInfo fileInfo = new System.IO.FileInfo(filePath);
System.IO.FileStream fileStream = fileInfo.OpenRead();
PicasaEntry entry;
try
{
entry = (PicasaEntry)service.Insert(postUri, fileStream, "image/jpeg", filePath);
}
catch
{
//If a first error. Try recreate service
service = GetPicasaService(true);
entry = (PicasaEntry)service.Insert(postUri, fileStream, "image/jpeg", filePath);
}
fileStream.Close();
return entry;
}
/*
public PicasaEntry GetItemById(string id)
{
PicasaEntry e = new PicasaEntry();
e.Id = new AtomId(id);
return (PicasaEntry)e.Update();
}
public void UploadPhoto2(string filePath, string albumId = "default")
{
var service = GetPicasaService();
//Uri postUri = new Uri(PicasaQuery.CreatePicasaUri("default", albumId));
Uri postUri = new Uri("https://www.googleapis.com/resumable/upload/plus/v1whitelisted/mediasets/me.cinstant/mediaBackground?uploadType=resumable&imageSize=1024&mediaType=photo&storage=**standard**&remainingMediaCount=111");
System.IO.FileInfo fileInfo = new System.IO.FileInfo(filePath);
System.IO.FileStream fileStream = fileInfo.OpenRead();
PicasaEntry entry = (PicasaEntry)service.Insert(postUri, fileStream, "image/jpeg", filePath);
//fullRes
///resumable/upload/plus/v1whitelisted/mediasets/me.cinstant/mediaBackground?uploadType=resumable&imageSize=1024&mediaType=photo&storage=**full**&remainingMediaCount=111
//new Uri("https://picasaweb.google.com/data/feed/api/user/default/albumid/default");
fileStream.Close();
}*/
}
}