Debian Codegraph @

dnd2share/src/applet-backend-imgur.c

raw
1 /**
2 * This file is a part of the Cairo-Dock project
3 *
4 * Copyright : (C) see the 'copyright' file.
5 * E-mail : see the 'copyright' file.
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 3
10 * of the License, or (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 *
19 * Inspired by Imgur Bash Upload Script (http://imgur.com/tools/imgurbash.sh)
20 */
21
22 #define _BSD_SOURCE
23 #include <stdlib.h>
24 #include <math.h>
25 #include <unistd.h>
26 #include <glib/gstdio.h>
27
28 #include "applet-struct.h"
29 #include "applet-dnd2share.h"
30 #include "applet-backend-imgur.h"
31
32 #define NB_URLS 6
33 static const gchar *s_UrlLabels[NB_URLS] = {N_("Direct Link"), N_("Display Image"), N_("Large"), N_("Small"), "BBCode", "HTML"};
34
35 /*
36 <?xml version="1.0" encoding="utf-8"?>
37 <rsp stat="ok"><image_hash>Elqb97k</image_hash><delete_hash>qkIeCD33HUtBA3h</delete_hash><original_image>http://i.imgur.com/Elqb97k.png</original_image><large_thumbnail>http://i.imgur.com/Elqb97kl.jpg</large_thumbnail><small_thumbnail>http://i.imgur.com/Elqb97ks.jpg</small_thumbnail><imgur_page>http://imgur.com/Elqb97k</imgur_page><delete_page>http://imgur.com/delete/qkIeCD33HUtBA3h</delete_page></rsp>
38 */
39
40 static void upload (const gchar *cFilePath, gchar *cLocalDir, gboolean bAnonymous, gint iLimitRate, gchar **cResultUrls, GError **pError)
41 {
42 gchar *cCommand = g_strdup_printf ("curl -L --connect-timeout 5 --retry 2 --limit-rate %dk http://imgur.com/api/upload.xml -F key=b3625162d3418ac51a9ee805b1840452 -H \"Expect: \" -F image=@\"%s\"", iLimitRate, cFilePath);
43 cd_debug ("%s", cCommand);
44 gchar *cResult = cairo_dock_launch_command_sync (cCommand);
45 g_free (cCommand);
46
47 if (! cResult)
48 {
49 DND2SHARE_SET_GENERIC_ERROR_WEBSITE ("Imgur");
50 return;
51 }
52
53 gchar *cHashStart = strstr (cResult, "<image_hash>");
54 if (cHashStart)
55 {
56 cHashStart += 12;
57 gchar *cHashEnd = strstr (cHashStart, "</image_hash>");
58 if (cHashEnd)
59 *cHashEnd = '\0';
60 }
61 gchar *cHash = g_strdup (cHashStart);
62 g_free (cResult);
63
64 const gchar *cExt = strrchr (cFilePath, '.');
65 if (cExt == NULL)
66 cExt = "";
67 cResultUrls[0] = g_strdup_printf ("http://i.imgur.com/%s%s", cHash, cExt);
68 cResultUrls[1] = g_strdup_printf ("http://imgur.com/%s", cHash);
69 cResultUrls[2] = g_strdup_printf ("http://i.imgur.com/%sl.jpg", cHash);
70 cResultUrls[3] = g_strdup_printf ("http://i.imgur.com/%ss.jpg", cHash);
71 cResultUrls[4] = g_strdup_printf ("[URL=http://imgur.com/%s][IMG]http://i.imgur.com/%s%s[/IMG][/URL]", cHash, cHash, cExt);
72 cResultUrls[5] = g_strdup_printf ("<a href='http://imgur.com/%s'><img src='http://i.imgur.com/%s%s' title='Hosted by imgur.com and sent with Cairo-Dock' /></a>", cHash, cHash, cExt);
73 }
74
75
76 void cd_dnd2share_register_imgur_backend (void)
77 {
78 cd_dnd2share_register_new_backend (CD_TYPE_IMAGE,
79 "Imgur.com",
80 NB_URLS,
81 s_UrlLabels,
82 0,
83 upload);
84 }