version 0.34 (by Andreas Neuper)
[FAPG] / fapg.c
diff --git a/fapg.c b/fapg.c
index 8a6667d..e627ea5 100644 (file)
--- a/fapg.c
+++ b/fapg.c
@@ -1,6 +1,8 @@
 /*
- * FAPG version 0.31
- *
+ *              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).
@@ -36,6 +38,7 @@
 #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;
 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='/';
@@ -67,10 +74,28 @@ unsigned char unix2dos[256]={0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,2
 
 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:";
@@ -103,6 +128,7 @@ void parse_options(int argc,char **argv)
         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':
@@ -129,6 +155,12 @@ void parse_options(int argc,char **argv)
       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;
@@ -160,6 +192,7 @@ void parse_options(int argc,char **argv)
         usage();
     }
   }
+  /* hostname = getenv("HOSTNAME"); */
   if (genrelist==NULL)
   {
     genrelist=calloc(257,sizeof(char));
@@ -479,8 +512,8 @@ void parse_mpc(unsigned char *file)
   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) ) )
@@ -542,13 +575,12 @@ int hlink_check(struct stat*info)
   return is_registered;
 }
 
-void parse_directory(unsigned char *path)
+
+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()
   {
@@ -563,32 +595,30 @@ void parse_directory(unsigned char *path)
   
   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);
-    /* hlink_check() might be applied more selective ... avoidhlink is only a simple prereq */
-    if (S_ISREG(infos.st_mode) && ! ( avoidhlinked && hlink_check(&infos) ) )
-    {
-      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 ? */
       {
@@ -621,8 +651,83 @@ void parse_directory(unsigned char *path)
             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]);
   }
@@ -642,12 +747,25 @@ int main(int argc,char **argv)
       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.31</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)
@@ -656,7 +774,10 @@ int main(int argc,char **argv)
       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.31</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);