LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Script to build web page of movies from directories (https://www.linuxquestions.org/questions/linux-newbie-8/script-to-build-web-page-of-movies-from-directories-4175441234/)

wgcooley 12-12-2012 09:32 PM

Script to build web page of movies from directories
 
Hello! I'm new to linux. I installed DD-WRT onto a router and have it serving movies over http. I'd like to build a web page of all the movie poster art so my kids can easily choose a movie from the home page.

All movies are in /mnt/Videos. Some movies are in the root directory, but there are sub-directories with movies (/mnt/Videos/Barney, /mnt/Videos/Sesame Street, etc). For each movie there is a jpg image of the DVD cover, named exactly the same as the movie (i.e., Toy Story.m4v and Toy Story.jpg)

How do I make a script that reads in all videos (including sub-directores), then builds an index.html of movie art jpg in two columns with as many rows as necessary (n x 2)? When the kids click on the picture, it should link to the movie file.

BTW, the media server works very well and uses hardly any power. I wrote up the install at:
http://www.dd-wrt.com/phpBB2/viewtop...=asc&start=315

cbtshare 12-13-2012 06:58 PM

this is a place where you get help ,if you have something that you have done we can assist but we rarely complete everything for you...

wgcooley 12-13-2012 09:15 PM

Wow. Thanks for the warm welcome on my first post here. I was expecting some guidance like "use this command," not go someplace else.

Here's what I was thinking...

#!/opt/bin/sh
rm index.html
<<don't need help with code to build html header and table>>
i=0
for file in /mnt/Videos/*jpg
do
i=i+1
<<code to assign row/column of movie poseter based on i>>
new variable = need help figuring our how change the ".jpg" in $file to ".m4v"
need help with writing to a file <a href="new variable"</a>
done

I also toyed around with using "find . -name *m4v > names.txt" then looping through the file to create the html code.

I'm asking for your help strategizing the best methods and commands to use, not to write the script for me.

montel 12-14-2012 12:36 PM

No need to get offended by what he said, he was just giving you feedback saying that you would most likely not get a good response unless you post what you have attempted/have so far. This isn't really a place to have people do things for you, its a community of people helping/learning.

That said, I am not entirely sure how you should tackle this, and not a linux guru, but my first thought was something like this:
##EDIT: I like my second one better.

This will list out the poster and movie as it comes across them. But I am pretty sure after writing this, there is some logical flaws with doing it this way.
Code:

#!/bin/bash
rdir="/tmp/grabtest/"*

for file in $rdir
do
#echo $file
        ndir="$file"

        for i in "$ndir/"*
        do
                #echo $i
                if [[ $i == *.jpg ]];then
                        poster=$i
                        echo "poster is $poster"
                fi
                if [[ $i == *.m4v ]];then
                        movie=$i
                        echo "movie is $movie"
                fi
        done
done

Then I was thinking about what you were doing with Find, and thought maybe something like this:

I left in the echos, i was just using them to test.
Code:

#!/bin/bash
rm /tmp/m4list

find /tmp/grabtest/ -name *.m4v > /tmp/m4list

#cat /tmp/m4list
file=/tmp/m4list

while read line;do
        echo $line is found
        p=${line##*/}
        name=${p%.m4v}
        #HTML to link to file could be here
        echo "<a href='$line'>$name</a>" >> /var/www/index.html
        echo $name
        t="${line%/*}/"
        poster="$t$name.jpg"

        if [[ -f $poster ]];then
                echo "poster exists"
                #do whatever HTML you need.
        fi

done < "$file"

These are pretty crude examples, but should give you at least an idea. I am at work right now, so I was trying to speed through it.

Hope it helps,

wgcooley 12-16-2012 07:26 PM

I ended up using find to create a list of all files on the drive. I think looked at the last four characters to see if the string was the movie (ending in m4v). The script works great. Thanks for the help.

!/bin/bash

rm sorted_names
rm movies.html
find | sort -n > sorted_names

FILENAME=sorted_names

while read LINE
do
ext=$(echo $LINE | tail -c -4)

if [[ "$ext" == "m4v" ]]
then
echo "<a href=\"$LINE\" >" >> movies.html
echo "<img src=\"${LINE%????}.jpg\" width=\"360\" height=\"510\" />" >> movies.html
fi

done < $FILENAME
rm sorted_names

grail 12-17-2012 09:31 AM

Ok ... so the second thing to cover is, please use [code][/code] tags around data and code as it will preserve formatting and make it easier to read.

You are on to some better ideas so let us see if we can get you over the line.
How about something like:
Code:

#!/bin/bash

movie_dir='/mnt/Videos'
html_file='movies.html'

rm "$html_file"

while read -r picture
do
    movie="${picture%.*}.m4v"

    cat >> $html_file <<-EOF
        <a href="$movie" >
        <img src="$picture" width="360" height="510" />
EOF
done< <(find "$movie_dir" -type f -name '*.jpg')

Not tested or complete but you get the idea :)

theNbomr 12-17-2012 11:47 AM

The solution posted by grail works iff all JPEGs in the directory tree are associated with a movie file. The more rigorous solution would find all movies, and from that, compose the respective JPEG filename. Actually, I see that is the algorithm used by montel, but merely using a temporary file to store the list of found files. It isn't stated in the original question, but montel's algorithm could also flag any movies that do not have an associated JPEG, and perhaps put a dummy image into the HTML output.
Always a good idea to assume the possibility of errors.

--- rod.

schneidz 12-17-2012 11:52 AM

this seems related (although not html).
http://www.linuxquestions.org/questi...4/#post3508846

have you considered using xbmc ?

grail 12-17-2012 12:04 PM

hmmm ... so you have marked the problem as solved but given no ideas on what your solution is?

Remember from the first 2 posts, you were advised that we are here to help each other and when someone new searches the site they get the benefit of
learning from you :)

David the H. 12-19-2012 09:33 AM

The LinuxCommand tutorial exercise is all about automating the building of an html page. It should help you out.

propofol 12-19-2012 05:13 PM

One thing to consider is using PHP on the server to create the page dynamically each time it is requested. PHP would be a better tool than Bash to accomplish what you have in mind. Have a look at: http://www.howtogeek.com/howto/progr...n-a-directory/

Regards,
Stefan


All times are GMT -5. The time now is 03:34 AM.