comparison src/scrobbler/scrobbler.c @ 2302:f074702a0df3

revise scrobblerqueue.txt a bit
author Tomasz Mon <desowin@gmail.com>
date Sat, 12 Jan 2008 18:12:08 +0100
parents 689bdfacb10c
children dd78327f5747
comparison
equal deleted inserted replaced
2301:227de9a5467d 2302:f074702a0df3
61 61
62 typedef struct { 62 typedef struct {
63 char *artist, 63 char *artist,
64 *title, 64 *title,
65 *mb, 65 *mb,
66 *album, 66 *album;
67 utctime[16], 67 int utctime, track, len;
68 track[16],
69 len[16];
70 int numtries; 68 int numtries;
71 void *next; 69 void *next;
72 } item_t; 70 } item_t;
73 71
74 static item_t *q_queue = NULL; 72 static item_t *q_queue = NULL;
122 curl_free(item->mb); 120 curl_free(item->mb);
123 curl_free(item->album); 121 curl_free(item->album);
124 free(item); 122 free(item);
125 } 123 }
126 124
127 static item_t *q_put(Tuple *tuple, int len) 125 static item_t *q_put(Tuple *tuple, int t, int len)
128 { 126 {
129 item_t *item; 127 item_t *item;
130 const gchar *album; 128 const gchar *album;
131 129
132 item = malloc(sizeof(item_t)); 130 item = malloc(sizeof(item_t));
133 131
134 item->artist = fmt_escape(aud_tuple_get_string(tuple, FIELD_ARTIST, NULL)); 132 item->artist = fmt_escape(aud_tuple_get_string(tuple, FIELD_ARTIST, NULL));
135 item->title = fmt_escape(aud_tuple_get_string(tuple, FIELD_TITLE, NULL)); 133 item->title = fmt_escape(aud_tuple_get_string(tuple, FIELD_TITLE, NULL));
136 g_snprintf(item->utctime, sizeof(item->utctime), "%ld", time(NULL)); 134 item->utctime = t;
137 g_snprintf(item->len, sizeof(item->len), "%d", len); 135 item->len = len;
138 g_snprintf(item->track, sizeof(item->track), "%d", 136 item->track = aud_tuple_get_int(tuple, FIELD_TRACK_NUMBER, NULL);
139 aud_tuple_get_int(tuple, FIELD_TRACK_NUMBER, NULL));
140 137
141 #ifdef NOTYET 138 #ifdef NOTYET
142 if(tuple->mb == NULL) 139 if(tuple->mb == NULL)
143 #endif 140 #endif
144 item->mb = fmt_escape(""); 141 item->mb = fmt_escape("");
145 #ifdef NOTYET 142 #ifdef NOTYET
146 else 143 else
147 item->mb = fmt_escape((char*)tuple->mb); 144 item->mb = fmt_escape((char*)tuple->mb);
148 #endif 145 #endif
149 146
150 if((album = aud_tuple_get_string(tuple, FIELD_ALBUM, NULL)) != NULL) 147 album = aud_tuple_get_string(tuple, FIELD_ALBUM, NULL);
148 if (album)
149 item->album = fmt_escape((char*) album);
150 else
151 item->album = fmt_escape(""); 151 item->album = fmt_escape("");
152 else
153 item->album = fmt_escape((char*) album);
154 152
155 q_nitems++; 153 q_nitems++;
156 154
157 item->next = NULL; 155 item->next = NULL;
158 156
623 { 621 {
624 /* 622 /*
625 * don't submit queued tracks which don't yet meet audioscrobbler 623 * don't submit queued tracks which don't yet meet audioscrobbler
626 * requirements... 624 * requirements...
627 */ 625 */
628 if ((time(NULL) - atoi(item->utctime)) < (atoi(item->len) / 2) && 626 if ((time(NULL) - item->utctime) < (item->len / 2) &&
629 (time(NULL) - atoi(item->utctime)) < 240) 627 (time(NULL) - item->utctime) < 240)
630 continue; 628 continue;
631 629
632 if (!item) 630 if (!item)
633 return i; 631 return i;
634 632
635 g_string_append(submission,sc_itemtag('a',i,I_ARTIST(item))); 633 g_string_append(submission,sc_itemtag('a',i,I_ARTIST(item)));
636 g_string_append(submission,sc_itemtag('t',i,I_TITLE(item))); 634 g_string_append(submission,sc_itemtag('t',i,I_TITLE(item)));
637 g_string_append(submission,sc_itemtag('l',i,I_LEN(item))); 635 gchar *tmp = g_strdup_printf("%d",I_LEN(item));
638 g_string_append(submission,sc_itemtag('i',i,I_TIME(item))); 636 g_string_append(submission,sc_itemtag('l',i,tmp));
637 g_free(tmp);
638 tmp = g_strdup_printf("%d",I_TIME(item));
639 g_string_append(submission,sc_itemtag('i',i,tmp));
640 g_free(tmp);
639 g_string_append(submission,sc_itemtag('m',i,I_MB(item))); 641 g_string_append(submission,sc_itemtag('m',i,I_MB(item)));
640 g_string_append(submission,sc_itemtag('b',i,I_ALBUM(item))); 642 g_string_append(submission,sc_itemtag('b',i,I_ALBUM(item)));
641 g_string_append(submission,sc_itemtag('o',i,"P")); 643 g_string_append(submission,sc_itemtag('o',i,"P"));
642 g_string_append(submission,sc_itemtag('n',i,item->track)); 644 tmp = g_strdup_printf("%d",item->track);
645 g_string_append(submission,sc_itemtag('n',i,tmp));
646 g_free(tmp);
643 g_string_append(submission,sc_itemtag('r',i,"")); 647 g_string_append(submission,sc_itemtag('r',i,""));
644 648
645 pdebug(fmt_vastr("a[%d]=%s t[%d]=%s l[%d]=%s i[%d]=%s m[%d]=%s b[%d]=%s", 649 pdebug(fmt_vastr("a[%d]=%s t[%d]=%s l[%d]=%s i[%d]=%s m[%d]=%s b[%d]=%s",
646 i, I_ARTIST(item), 650 i, I_ARTIST(item),
647 i, I_TITLE(item), 651 i, I_TITLE(item),
831 } 835 }
832 } 836 }
833 837
834 static void read_cache(void) 838 static void read_cache(void)
835 { 839 {
836 FILE *fd; 840 FILE *fd;
837 char buf[PATH_MAX], *cache = NULL, *ptr1, *ptr2; 841 char buf[PATH_MAX];
838 int cachesize, written, i = 0; 842 int i=0;
839 item_t *item; 843 item_t *item;
840 gchar* config_datadir; 844 gchar* config_datadir;
841 845
842 cachesize = written = 0; 846 config_datadir = audacious_get_localdir();
843 847 g_snprintf(buf, sizeof(buf), "%s/scrobblerqueue.txt", config_datadir);
844 config_datadir = audacious_get_localdir(); 848 g_free(config_datadir);
845 g_snprintf(buf, sizeof(buf), "%s/scrobblerqueue.txt", config_datadir); 849
846 g_free(config_datadir); 850 if (!(fd = fopen(buf, "r")))
847 851 return;
848 if (!(fd = fopen(buf, "r"))) 852 pdebug(fmt_vastr("Opening %s", buf), DEBUG);
849 return; 853 fclose(fd);
850 pdebug(fmt_vastr("Opening %s", buf), DEBUG); 854
851 while(!feof(fd)) 855 gchar* cache;
852 { 856 gchar** values;
853 cachesize += CACHE_SIZE; 857 gchar** entry;
854 cache = realloc(cache, cachesize + 1); 858 g_file_get_contents(buf, &cache, NULL, NULL);
855 written += fread(cache + written, 1, CACHE_SIZE, fd); 859 values = g_strsplit(cache, "\n", 0);
856 cache[written] = '\0'; 860
857 } 861 int x;
858 fclose(fd); 862 for (x=0; values[x] && strlen(values[x]); x++) {
859 ptr1 = cache; 863 entry = g_strsplit(values[x], "\t", 0);
860 while(ptr1 < cache + written - 1) 864 if (entry[0] && entry[1] && entry[2] && entry[3] && entry[4] && entry[6]) {
861 { 865 char *artist, *title, *album;
862 char *artist, *title, *len, *time, *album, *mb; 866 int t, len, track;
863 867
864 pdebug("Pushed:", DEBUG); 868 artist = g_strdup(entry[0]);
865 ptr2 = strchr(ptr1, ' '); 869 album = g_strdup(entry[1]);
866 artist = calloc(1, ptr2 - ptr1 + 1); 870 title = g_strdup(entry[2]);
867 strncpy(artist, ptr1, ptr2 - ptr1); 871 track = atoi(entry[3]);
868 ptr1 = ptr2 + 1; 872 len = atoi(entry[4]);
869 ptr2 = strchr(ptr1, ' '); 873 /* entry[5] should always be "P"... */
870 title = calloc(1, ptr2 - ptr1 + 1); 874 t = atoi(entry[6]);
871 strncpy(title, ptr1, ptr2 - ptr1); 875
872 ptr1 = ptr2 + 1; 876 {
873 ptr2 = strchr(ptr1, ' '); 877 Tuple *tuple = aud_tuple_new();
874 len = calloc(1, ptr2 - ptr1 + 1); 878 gchar* string_value;
875 strncpy(len, ptr1, ptr2 - ptr1); 879 string_value = xmms_urldecode_plain(artist);
876 ptr1 = ptr2 + 1; 880 aud_tuple_associate_string(tuple, FIELD_ARTIST, NULL, string_value);
877 ptr2 = strchr(ptr1, ' '); 881 g_free(string_value);
878 time = calloc(1, ptr2 - ptr1 + 1); 882 string_value = xmms_urldecode_plain(title);
879 strncpy(time, ptr1, ptr2 - ptr1); 883 aud_tuple_associate_string(tuple, FIELD_TITLE, NULL, string_value);
880 ptr1 = ptr2 + 1; 884 g_free(string_value);
881 ptr2 = strchr(ptr1, ' '); 885 string_value = xmms_urldecode_plain(album);
882 album = calloc(1, ptr2 - ptr1 + 1); 886 aud_tuple_associate_string(tuple, FIELD_ALBUM, NULL, string_value);
883 strncpy(album, ptr1, ptr2 - ptr1); 887 g_free(string_value);
884 ptr1 = ptr2 + 1; 888 aud_tuple_associate_int(tuple, FIELD_TRACK_NUMBER, NULL, track);
885 ptr2 = strchr(ptr1, '\n'); 889 item = q_put(tuple, t, len);
886 if(ptr2 != NULL) 890
887 *ptr2 = '\0'; 891 aud_tuple_free(tuple);
888 mb = calloc(1, strlen(ptr1) + 1); 892 }
889 strncpy(mb, ptr1, strlen(ptr1)); 893
890 if(ptr2 != NULL) 894 pdebug(fmt_vastr("a[%d]=%s t[%d]=%s l[%d]=%d i[%d]=%d m[%d]=%s b[%d]=%s",
891 *ptr2 = '\n'; 895 i, I_ARTIST(item),
892 /* Why is our save printing out CR/LF? */ 896 i, I_TITLE(item),
893 ptr1 = ptr2 + 1; 897 i, I_LEN(item),
894 898 i, I_TIME(item),
895 { 899 i, I_MB(item),
896 Tuple *tuple = aud_tuple_new(); 900 i, I_ALBUM(item)), DEBUG);
897 gchar* string_value; 901 free(artist);
898 902 free(title);
899 string_value = xmms_urldecode_plain(artist); 903 free(album);
900 aud_tuple_associate_string(tuple, FIELD_ARTIST, NULL, string_value); 904 g_free(entry);
901 g_free(string_value); 905 i++;
902 906 }
903 string_value = xmms_urldecode_plain(title); 907 }
904 aud_tuple_associate_string(tuple, FIELD_TITLE, NULL, string_value); 908 g_free(values);
905 g_free(string_value); 909 g_free(cache);
906 910 pdebug("Done loading cache.", DEBUG);
907 string_value = xmms_urldecode_plain(album);
908 aud_tuple_associate_string(tuple, FIELD_ALBUM, NULL, string_value);
909 g_free(string_value);
910
911 item = q_put(tuple, atoi(len));
912
913 aud_tuple_free(tuple);
914 }
915
916 pdebug(fmt_vastr("a[%d]=%s t[%d]=%s l[%d]=%s i[%d]=%s m[%d]=%s b[%d]=%s",
917 i, I_ARTIST(item),
918 i, I_TITLE(item),
919 i, I_LEN(item),
920 i, I_TIME(item),
921 i, I_MB(item),
922 i, I_ALBUM(item)), DEBUG);
923 free(artist);
924 free(title);
925 free(len);
926 free(time);
927 free(album);
928 free(mb);
929
930 i++;
931 }
932 pdebug("Done loading cache.", DEBUG);
933 free(cache);
934 } 911 }
935 912
936 static void dump_queue(void) 913 static void dump_queue(void)
937 { 914 {
938 FILE *fd; 915 FILE *fd;
939 item_t *item; 916 item_t *item;
940 char *home, buf[PATH_MAX]; 917 char *home, buf[PATH_MAX];
941 gchar* config_datadir; 918 gchar* config_datadir;
942 919
943 /*pdebug("Entering dump_queue();", DEBUG);*/ 920 /*pdebug("Entering dump_queue();", DEBUG);*/
944 921
945 if (!(home = getenv("HOME"))) 922 if (!(home = getenv("HOME")))
946 { 923 {
947 pdebug("No HOME directory found. Cannot dump queue.", DEBUG); 924 pdebug("No HOME directory found. Cannot dump queue.", DEBUG);
948 return; 925 return;
949 } 926 }
950 927
951 config_datadir = audacious_get_localdir(); 928 config_datadir = audacious_get_localdir();
952 g_snprintf(buf, sizeof(buf), "%s/scrobblerqueue.txt", config_datadir); 929 g_snprintf(buf, sizeof(buf), "%s/scrobblerqueue.txt", config_datadir);
953 g_free(config_datadir); 930 g_free(config_datadir);
954 931
955 if (!(fd = fopen(buf, "w"))) 932 if (!(fd = fopen(buf, "w")))
956 { 933 {
957 pdebug(fmt_vastr("Failure opening %s", buf), DEBUG); 934 pdebug(fmt_vastr("Failure opening %s", buf), DEBUG);
958 return; 935 return;
959 } 936 }
960 937
961 pdebug(fmt_vastr("Opening %s", buf), DEBUG); 938 pdebug(fmt_vastr("Opening %s", buf), DEBUG);
962 939
963 q_peekall(1); 940 q_peekall(1);
964 941
965 while ((item = q_peekall(0))) { 942 while ((item = q_peekall(0))) {
966 fprintf(fd, "%s %s %s %s %s %s\n", 943 fprintf(fd, "%s\t%s\t%s\t%d\t%d\t%s\t%d\n",
967 I_ARTIST(item), 944 I_ARTIST(item),
968 I_TITLE(item), 945 I_ALBUM(item),
969 I_LEN(item), 946 I_TITLE(item),
970 I_TIME(item), 947 item->track,
971 I_ALBUM(item), 948 I_LEN(item),
972 I_MB(item)); 949 "P",
973 } 950 I_TIME(item));
974 951 }
975 fclose(fd); 952
953 fclose(fd);
976 } 954 }
977 955
978 /* This was made public */ 956 /* This was made public */
979 957
980 void sc_cleaner(void) 958 void sc_cleaner(void)
1048 void sc_addentry(GMutex *mutex, Tuple *tuple, int len) 1026 void sc_addentry(GMutex *mutex, Tuple *tuple, int len)
1049 { 1027 {
1050 g_mutex_lock(mutex); 1028 g_mutex_lock(mutex);
1051 1029
1052 sc_submit_np(tuple); 1030 sc_submit_np(tuple);
1053 q_put(tuple, len); 1031 q_put(tuple, time(NULL), len);
1054 1032
1055 /* 1033 /*
1056 * This will help make sure the queue will be saved on a nasty 1034 * This will help make sure the queue will be saved on a nasty
1057 * segfault... 1035 * segfault...
1058 */ 1036 */