/*
- * FAPG version 0.33
- *
+ * FAPG
+ */
+#define VERSION "0.34"
+/*
* FAPG means Fast Audio Playlist Generator.
* It is a tool to generate list of audio files (Wav, MP3, Ogg, etc)
* in various formats (M3U, PLS, HTML, etc).
#include <limits.h>
#include <unistd.h>
#include <ctype.h>
+#include <time.h>
#include <assert.h>
#include "genres.h"
#define MAX 1024*200 /* 200ko for ID3 with JPEG images in it */
int debug=0;
-int format=0; /* 0 = m3u ; 1 = pls ; 2 = html */
+int format=0; /* 0 = m3u ; 1 = pls ; 2 = html ; 3 = rss */
char *genrelist=NULL;
-char *prefix="";
+unsigned char *prefix="";
+unsigned char *base="";
+unsigned char *dir="";
+unsigned char *hostname="fritzserver.de";
+unsigned char *referal="http://www.explaining.text.org/select.php?title=";
int recursive=0;
int avoidhlinked=0;
int separator='/';
int skip=0;
int windows=0;
-char *eol="\n";
-char buffer[MAX];
+unsigned char *eol="\n";
+unsigned char buffer[MAX];
int counter=0;
-char artist[1024];
-char title[1024];
-char genrebuf[1024];
-char genre=0;
+unsigned char artist[1024];
+unsigned char title[1024];
+unsigned char genrebuf[1024];
+unsigned char genre=0;
int duration;
-char unix2dos[256]={0,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,70,35,36,37,38,39,40,41,82,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,84,59,36,61,65,71,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,36,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,36,125,126,127,199,252,233,226,228,224,229,231,234,235,232,239,238,236,196,197,201,230,198,244,246,242,251,249,255,214,220,248,163,216,215,131,225,237,243,250,241,209,170,186,191,174,172,189,188,161,171,187,166,166,166,166,166,193,194,192,169,166,166,43,43,162,165,43,43,45,45,43,45,43,227,195,43,43,45,45,166,45,43,164,240,208,202,203,200,105,205,206,207,43,43,166,220,166,204,175,211,223,212,210,245,213,181,254,222,218,219,217,253,221,175,180,173,177,61,190,182,167,247,184,176,168,183,185,179,178,166,160};
+unsigned char unix2dos[256]={0,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,70,35,36,37,38,39,40,41,82,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,84,59,36,61,65,71,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,36,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,36,125,126,127,199,252,233,226,228,224,229,231,234,235,232,239,238,236,196,197,201,230,198,244,246,242,251,249,255,214,220,248,163,216,215,131,225,237,243,250,241,209,170,186,191,174,172,189,188,161,171,187,166,166,166,166,166,193,194,192,169,166,166,43,43,162,165,43,43,45,45,43,45,43,227,195,43,43,45,45,166,45,43,164,240,208,202,203,200,105,205,206,207,43,43,166,220,166,204,175,211,223,212,210,245,213,181,254,222,218,219,217,253,221,175,180,173,177,61,190,182,167,247,184,176,168,183,185,179,178,166,160};
void usage()
{
- fprintf(stderr,"Usage >> fapg [-b|--backslash] [-d|--debug] [-f|--format=m3u|pls|html] [-g|--genre=#:#:...] [-n|--nohardlink] [-o|--output=/path/to/file.m3u] [-p|--prefix=/the/prefix] [-r|--recursive] [-w|--windows] [-x|--exclude=#:#:...] /path/to/mp3/dir1 [/path/to/mp3/dir2 ...]\n");
+ fprintf(stderr,"Usage >> fapg [-b|--backslash] [-d|--debug] [-f|--format=m3u|pls|html|rss] [-g|--genre=#:#:...] [-n|--nohardlink] [-o|--output=/path/to/file.m3u] [-p|--prefix=/the/prefix] [-r|--recursive] [-w|--windows] [-x|--exclude=#:#:...] /path/to/mp3/dir1 [/path/to/mp3/dir2 ...]\n");
exit(1);
}
+const char*reflink(const char*title)
+{
+ int i=0;
+ static char *buffer;
+ buffer=malloc(strlen(title)+strlen(referal)+2);
+ strcpy(buffer,referal);
+ strcat(buffer,title);
+ for(i=strlen(referal);i<strlen(buffer);i++)
+ {
+ switch(buffer[i])
+ {
+ case ' ': buffer[i]='+'; break;
+ default: ;
+ }
+ }
+ return(buffer);
+}
+
void parse_options(int argc,char **argv)
{
static char const short_options[]="bdf:g:lo:np:rwx:";
if (strcmp(optarg,"m3u")==0) format=0; else
if (strcmp(optarg,"pls")==0) format=1; else
if (strcmp(optarg,"html")==0) format=2; else
+ if (strcmp(optarg,"rss")==0) format=3; else
usage();
break;
case 'g':
case 'p':
prefix=malloc(strlen(optarg)+1);
strcpy(prefix,optarg);
+ base=malloc(strlen(prefix)+1);
+ strcpy(base,prefix);
+ dir=strchr(base,'/');
+ if( (dir!=NULL) && (dir[1]=='/') ) dir=strchr(dir+2,'/');
+ if (dir!=NULL) *dir++=0; else dir="";
+ /* if prefix is a weblink, base is the baselink, dir is the path */
break;
case 'r':
recursive=1;
usage();
}
}
+ /* hostname = getenv("HOSTNAME"); */
if (genrelist==NULL)
{
genrelist=calloc(257,sizeof(char));
}
}
-void parse_mp3(char *file)
+void parse_mp3(unsigned char *file)
{
int bitrates[2][3][15]=
{{{0, 32, 64, 96, 128, 160, 192, 224, 256, 288, 320, 352, 384, 416, 448},
{0, 8, 16, 24, 32, 40, 48, 56, 64, 80, 96, 112, 128, 144, 160},
{0, 8, 16, 24, 32, 40, 48, 56, 64, 80, 96, 112, 128, 144, 160}}};
FILE *fic;
- char *c;
+ unsigned char *c;
int lus;
genre=0;
while (c<buffer+lus-10)
{
- if ((unsigned char)*c==0xFF && ((unsigned char)*(c+1)&0xF0)==0xF0)
+ if (*c==0xFF && (*(c+1)&0xF0)==0xF0)
{
int version;
int lay;
fclose(fic);
}
-void parse_ogg(char *file)
+void parse_ogg(unsigned char *file)
{
FILE *fic;
- char *c;
+ unsigned char *c;
int lus;
int sample_rate;
int samples;
fclose(fic);
}
-void parse_mpc(char *file)
+void parse_mpc(unsigned char *file)
{
FILE *fic;
- char *c;
+ unsigned char *c;
int lus;
int sample_rates[4]={44100,48000,37800,32000};
int frame_count;
fclose(fic);
}
-#define FSN 8
-#define MAXINO (1<<22)
+#define FSN 32
+#define MAXINO (1<<24)
#define INOTYP unsigned long
#define regbit_qry(x,y) ( x[( (y) / sizeof(INOTYP) )] & 1<<( (y) % sizeof(INOTYP) ) )
#define regbit_set(x,y) ( x[( (y) / sizeof(INOTYP) )] |= 1<<( (y) % sizeof(INOTYP) ) )
return is_registered;
}
-void parse_directory(char *path)
+
+void parse_file(unsigned char *newpath)
{
- int i,n;
- struct dirent **namelist;
- char newpath[PATH_MAX];
- char *c;
- struct stat infos;
+ unsigned char *c;
+ unsigned char ext[5];
+ int j;
void print_faketitle()
{
while (c<newpath+strlen(newpath)-5)
{
c++;
- if (*c=='_') putchar(' '); else if (windows) putchar(unix2dos[(int)*c]); else putchar(*c);
+ if (*c=='_') putchar(' '); else if (windows) putchar(unix2dos[*c]); else putchar(*c);
}
}
/* remove double "//" when parsing a directory ending with a "/" */
if (last=='/' && *c=='/') continue;
last=*c;
- if (*c=='/') putchar(separator); else if (windows) putchar(unix2dos[(int)*c]); else putchar(*c);
+ if (*c=='/') putchar(separator); else if (windows) putchar(unix2dos[*c]); else putchar(*c);
}
}
- if (debug) fprintf(stderr,"Debug >> parsing directory : %s\n",path);
- if ((n=scandir(path,&namelist,0,alphasort))<0) { fprintf(stderr,"Warning >> can't open directory : %s\n",path); return; }
- for (i=0;i<n;i++)
- {
- sprintf(newpath,"%s/%s",path,namelist[i]->d_name);
- if (stat(newpath,&infos)!=0) { fprintf(stderr,"Warning >> can't stat file : %s\n",newpath); continue; }
- if (recursive && S_ISDIR(infos.st_mode) && strcmp(namelist[i]->d_name,".")!=0 && strcmp(namelist[i]->d_name,"..")!=0) parse_directory(newpath);
- /* hlink_check() might be applied more selective ... avoidhlink is only a simple prereq */
- if (S_ISREG(infos.st_mode) && ! ( avoidhlinked && hlink_check(&infos) ) )
- {
- char ext[5];
- int j;
- for (j=0;j<5;j++) ext[j]=tolower(namelist[i]->d_name[strlen(namelist[i]->d_name)-4+j]);
- artist[0]='\0';
- title[0]='\0';
- duration=-2;
- if (strcmp(".mp2",ext)==0) { duration=-1; parse_mp3(newpath); }
- if (strcmp(".mp3",ext)==0) { duration=-1; parse_mp3(newpath); }
- if (strcmp(".mpc",ext)==0) { duration=-1; parse_mpc(newpath); }
- if (strcmp(".mp+",ext)==0) { duration=-1; parse_mpc(newpath); }
- if (strcmp(".ogg",ext)==0) { duration=-1; parse_ogg(newpath); }
- if (strcmp(".wav",ext)==0) { duration=-1; /* parse_wav(newpath); */ }
+ for (j=0;j<5;j++) ext[j]=tolower(newpath[strlen(newpath)-4+j]);
+ artist[0]='\0';
+ title[0]='\0';
+ duration=-2;
+ if (strcmp(".mp2",ext)==0) { duration=-1; parse_mp3(newpath); }
+ if (strcmp(".mp3",ext)==0) { duration=-1; parse_mp3(newpath); }
+ if (strcmp(".mpc",ext)==0) { duration=-1; parse_mpc(newpath); }
+ if (strcmp(".mp+",ext)==0) { duration=-1; parse_mpc(newpath); }
+ if (strcmp(".ogg",ext)==0) { duration=-1; parse_ogg(newpath); }
+ if (strcmp(".wav",ext)==0) { duration=-1; /* parse_wav(newpath); */ }
- if (duration!=-2 && genrelist[(int)genre]) /* is it an audio file ? */
+ if (duration!=-2 && genrelist[genre]) /* is it an audio file ? */
{
counter++;
switch (format)
if (duration==-1) printf("?"); else printf("%d:%s%d",duration/60,duration%60<10?"0":"",duration%60);
printf("</td></tr>%s",eol);
break;
+ case 3:
+ if (duration!=-1)
+ { time_t zeit;
+ time(&zeit);
+ char timebuffer[256];
+ strftime(timebuffer,255, "%a %d %b %Y %T %Z", localtime(&zeit) ); /* ctime() had a trailing CR */
+ printf("\t<item>%s",eol);
+ if (strlen(artist)==0 && strlen(title)==0)
+ {
+ /* find a better solution for this */
+ printf("\t\t<author>%s</author>%s\t\t<title>%s</title>%s",newpath,eol, newpath,eol );
+ }
+ else printf("\t\t<author>%s</author>%s\t\t<title>%s</title>%s",artist,eol, title,eol );
+ /* you might want to add more into description --> look for a smart, not a fast program */
+ printf("\t\t<description><![CDATA[<h4>%s</h4><h5>%s</h5><a href=\"",title,artist);
+ print_path();
+ printf("\">Direct Link to Audiofile</a><br>]]></description>%s",eol);
+#if 1
+ printf("\t\t<link>%s</link>%s",reflink(title),eol);
+#endif
+ printf("\t\t<pubDate>%s</pubDate>%s",timebuffer,eol);
+ printf("\t\t<enclosure url=\"");
+ print_path();
+ printf("\" length=\"%d\" type=\"audio/mpeg\"/>%s\t\t<guid>",duration, eol);
+ print_path();
+ printf("</guid>%s",eol );
+ printf("\t\t<itunes:duration>%d:%d:%d</itunes:duration>%s",duration/3600,(duration/60)%60,duration%60,eol);
+ printf("\t\t<itunes:author>%s</itunes:author>%s", artist, eol);
+ printf("\t</item>%s",eol );
+ }
+ break;
}
}
+}
+
+void parse_directory(unsigned char *path)
+{
+ int i,n;
+ struct dirent **namelist;
+ unsigned char newpath[PATH_MAX];
+ struct stat infos;
+
+ if (debug)
+ fprintf(stderr,"Debug >> parsing directory : %s\n",path);
+ if (stat(path,&infos)!=0)
+ {
+ fprintf(stderr,"Warning >> can't stat entry : %s\n",path);
+ return;
+ }
+ /* check if it is a filename */
+ if (S_ISREG(infos.st_mode) || S_ISLNK(infos.st_mode))
+ {
+ parse_file(path);
+ return;
+ }
+ /* must be a directory - or something unusable like pipe, socket, etc */
+ if ((n=scandir(path,&namelist,0,alphasort))<0)
+ {
+ fprintf(stderr,"Warning >> can't open directory : %s\n",path);
+ return;
+ }
+ for (i=0;i<n;i++)
+ {
+ sprintf(newpath,"%s/%s",path,namelist[i]->d_name);
+
+ if (stat(newpath,&infos)!=0)
+ {
+ fprintf(stderr,"Warning >> can't stat entry : %s\n",newpath);
+ continue;
+ }
+ if (recursive && S_ISDIR(infos.st_mode)
+ && strcmp(namelist[i]->d_name,".")!=0
+ && strcmp(namelist[i]->d_name,"..")!=0) parse_directory(newpath);
+ /* hlink_check() might be applied more selective ... avoidhlink is only a simple prereq */
+ if (S_ISREG(infos.st_mode) && ! ( avoidhlinked && hlink_check(&infos) ) )
+ {
+ parse_file(newpath);
}
free(namelist[i]);
}
printf("[playlist]%s",eol);
break;
case 2:
- printf("<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">%s%s<html>%s%s<head>%s<title>Playlist generated by FAPG 0.33</title>%s<meta http-equiv=\"Content-Type\" content=\"text/html; charset=iso-8859-1\" />%s<style type=\"text/css\">%s<!--%s%sbody,td,tr {%s font-family: Verdana, Arial, Helvetica, sans-serif;%s font-size: 12px;%s color: #000000;%s}%s%sbody {%s background: #ffffff;%s}%s%sth {%s text-align: center;%s background: #ffcccc;%s padding-left: 15px;%s padding-right: 15px;%s border: 1px #dd8888 solid;%s}%s%std {%s text-align: center;%s background: #eeeeee;%s padding-left: 15px;%s padding-right: 15px;%s border: 1px #cccccc solid;%s}%s%sh1 {%s font-size: 25px;%s}%s%sp {%s font-size: 10px;%s}%s%sa {%s color: #993333;%s text-decoration: none;%s}%s%sa:hover {%s text-decoration: underline;%s}%s%s-->%s</style>%s</head>%s%s<body>%s%s<h1>Playlist</h1>%s%s<table>%s<tr><th>Entry</th><th>Artist</th><th>Title</th><th>Length</th></tr>%s",eol,eol,eol,eol,eol,eol,eol,eol,eol,eol,eol,eol,eol,eol,eol,eol,eol,eol,eol,eol,eol,eol,eol,eol,eol,eol,eol,eol,eol,eol,eol,eol,eol,eol,eol,eol,eol,eol,eol,eol,eol,eol,eol,eol,eol,eol,eol,eol,eol,eol,eol,eol,eol,eol,eol,eol,eol,eol,eol,eol,eol,eol,eol);
+ printf("<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">%s%s<html>%s%s<head>%s<title>Playlist generated by FAPG " VERSION "</title>%s<meta http-equiv=\"Content-Type\" content=\"text/html; charset=iso-8859-1\" />%s<style type=\"text/css\">%s<!--%s%sbody,td,tr {%s font-family: Verdana, Arial, Helvetica, sans-serif;%s font-size: 12px;%s color: #000000;%s}%s%sbody {%s background: #ffffff;%s}%s%sth {%s text-align: center;%s background: #ffcccc;%s padding-left: 15px;%s padding-right: 15px;%s border: 1px #dd8888 solid;%s}%s%std {%s text-align: center;%s background: #eeeeee;%s padding-left: 15px;%s padding-right: 15px;%s border: 1px #cccccc solid;%s}%s%sh1 {%s font-size: 25px;%s}%s%sp {%s font-size: 10px;%s}%s%sa {%s color: #993333;%s text-decoration: none;%s}%s%sa:hover {%s text-decoration: underline;%s}%s%s-->%s</style>%s</head>%s%s<body>%s%s<h1>Playlist</h1>%s%s<table>%s<tr><th>Entry</th><th>Artist</th><th>Title</th><th>Length</th></tr>%s",eol,eol,eol,eol,eol,eol,eol,eol,eol,eol,eol,eol,eol,eol,eol,eol,eol,eol,eol,eol,eol,eol,eol,eol,eol,eol,eol,eol,eol,eol,eol,eol,eol,eol,eol,eol,eol,eol,eol,eol,eol,eol,eol,eol,eol,eol,eol,eol,eol,eol,eol,eol,eol,eol,eol,eol,eol,eol,eol,eol,eol,eol,eol);
+ break;
+ case 3:
+ { time_t zeit;
+ time(&zeit);
+ char timebuffer[256];
+ strftime(timebuffer,255, "%a %d %b %Y %T %Z", localtime(&zeit) );
+ printf("<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>%s<!-- generator=\"FAPG " VERSION " -->%s<rss xmlns:itunes=\"http://www.itunes.com/DTDs/Podcast-1.0.dtd\" version=\"2.0\">%s <channel>%s\t<title>%s - %s</title>%s\t<description>Directory Tree %s</description>%s\t<link>%s</link>%s\t<itunes:image href=\"%s/rss/podcast.jpg\"/>%s\t<lastBuildDate>%s</lastBuildDate>%s\t<generator>FAPG " VERSION "</generator>%s\t<image>%s\t\t<url>%s/podcast.jpg</url>%s\t\t<title>Server Logo</title>%s\t\t<link>%s</link>%s\t\t<description>Feed provided by FAPG. Click to visit.</description>%s\t</image>%s\t<itunes:owner>%s\t\t<itunes:name>Admin %s</itunes:name>%s\t\t<itunes:email>podcast@%s</itunes:email>%s\t</itunes:owner>%s\t<category>Various</category>%s\t<itunes:subtitle>Directory Tree %s</itunes:subtitle>%s\t<itunes:author>%s</itunes:author>%s\t<copyright>unknown</copyright>%s\t<language>%s</language>%s\t<itunes:explicit>No</itunes:explicit>%s\t<ttl>1800</ttl>%s",eol,eol,eol,eol,hostname,dir,eol,prefix,eol,base,eol,prefix,eol,timebuffer,eol,eol,eol,base,eol,eol,base,eol,eol,eol,eol,base,eol,hostname,eol,eol,eol,dir,eol,getenv("LOGNAME"),eol,eol,getenv("LANG"),eol,eol,eol);
+ }
break;
}
for (;optind<argc;optind++)
{
+#if 0
+ /* activating skip means that all given path components are stripped */
+ /* in consequence a list of subdirs is always mapped to the same prefix */
+ /* a recommended workaround is to move to the desired subdir and avoid absolute directories in the list */
if (strlen(prefix)!=0) skip=strlen(argv[optind]);
+#endif
parse_directory(argv[optind]);
}
switch(format)
printf("NumberOfEntries=%d%sVersion=2%s",counter,eol,eol);
break;
case 2:
- printf("</table>%s%s<p>Playlist generated by <a href=\"http://royale.zerezo.com/fapg/\">FAPG 0.33</a></p>%s%s</body>%s%s</html>",eol,eol,eol,eol,eol,eol);
+ printf("</table>%s%s<p>Playlist generated by <a href=\"http://royale.zerezo.com/fapg/\">FAPG " VERSION "</a></p>%s%s</body>%s%s</html>",eol,eol,eol,eol,eol,eol);
+ break;
+ case 3:
+ printf(" </channel>%s</rss>%s",eol,eol);
break;
}
if (genrelist) free(genrelist);