/*
- * FAPG version 0.30
- *
+ * 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 MP3_BASE 1024
#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;
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;
void usage()
{
- fprintf(stderr,"Usage >> fapg [-b|--backslash] [-d|--debug] [-f|--format=m3u|pls|html] [-g|--genre=#:#:...] [-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:o:p:rwx:";
+ static char const short_options[]="bdf:g:lo:np:rwx:";
static struct option long_options[]=
{
{"backslash",no_argument,NULL,'b'},
{"debug",no_argument,NULL,'d'},
{"format",required_argument,NULL,'f'},
{"genre",required_argument,NULL,'g'},
+ {"nohardlink",no_argument,NULL,'n'},
{"output",required_argument,NULL,'o'},
{"prefix",required_argument,NULL,'p'},
{"recursive",no_argument,NULL,'r'},
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':
}
}
break;
+ case 'n':
+ avoidhlinked=1;
+ break;
case 'o':
close(1);
if (fopen(optarg,"w")==NULL) { fprintf(stderr,"Error >> unable to open output file : %s\n",optarg); exit(2); }
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));
fclose(fic);
}
-void parse_directory(unsigned char *path)
+#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) ) )
+
+int hlink_check(struct stat*info)
+{
+ /*
+ * for speed this subroutine should only be called
+ * - if the file has more than one hardlink
+ * - if the file is a resolved softlink
+ */
+ /* the persistent variables */
+ static INOTYP *list[FSN];
+ static dev_t name[FSN];
+ /* some temporary variables */
+ int fsn, is_registered=0;
+
+ /* assertions - in case parameters are lowered for less memory usage */
+ assert(fsn<FSN);
+ assert((info->st_ino)/sizeof(INOTYP)<MAXINO);
+
+ /* search which internal registration number is used for this filesystem */
+ for( fsn=0; (name[fsn]!=(info->st_dev)) && (name[fsn]!=0) ; fsn++);
+
+ /* if file system is not registered yet, do it and leave */
+ if( name[fsn] == 0 )
+ {
+ name[fsn] = (info->st_dev);
+ /* provide space for the bitmap that maps the inodes of this file system */
+ list[fsn] = (INOTYP*)calloc(MAXINO,sizeof(INOTYP));
+ /* no comparison is needed in empty lists ... return */
+ if(debug) fprintf(stderr, "Debug >> Linked >> Init List %04x @mem %04lx\n", (int)name[fsn], (long)&list[fsn] );
+ } else
+ {
+ /* this looks more complicated than it really is */
+ /* the idea is very simple:
+ * provide a bitmap that maps all inodes of a file system
+ * to mark all files that have already been visited.
+ * If it is already visited, do not add it to the playlist
+ */
+ /*
+ * The difficulty is as follows:
+ * struct inode_bitmap { char registered:1; } bitmap[1<<MAXINO];
+ * would be byte-aligned and would allocate at least eight times the needed space.
+ * Feel free to change the definitions that are involved here, if you know better.
+ */
+ if( regbit_qry( list[fsn], (info->st_ino) ) ) is_registered=1;
+ else regbit_set( list[fsn], (info->st_ino) );
+ /*
+ * the debug expression is more complicated then the working stuff
+ */
+ if(debug)
+ fprintf(stderr, "Debug >> Linked >> DEV %04x INO %06x => "
+ "list[%02x][%04x] = %04x & %04x --> %s registered\n",
+ (int)info->st_dev, (int)info->st_ino, fsn, (int)((info->st_ino)/sizeof(INOTYP)),
+ (int)list[fsn][(info->st_ino)/sizeof(INOTYP)],
+ 1<<((info->st_ino)%sizeof(INOTYP)), is_registered?"Already":"Not" );
+ }
+ return is_registered;
+}
+
+
+void parse_file(unsigned char *newpath)
{
- int i,n;
- struct dirent **namelist;
- unsigned char newpath[PATH_MAX];
unsigned char *c;
- struct stat infos;
+ unsigned char ext[5];
+ int j;
void print_faketitle()
{
void print_path()
{
+ char last=0;
printf(prefix);
- for (c=newpath+skip;*c!='\0';c++) if (*c=='/') putchar(separator); else if (windows) putchar(unix2dos[*c]); else putchar(*c);
+ c=newpath+skip;
+ /* remove leading "./" when parsing current directory */
+ if (*c=='.' && *(c+1)=='/') { c+=2; last='/'; }
+ for (;*c!='\0';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[*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);
- if (S_ISREG(infos.st_mode))
- {
- unsigned 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[genre]) /* is it an audio file ? */
{
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.30</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.30</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);