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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
|
#include <string>
using namespace std;
#include <stdlib.h>
#include <stdio.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <mysql/mysql.h>
#include <taglib/tag.h>
#include <taglib/fileref.h>
#include "mg_tools.h"
MYSQL *db;
char *host, *user, *pass, *dbname;
int init_database()
{
db = mysql_init(NULL);
if( db == NULL )
{
return -1;
}
if( mysql_real_connect( db, host, user, pass,
dbname, 0, NULL, 0 ) == NULL )
{
return -2;
}
return 0;
}
time_t get_fs_modification_time( string filename )
{
struct stat *buf = (struct stat*) malloc( sizeof( struct stat ) );
// yes: obtain modification date for file and db entry
int statres = stat( filename.c_str(), buf );
time_t mod = buf->st_mtime;
free( buf );
return mod;
}
time_t get_db_modification_time( long uid )
{
time_t mt;
MYSQL_RES *result = mgSqlReadQuery( db, "SELECT modification_time FROM tracks WHERE id=\"%d\"", uid );
MYSQL_ROW row = mysql_fetch_row( result );
string mod_time = row[0];
mt = (time_t) atol( mod_time.c_str() );
return mt;
}
long find_file_in_database( string filename )
{
MYSQL_RES *result = mgSqlReadQuery( db, "SELECT id FROM tracks WHERE mp3file=\"%s\"", filename.c_str() );
MYSQL_ROW row = mysql_fetch_row( result );
// obtain ID and return
return atol( row[0] );
}
// read tags from the mp3 file and store them into the corresponding database entry
void update_db( long uid, string filename )
{
// char title[1024], album[1024], year[5], artist[1024], cddbid[20], trackno[5];
TagLib::String title, album, artist, genre, cddbid;
uint trackno, year;
// ID3_Tag filetag( filename.c_str() );
TagLib::FileRef f( filename.c_str() );
if( !f.isNull() && f.tag() )
{
// cout << "Evaluating " << filename << endl;
TagLib::Tag *tag = f.tag();
// obtain tag information
title = tag->title();
album = tag->album();
year = tag->year();
artist = tag->artist();
trackno = tag->track();
genre = tag->genre();
// TODO: CD identifier (?), playcounter, popularimeter?, volume adjustment
// finally update the database
// obtain associated album or create
if( album == "" )
{ // no album found, associate with default album
cddbid = "0000unknown0000";
}
else
{ // album tag found, associate or create
MYSQL_RES *result = mgSqlReadQuery( db, "SELECT cddbid FROM album WHERE title=\"%s\" AND artist=\"%s\"",
album.toCString(), artist.toCString() );
MYSQL_ROW row = mysql_fetch_row( result );
// num rows == 0 ?
int nrows = mysql_num_rows(result);
if( nrows == 0 )
{
// create new album entry
long id = random();
char *buf;
asprintf( &buf, "%d-%s", id, album.toCString() );
cddbid = TagLib::String( buf ).substr( 0, 20 );
free( buf );
mgSqlWriteQuery( db, "INSERT INTO album (artist,title,cddbid) VALUES (\"%s\", \"%s\", \"%s\")", artist.toCString(), album.toCString(), cddbid.toCString() );
}
else
{ // use first album found as source id for the track
cddbid = row[0];
}
}
// update tracks table
if( uid > 0 )
{ // the entry is known to exist already, hence update it
mgSqlWriteQuery( db, "UPDATE tracks SET artist=\"%s\", title=\"%s\", year=\"%s\", sourceid=\"%s\", mp3file=\"%s\""
"WHERE id=%d", artist.toCString(), title.toCString(), year, cddbid.toCString(), filename.c_str(), uid );
}
else
{ // the entry does not exist, create it
// int t = title.find( "'" );
// int a = artist.find( "'" );
mgSqlWriteQuery( db,
"INSERT INTO tracks (artist,title,year,sourceid,tracknb,mp3file)"
" VALUES (\"%s\", \"%s\", %d, \"%s\", %d, \"%s\")",
artist.toCString(), title.toCString(), year, cddbid.toCString(), trackno, filename.c_str() );
/*
cout << "-- TAG --" << endl;
cout << "title - \"" << tag->title() << "\"" << endl;
cout << "artist - \"" << tag->artist() << "\"" << endl;
cout << "album - \"" << tag->album() << "\"" << endl;
cout << "year - \"" << tag->year() << "\"" << endl;
cout << "comment - \"" << tag->comment() << "\"" << endl;
cout << "track - \"" << tag->track() << "\"" << endl;
cout << "genre - \"" << tag->genre() << "\"" << endl;
*/
}
}
}
void update_tags( long uid, string filename )
{
}
void evaluate_file( string filename )
{
if( 0 == init_database() )
{
// is filename stored in database?
long uid = find_file_in_database( filename );
if( uid >= 0 )
{
// currently only update database, do not consider writing changes from the db back
/*
// determine modification times in database and on filesystem
time_t db_time = get_db_modification_time( uid );
time_t fs_time = get_fs_modification_time( filename );
if( db_time > fs_time )
{
// db is newer: update id3 tags from db
update_tags( uid, filename );
}
else
{
// file is newer: update db from id3 tags
update_db( uid, filename );
}
*/
update_db( uid, filename );
}
else
{
// not in db yet: import file
update_db( -1, filename );
}
}
}
int main( int argc, char *argv[] )
{
host = "134.130.124.222";
user = "root";
dbname = "giantdisc";
pass = NULL;
/*
host = "localhost";
user = "vdr";
dbname = "GiantDisc";
pass = NULL;
*/
struct timeval tv;
struct timezone tz;
gettimeofday( &tv, &tz );
srandom( tv.tv_usec );
int res = init_database();
if( !res )
{
update_db( 0, string( argv[1] ) );
}
else
{
printf( "Database initialization failed. Exiting.\n" );
}
return res;
}
|