Added album selection to Photobucket

git-svn-id: http://svn.code.sf.net/p/greenshot/code/trunk@2424 7dccd23d-a4a3-4e1f-8c07-b4c1b4018ab4
This commit is contained in:
RKrom 2013-01-13 12:17:28 +00:00
commit 3fa043782f
4 changed files with 150 additions and 50 deletions

View file

@ -19,7 +19,9 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>. * along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
using System.ComponentModel; using System.ComponentModel;
using System.Collections.Generic;
using System.Drawing; using System.Drawing;
using Greenshot.IniFile; using Greenshot.IniFile;
using Greenshot.Plugin; using Greenshot.Plugin;
using GreenshotPlugin.Core; using GreenshotPlugin.Core;
@ -32,9 +34,16 @@ namespace GreenshotPhotobucketPlugin {
private static log4net.ILog LOG = log4net.LogManager.GetLogger(typeof(PhotobucketDestination)); private static log4net.ILog LOG = log4net.LogManager.GetLogger(typeof(PhotobucketDestination));
private static PhotobucketConfiguration config = IniConfig.GetIniSection<PhotobucketConfiguration>(); private static PhotobucketConfiguration config = IniConfig.GetIniSection<PhotobucketConfiguration>();
private PhotobucketPlugin plugin = null; private PhotobucketPlugin plugin = null;
private string albumPath = null;
public PhotobucketDestination(PhotobucketPlugin plugin) { /// <summary>
/// Create a Photobucket destination, which also has the path to the album in it
/// </summary>
/// <param name="plugin"></param>
/// <param name="albumPath">path to the album, null for default</param>
public PhotobucketDestination(PhotobucketPlugin plugin, string albumPath) {
this.plugin = plugin; this.plugin = plugin;
this.albumPath = albumPath;
} }
public override string Designation { public override string Designation {
@ -45,6 +54,9 @@ namespace GreenshotPhotobucketPlugin {
public override string Description { public override string Description {
get { get {
if (albumPath != null) {
return albumPath;
}
return Language.GetString("photobucket", LangKey.upload_menu_item); return Language.GetString("photobucket", LangKey.upload_menu_item);
} }
} }
@ -56,10 +68,34 @@ namespace GreenshotPhotobucketPlugin {
} }
} }
public override bool isDynamic {
get {
return true;
}
}
public override IEnumerable<IDestination> DynamicDestinations() {
List<string> albums = PhotobucketUtils.RetrievePhotobucketAlbums();
if (albums == null || albums.Count == 0) {
yield break;
}
foreach(string album in albums) {
yield return new PhotobucketDestination(plugin, album);
}
}
/// <summary>
/// Export the capture to Photobucket
/// </summary>
/// <param name="manuallyInitiated"></param>
/// <param name="surface"></param>
/// <param name="captureDetails"></param>
/// <returns></returns>
public override ExportInformation ExportCapture(bool manuallyInitiated, ISurface surface, ICaptureDetails captureDetails) { public override ExportInformation ExportCapture(bool manuallyInitiated, ISurface surface, ICaptureDetails captureDetails) {
ExportInformation exportInformation = new ExportInformation(this.Designation, this.Description); ExportInformation exportInformation = new ExportInformation(this.Designation, this.Description);
string uploadURL = null; string uploadURL = null;
bool uploaded = plugin.Upload(captureDetails, surface, out uploadURL); bool uploaded = plugin.Upload(captureDetails, surface, albumPath, out uploadURL);
if (uploaded) { if (uploaded) {
exportInformation.ExportMade = true; exportInformation.ExportMade = true;
exportInformation.Uri = uploadURL; exportInformation.Uri = uploadURL;

View file

@ -26,7 +26,7 @@ namespace GreenshotPhotobucketPlugin
/// <summary> /// <summary>
/// Description of PhotobucketInfo. /// Description of PhotobucketInfo.
/// </summary> /// </summary>
public class PhotobucketInfo : IDisposable { public class PhotobucketInfo {
private static readonly log4net.ILog LOG = log4net.LogManager.GetLogger(typeof(PhotobucketInfo)); private static readonly log4net.ILog LOG = log4net.LogManager.GetLogger(typeof(PhotobucketInfo));
private string original; private string original;
@ -51,25 +51,11 @@ namespace GreenshotPhotobucketPlugin
} }
/// <summary> /// <summary>
/// The public accessible Dispose /// Parse the upload response
/// Will call the GarbageCollector to SuppressFinalize, preventing being cleaned twice
/// </summary> /// </summary>
public void Dispose() { /// <param name="response">XML</param>
Dispose(true); /// <returns>PhotobucketInfo object</returns>
GC.SuppressFinalize(this); public static PhotobucketInfo FromUploadResponse(string response) {
}
/// <summary>
/// This Dispose is called from the Dispose and the Destructor.
/// When disposing==true all non-managed resources should be freed too!
/// </summary>
/// <param name="disposing"></param>
protected virtual void Dispose(bool disposing) {
if (disposing) {
}
}
public static PhotobucketInfo ParseResponse(string response) {
LOG.Debug(response); LOG.Debug(response);
PhotobucketInfo PhotobucketInfo = new PhotobucketInfo(); PhotobucketInfo PhotobucketInfo = new PhotobucketInfo();
try { try {

View file

@ -45,7 +45,7 @@ namespace GreenshotPhotobucketPlugin {
} }
public IEnumerable<IDestination> Destinations() { public IEnumerable<IDestination> Destinations() {
yield return new PhotobucketDestination(this); yield return new PhotobucketDestination(this, null);
} }
public IEnumerable<IProcessor> Processors() { public IEnumerable<IProcessor> Processors() {
@ -114,7 +114,7 @@ namespace GreenshotPhotobucketPlugin {
/// <param name="surfaceToUpload">ISurface</param> /// <param name="surfaceToUpload">ISurface</param>
/// <param name="uploadURL">out string for the url</param> /// <param name="uploadURL">out string for the url</param>
/// <returns>true if the upload succeeded</returns> /// <returns>true if the upload succeeded</returns>
public bool Upload(ICaptureDetails captureDetails, ISurface surfaceToUpload, out string uploadURL) { public bool Upload(ICaptureDetails captureDetails, ISurface surfaceToUpload, string albumPath, out string uploadURL) {
SurfaceOutputSettings outputSettings = new SurfaceOutputSettings(config.UploadFormat, config.UploadJpegQuality, config.UploadReduceColors); SurfaceOutputSettings outputSettings = new SurfaceOutputSettings(config.UploadFormat, config.UploadJpegQuality, config.UploadReduceColors);
try { try {
string filename = Path.GetFileName(FilenameHelper.GetFilename(config.UploadFormat, captureDetails)); string filename = Path.GetFileName(FilenameHelper.GetFilename(config.UploadFormat, captureDetails));
@ -123,7 +123,7 @@ namespace GreenshotPhotobucketPlugin {
// Run upload in the background // Run upload in the background
new PleaseWaitForm().ShowAndWait(Attributes.Name, Language.GetString("photobucket", LangKey.communication_wait), new PleaseWaitForm().ShowAndWait(Attributes.Name, Language.GetString("photobucket", LangKey.communication_wait),
delegate() { delegate() {
photobucketInfo = PhotobucketUtils.UploadToPhotobucket(surfaceToUpload, outputSettings, captureDetails.Title, filename); photobucketInfo = PhotobucketUtils.UploadToPhotobucket(surfaceToUpload, outputSettings, albumPath, captureDetails.Title, filename);
} }
); );
// This causes an exeption if the upload failed :) // This causes an exeption if the upload failed :)

View file

@ -21,6 +21,7 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Drawing; using System.Drawing;
using System.Xml;
using Greenshot.IniFile; using Greenshot.IniFile;
using Greenshot.Plugin; using Greenshot.Plugin;
using GreenshotPlugin.Core; using GreenshotPlugin.Core;
@ -29,21 +30,69 @@ namespace GreenshotPhotobucketPlugin {
/// <summary> /// <summary>
/// Description of PhotobucketUtils. /// Description of PhotobucketUtils.
/// </summary> /// </summary>
public class PhotobucketUtils { public static class PhotobucketUtils {
private static readonly log4net.ILog LOG = log4net.LogManager.GetLogger(typeof(PhotobucketUtils)); private static readonly log4net.ILog LOG = log4net.LogManager.GetLogger(typeof(PhotobucketUtils));
private static PhotobucketConfiguration config = IniConfig.GetIniSection<PhotobucketConfiguration>(); private static readonly PhotobucketConfiguration config = IniConfig.GetIniSection<PhotobucketConfiguration>();
private PhotobucketUtils() {
}
/// <summary> /// <summary>
/// Do the actual upload to Photobucket /// Do the actual upload to Photobucket
/// For more details on the available parameters, see: http://api.Photobucket.com/resources_anon /// For more details on the available parameters, see: http://api.Photobucket.com/resources_anon
/// </summary> /// </summary>
/// <returns>PhotobucketResponse</returns> /// <returns>PhotobucketResponse</returns>
public static PhotobucketInfo UploadToPhotobucket(ISurface surfaceToUpload, SurfaceOutputSettings outputSettings, string title, string filename) { public static PhotobucketInfo UploadToPhotobucket(ISurface surfaceToUpload, SurfaceOutputSettings outputSettings, string albumPath, string title, string filename) {
string responseString; string responseString;
if (string.IsNullOrEmpty(albumPath)) {
albumPath = "!";
}
OAuthSession oAuth = createSession();
IDictionary<string, object> signedParameters = new Dictionary<string, object>();
// add album
if (albumPath == null) {
signedParameters.Add("id", config.Username);
} else {
signedParameters.Add("id", albumPath);
}
// add type
signedParameters.Add("type", "image");
// add title
if (title != null) {
signedParameters.Add("title", title);
}
// add filename
if (filename != null) {
signedParameters.Add("filename", filename);
}
IDictionary<string, object> unsignedParameters = new Dictionary<string, object>();
// Add image
unsignedParameters.Add("uploadfile", new SurfaceContainer(surfaceToUpload, outputSettings, filename));
try {
string apiUrl = "http://api.photobucket.com/album/!/upload";
responseString = oAuth.MakeOAuthRequest(HTTPMethod.POST, apiUrl, apiUrl.Replace("api.photobucket.com", config.SubDomain), signedParameters, unsignedParameters, null);
} catch (Exception ex) {
LOG.Error("Error uploading to Photobucket.", ex);
throw ex;
} finally {
if (!string.IsNullOrEmpty(oAuth.Token)) {
config.Token = oAuth.Token;
}
if (!string.IsNullOrEmpty(oAuth.TokenSecret)) {
config.TokenSecret = oAuth.TokenSecret;
}
}
LOG.Info(responseString);
PhotobucketInfo PhotobucketInfo = PhotobucketInfo.FromUploadResponse(responseString);
LOG.Debug("Upload to Photobucket was finished");
return PhotobucketInfo;
}
/// <summary>
/// Helper method to create an OAuth session object for contacting the Photobucket API
/// </summary>
/// <returns>OAuthSession</returns>
private static OAuthSession createSession() {
OAuthSession oAuth = new OAuthSession(PhotobucketCredentials.ConsumerKey, PhotobucketCredentials.ConsumerSecret); OAuthSession oAuth = new OAuthSession(PhotobucketCredentials.ConsumerKey, PhotobucketCredentials.ConsumerSecret);
oAuth.CheckVerifier = false; oAuth.CheckVerifier = false;
// This url is configured in the Photobucket API settings in the Photobucket site!! // This url is configured in the Photobucket API settings in the Photobucket site!!
@ -77,26 +126,22 @@ namespace GreenshotPhotobucketPlugin {
} }
oAuth.Token = config.Token; oAuth.Token = config.Token;
oAuth.TokenSecret = config.TokenSecret; oAuth.TokenSecret = config.TokenSecret;
return oAuth;
}
/// <summary>
/// Get list of photobucket albums
/// </summary>
/// <returns>List<string></returns>
public static List<string> RetrievePhotobucketAlbums() {
string responseString;
OAuthSession oAuth = createSession();
IDictionary<string, object> signedParameters = new Dictionary<string, object>(); IDictionary<string, object> signedParameters = new Dictionary<string, object>();
// add album
signedParameters.Add("id", config.Username);
// add type
signedParameters.Add("type", "image");
// add title
if (title != null) {
signedParameters.Add("title", title);
}
// add filename
if (filename != null) {
signedParameters.Add("filename", filename);
}
IDictionary<string, object> unsignedParameters = new Dictionary<string, object>();
// Add image
unsignedParameters.Add("uploadfile", new SurfaceContainer(surfaceToUpload, outputSettings, filename));
try { try {
string apiUrl = "http://api.photobucket.com/album/!/upload"; string apiUrl = string.Format("http://api.photobucket.com/album/{0}", config.Username);
responseString = oAuth.MakeOAuthRequest(HTTPMethod.POST, apiUrl, apiUrl.Replace("api.photobucket.com", config.SubDomain), signedParameters, unsignedParameters, null); responseString = oAuth.MakeOAuthRequest(HTTPMethod.GET, apiUrl, apiUrl.Replace("api.photobucket.com", config.SubDomain), signedParameters, null, null);
} catch (Exception ex) { } catch (Exception ex) {
LOG.Error("Error uploading to Photobucket.", ex); LOG.Error("Error uploading to Photobucket.", ex);
throw ex; throw ex;
@ -108,10 +153,43 @@ namespace GreenshotPhotobucketPlugin {
config.TokenSecret = oAuth.TokenSecret; config.TokenSecret = oAuth.TokenSecret;
} }
} }
LOG.Info(responseString); try {
PhotobucketInfo PhotobucketInfo = PhotobucketInfo.ParseResponse(responseString); XmlDocument doc = new XmlDocument();
doc.LoadXml(responseString);
List<string> albums = new List<string>();
recurseAlbums(albums, null, doc.GetElementsByTagName("content").Item(0).ChildNodes);
LOG.DebugFormat("Albums: {0}", string.Join(",", albums.ToArray()));
return albums;
} catch(Exception e) {
LOG.Error("Error while Reading albums: ", e);
}
LOG.Debug("Upload to Photobucket was finished"); LOG.Debug("Upload to Photobucket was finished");
return PhotobucketInfo; return null;
}
/// <summary>
/// Parse the album nodes recursively
/// </summary>
/// <param name="albums"></param>
/// <param name="path"></param>
/// <param name="nodes"></param>
private static void recurseAlbums(List<string>albums, string path, XmlNodeList nodes) {
foreach(XmlNode node in nodes) {
if (node.Name != "album") {
continue;
}
string currentAlbum = node.Attributes["name"].Value;
string currentPath = currentAlbum;
if (path != null && path.Length > 0) {
currentPath = string.Format("{0}/{1}", path, currentAlbum);
}
albums.Add(currentPath);
if (node.Attributes["subalbum_count"] != null && node.Attributes["subalbum_count"].Value != "0") {
recurseAlbums(albums, currentPath, node.ChildNodes);
}
}
} }
} }
} }