rb-audioscrobbler-entry.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
  2. *
  3. * Copyright (C) 2007 Christophe Fergeau <teuf@gnome.org>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * The Rhythmbox authors hereby grant permission for non-GPL compatible
  11. * GStreamer plugins to be used and distributed together with GStreamer
  12. * and Rhythmbox. This permission is above and beyond the permissions granted
  13. * by the GPL license by which Rhythmbox is covered. If you modify this code
  14. * you may extend this exception to your version of the code, but you are not
  15. * obligated to do so. If you do not wish to do so, delete this exception
  16. * statement from your version.
  17. *
  18. * This program is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU General Public License
  24. * along with this program; if not, write to the Free Software
  25. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  26. *
  27. */
  28. #define __EXTENSIONS__
  29. #include "config.h"
  30. #include <string.h>
  31. #include <time.h>
  32. #include <glib.h>
  33. #include <glib/gi18n.h>
  34. #include "rb-debug.h"
  35. #include "rhythmdb.h"
  36. #include <libsoup/soup.h>
  37. #include "rb-audioscrobbler-entry.h"
  38. #define SCROBBLER_DATE_FORMAT "%Y%%2D%m%%2D%d%%20%H%%3A%M%%3A%S"
  39. void
  40. rb_audioscrobbler_entry_init (AudioscrobblerEntry *entry)
  41. {
  42. entry->artist = g_strdup ("");
  43. entry->album = g_strdup ("");
  44. entry->title = g_strdup ("");
  45. entry->length = 0;
  46. entry->play_time = 0;
  47. entry->mbid = g_strdup ("");
  48. }
  49. void
  50. rb_audioscrobbler_entry_free (AudioscrobblerEntry *entry)
  51. {
  52. g_free (entry->artist);
  53. g_free (entry->album);
  54. g_free (entry->title);
  55. g_free (entry->mbid);
  56. g_free (entry);
  57. }
  58. void
  59. rb_audioscrobbler_encoded_entry_free (AudioscrobblerEncodedEntry *entry)
  60. {
  61. g_free (entry->artist);
  62. g_free (entry->album);
  63. g_free (entry->title);
  64. g_free (entry->mbid);
  65. g_free (entry->timestamp);
  66. g_free (entry);
  67. }
  68. AudioscrobblerEntry *
  69. rb_audioscrobbler_entry_create (RhythmDBEntry *rb_entry)
  70. {
  71. AudioscrobblerEntry *as_entry = g_new0 (AudioscrobblerEntry, 1);
  72. as_entry->title = rhythmdb_entry_dup_string (rb_entry,
  73. RHYTHMDB_PROP_TITLE);
  74. as_entry->artist = rhythmdb_entry_dup_string (rb_entry,
  75. RHYTHMDB_PROP_ARTIST);
  76. as_entry->album = rhythmdb_entry_dup_string (rb_entry,
  77. RHYTHMDB_PROP_ALBUM);
  78. if (strcmp (as_entry->album, _("Unknown")) == 0) {
  79. g_free (as_entry->album);
  80. as_entry->album = g_strdup ("");
  81. }
  82. as_entry->length = rhythmdb_entry_get_ulong (rb_entry,
  83. RHYTHMDB_PROP_DURATION);
  84. as_entry->mbid = rhythmdb_entry_dup_string (rb_entry,
  85. RHYTHMDB_PROP_MUSICBRAINZ_TRACKID);
  86. return as_entry;
  87. }
  88. AudioscrobblerEncodedEntry *
  89. rb_audioscrobbler_entry_encode (AudioscrobblerEntry *entry)
  90. {
  91. AudioscrobblerEncodedEntry *encoded;
  92. encoded = g_new0 (AudioscrobblerEncodedEntry, 1);
  93. encoded->artist = soup_uri_encode (entry->artist,
  94. EXTRA_URI_ENCODE_CHARS);
  95. encoded->title = soup_uri_encode (entry->title,
  96. EXTRA_URI_ENCODE_CHARS);
  97. encoded->album = soup_uri_encode (entry->album,
  98. EXTRA_URI_ENCODE_CHARS);
  99. encoded->mbid = soup_uri_encode (entry->mbid,
  100. EXTRA_URI_ENCODE_CHARS);
  101. encoded->timestamp = g_new0 (gchar, 30);
  102. strftime (encoded->timestamp, 30, SCROBBLER_DATE_FORMAT,
  103. gmtime (&entry->play_time));
  104. encoded->length = entry->length;
  105. return encoded;
  106. }
  107. AudioscrobblerEntry*
  108. rb_audioscrobbler_entry_load_from_string (const char *string)
  109. {
  110. AudioscrobblerEntry *entry;
  111. int i = 0;
  112. char **breaks;
  113. entry = g_new0 (AudioscrobblerEntry, 1);
  114. rb_audioscrobbler_entry_init (entry);
  115. breaks = g_strsplit (string, "&", 6);
  116. for (i = 0; breaks[i] != NULL; i++) {
  117. char **breaks2 = g_strsplit (breaks[i], "=", 2);
  118. if (breaks2[0] != NULL && breaks2[1] != NULL) {
  119. if (g_str_has_prefix (breaks2[0], "a")) {
  120. g_free (entry->artist);
  121. entry->artist = soup_uri_decode (breaks2[1]);
  122. }
  123. if (g_str_has_prefix (breaks2[0], "t")) {
  124. g_free (entry->title);
  125. entry->title = soup_uri_decode (breaks2[1]);
  126. }
  127. if (g_str_has_prefix (breaks2[0], "b")) {
  128. g_free (entry->album);
  129. entry->album = soup_uri_decode (breaks2[1]);
  130. }
  131. if (g_str_has_prefix (breaks2[0], "m")) {
  132. g_free (entry->mbid);
  133. entry->mbid = soup_uri_decode (breaks2[1]);
  134. }
  135. if (g_str_has_prefix (breaks2[0], "l")) {
  136. entry->length = atoi (breaks2[1]);
  137. }
  138. if (g_str_has_prefix (breaks2[0], "i")) {
  139. struct tm tm;
  140. strptime (breaks2[1], SCROBBLER_DATE_FORMAT,
  141. &tm);
  142. entry->play_time = mktime (&tm);
  143. }
  144. /* slight format extension: time_t */
  145. if (g_str_has_prefix (breaks2[0], "I")) {
  146. entry->play_time = strtol (breaks2[1], NULL, 10);
  147. }
  148. }
  149. g_strfreev (breaks2);
  150. }
  151. g_strfreev (breaks);
  152. if (strcmp (entry->artist, "") == 0 || strcmp (entry->title, "") == 0) {
  153. rb_audioscrobbler_entry_free (entry);
  154. entry = NULL;
  155. }
  156. return entry;
  157. }
  158. void
  159. rb_audioscrobbler_entry_save_to_string (GString *string, AudioscrobblerEntry *entry)
  160. {
  161. AudioscrobblerEncodedEntry *encoded;
  162. encoded = rb_audioscrobbler_entry_encode (entry);
  163. g_string_append_printf (string,
  164. "a=%s&t=%s&b=%s&m=%s&l=%d&I=%ld\n",
  165. encoded->artist,
  166. encoded->title,
  167. encoded->album,
  168. encoded->mbid,
  169. encoded->length,
  170. entry->play_time);
  171. rb_audioscrobbler_encoded_entry_free (encoded);
  172. }
  173. void
  174. rb_audioscrobbler_entry_debug (AudioscrobblerEntry *entry, int index)
  175. {
  176. char timestamp[30];
  177. rb_debug ("%-3d artist: %s", index, entry->artist);
  178. rb_debug (" album: %s", entry->album);
  179. rb_debug (" title: %s", entry->title);
  180. rb_debug (" length: %d", entry->length);
  181. rb_debug (" playtime: %ld", entry->play_time);
  182. strftime (timestamp, 30, SCROBBLER_DATE_FORMAT,
  183. gmtime (&entry->play_time));
  184. rb_debug (" timestamp: %s", timestamp);
  185. }