File: mpdscribble.c

package info (click to toggle)
mpdscribble 0.2.10-1
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 716 kB
  • ctags: 500
  • sloc: ansic: 3,904; sh: 3,603; makefile: 70
file content (202 lines) | stat: -rw-r--r-- 5,284 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
/*
    This file is part of mpdscribble,
    another audioscrobbler plugin for music player daemon.
    Copyright © 2005 Kuno Woudt <kuno@frob.nl>

    mpdscribble is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    mpdscribble is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with mpdscribble; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

*/

#include <signal.h>

#include <stdlib.h>
#include <stdio.h>

#include "file.h"
#include "misc.h"
#include "lmc.h"
#include "as.h"

#include "mbid.h"

#define MAX_SKIP_ERROR (file_config.sleep + 3) /* in seconds. */

const char *program_name;

void
cleanup (void)
{
  notice ("shutting down...");

  as_cleanup ();
  lmc_disconnect ();
  file_cleanup ();
}

void
signal_handler (int signum)
{
  exit (23);
}

void
sigpipe_handler (int signum)
{
  warning ("broken pipe, disconnected from mpd");
}

int
main (int argc, char** argv)
{
  lmc_song song;

  int last_id = -1;
  int last_el = 0;
  int last_mark = 0;
  int elapsed = 0;
  int submitted = 1;
  int was_paused = 0;
  int next_save = 0;
  char mbid[MBID_BUFFER_SIZE];
  FILE * log;

  /* apparantly required for regex.h, which
     is used in file.h */
  program_name = argv[0];
  set_logfile (stderr, 2);
  if (!file_read_config (argc, argv))
    fatal ("cannot read configuration file.\n");

  log = file_open_logfile ();
  set_logfile (log, file_config.verbose);

  lmc_connect (file_config.host, file_config.port);
  as_init (file_config.sleep);

  atexit (cleanup);

  if (signal (SIGINT, signal_handler) == SIG_IGN)
    signal (SIGINT, SIG_IGN);
  if (signal (SIGHUP, signal_handler) == SIG_IGN)
    signal (SIGHUP, SIG_IGN);
  if (signal (SIGTERM, signal_handler) == SIG_IGN)
    signal (SIGTERM, SIG_IGN);
  if (signal (SIGPIPE, sigpipe_handler) == SIG_IGN)
    signal (SIGPIPE, SIG_IGN);

  next_save = now () + file_config.cache_interval;
  mbid[0] = 0x00;

  while (1)
    {
      as_poll ();
      fflush (log);
      as_sleep ();
      elapsed = lmc_current (&song);

      if (now () > next_save)
        {
          as_save_cache ();
          next_save = now () + file_config.cache_interval;
        }


      if (elapsed == LMC_PAUSED)
        {
          was_paused = 1;
          continue;
        }
      else if (elapsed == LMC_NOTPLAYING)
        {
          last_id = -1;
          continue;
        }

      if (was_paused)
        {
          /* song is paused, reset the measured time to the amount
             the song has actually played. */
          was_paused = 0;
          last_el = elapsed;
          last_mark = now ();
        }

      /* new song. */
      if (song.id != last_id)
        {
          if (song.artist && song.title)
            notice ("new song detected (%s - %s)", song.artist, song.title);
          else
            notice ("new song detected with tags missing (%s)", song.file); 
          last_id = song.id;
          last_el = elapsed;
          last_mark = now ();

          submitted = 1;

          if (file_config.musicdir)
            {
              // yeah, I know i'm being silly, but I can't be arsed to 
              // concat the parts :P
              chdir (file_config.musicdir);
              if (getMBID (song.file, mbid))
                mbid[0] = 0x00;
              else
                notice ("mbid is %s.", mbid);
            }

          if (song.time < 30)
            notice ("however, song is too short, not submitting.");
          /* don't submit the song which is being played when we start,.. too
             many double submits when restarting the client during testing in
             the first half of a song ;) */
          else if (elapsed > MAX_SKIP_ERROR*2)
            notice ("skipping detected, not submitting.");
          else
            submitted = 0;
        }
      /* not a new song, so check for skipping. */
      else if (!submitted)
        {
          int time = now ();
          int calculated = last_el + (time-last_mark);

          if ((elapsed+MAX_SKIP_ERROR < calculated)
              || (elapsed-MAX_SKIP_ERROR > calculated))
            {
              notice ("skipping detected, not submitting.");
              submitted = 1;
            }

          last_el = elapsed;
          last_mark = time;
        }

      if (!submitted && ((elapsed > 240) || (elapsed > (song.time/2))))
        {
          /* FIXME:
             libmpdclient doesn't have any way to fetch the musicbrainz id. */
          int q = as_songchange (song.file, song.artist, song.title,
                                 song.album, mbid, song.time, NULL);
          if (q != -1)
            notice ("added (%s - %s) to submit queue at position %i.",
                    song.artist, song.title, q);

          submitted = 1;
        }
    }

  return 0;
}