Ejemplo n.º 1
0
static gboolean queue_track_to_scrobble (gpointer data) {
    AUDDBG("The playing track is going to be ENQUEUED!\n.");
    gchar *tab_remover;

    char *queuepath = g_strconcat(aud_get_path(AUD_PATH_USER_DIR),"/scrobbler.log", NULL);

    char *artist = tuple_get_str(playing_track, FIELD_ARTIST);
    char *title  = tuple_get_str(playing_track, FIELD_TITLE);
    char *album  = tuple_get_str(playing_track, FIELD_ALBUM);

    tab_remover = remove_tabs(artist);
    str_unref(artist);
    artist = tab_remover;

    tab_remover = remove_tabs(title);
    str_unref(title);
    title = tab_remover;

    tab_remover = remove_tabs(album);
    str_unref(album);
    album = tab_remover;
    tab_remover = NULL;

    int number = tuple_get_int(playing_track, FIELD_TRACK_NUMBER);
    int length = tuple_get_int(playing_track, FIELD_LENGTH) / 1000;

    //artist, title and timestamp are required for a successful scrobble
    if (  artist != NULL && strlen(artist) > 0
        && title != NULL && strlen(title)  > 0) {

        pthread_mutex_lock(&log_access_mutex);
        FILE *f = fopen(queuepath, "a");

        if (f == NULL) {
            perror("fopen");
        } else {
            //This isn't exactly the scrobbler.log format because the header
            //is missing, but we're sticking to it anyway...
            //See http://www.audioscrobbler.net/wiki/Portable_Player_Logging
            if (fprintf(f, "%s\t%s\t%s\t%i\t%i\t%s\t%"G_GINT64_FORMAT"\n",
                        artist, (album == NULL ? "" : album), title, number, length, "L", timestamp
                       ) < 0) {
                perror("fprintf");
            } else {
                pthread_mutex_lock(&communication_mutex);
                pthread_cond_signal(&communication_signal);
                pthread_mutex_unlock(&communication_mutex);
            }
            fclose(f);
        }
        pthread_mutex_unlock(&log_access_mutex);
    }
    g_free(queuepath);
    g_free(artist);
    g_free(title);
    g_free(album);

    cleanup_current_track();
    return FALSE;
}
Ejemplo n.º 2
0
Archivo: foo.c Proyecto: leolas95/c
int main(void)
{
        char line[MAXLN];
        int len;
        int i;
        int num_blanks = 0, num_tabs = 0;


        /* ARREGLAR: No imprime [BLANK LINE] */
        while ((len = getline(line, MAXLN)) >= 0) {
                if (len == 1 && line[0] == '\n') {
                        printf("[BLANK LINE]\n");
                        continue;
                }
                for (i = 0; i < len; ++i) {
                        if (line[i] == ' ') {
                                num_blanks++;
                        } else if (line[i] == '\t') {
                                num_tabs++;
                        }
                }
                while (num_blanks > 0) {
                        remove_blanks(line, len);
                        num_blanks--;
                }
                while (num_tabs > 0) {
                        remove_tabs(line, len);
                        num_tabs--;
                }
                printf("%s\n\n", line);
        }
        return 0;
}
Ejemplo n.º 3
0
/// read string, remove redundant spaces or tabulations, 
/// if string longer than LEN then 
/// print string as several short strings.
void main(){

	while(TRUE){
		sleep( 0.200 );

		clear(string, MAX_LEN);

		length = read_line(string);
		if(length == 0)
			break;

		length = remove_tabs(string, length);

		if(length < LEN)
			printf("%s", string);
		else
			split_and_print(string, LEN);

	}

}
static void send_now_playing() {

  gchar *error_code = NULL;
  gchar *error_detail = NULL;
  /*
   * now_playing_track can be set to something else while we this method is
   * running. Creating a local variable avoids to get data for different tracks,
   * while now_playing_track was updated concurrently.
   */
  Tuple *curr_track = now_playing_track;

  gchar *tab_remover;

  gchar *artist = tuple_get_str(curr_track, FIELD_ARTIST, NULL);
  gchar *album = tuple_get_str(curr_track, FIELD_ALBUM, NULL);
  gchar *title = tuple_get_str(curr_track, FIELD_TITLE, NULL);

  tab_remover = remove_tabs(artist);
  str_unref(artist);
  artist = tab_remover;

  tab_remover = remove_tabs(album);
  str_unref(album);
  album = tab_remover;

  tab_remover = remove_tabs(title);
  str_unref(title);
  title = tab_remover;

  tab_remover = NULL;

  gchar *number = g_strdup_printf("%i", tuple_get_int(curr_track, FIELD_TRACK_NUMBER, NULL));
  gchar *length = g_strdup_printf("%i", tuple_get_int(curr_track, FIELD_LENGTH, NULL) / 1000);
  tuple_unref(curr_track);


  if (artist != NULL && strlen(artist) > 0 &&
       title != NULL && strlen(title)  > 0) {

    gchar *playingmsg = create_message_to_lastfm("track.updateNowPlaying",
                                            7,
                                           "artist", artist,
                                           "album", (album == NULL ? "" : album),
                                           "track", title,
                                           "trackNumber", number,
                                           "duration", length,
                                           "api_key", SCROBBLER_API_KEY,
                                           "sk", session_key);
    g_free(artist);
    g_free(album);
    g_free(title);
    g_free(number);
    g_free(length);

    bool_t success = send_message_to_lastfm(playingmsg);
    g_free(playingmsg);
    if (success == FALSE) {
      AUDDBG("Network problems. Could not send \"now playing\" to last.fm\n");
      scrobbling_enabled = FALSE;
      return;
    }

    if (read_scrobble_result(&error_code, &error_detail) == TRUE) {
      //see scrobble_cached_queue()
      AUDDBG("NOW PLAYING OK.\n");
    } else {
      AUDDBG("NOW PLAYING NOT OK. Error code: %s. Error detail: %s.\n", error_code, error_detail);
      //From the API: Now Playing requests that fail should not be retried.

      if (g_strcmp0(error_code, "9") == 0) {
        //Bad Session. Reauth.
        //We don't really care about any other errors.
        scrobbling_enabled = FALSE;
        g_free(session_key);
        session_key = NULL;
        aud_set_string("scrobbler", "session_key", "");
      }

    }
    g_free(error_code);
    g_free(error_detail);
    //We don't care if the now playing was not accepted, no need to read the result from the server.

  }
}