Tuesday 21 July 2015

A PowerShell script to copy files listed in a M3U playlist to a destination folder whilst preserving directory structure

Some background to this script:


  • I have a large music collection, but only limited space on my phone, so I only want to copy a subset of the files
  • I use Logitech Media Server (a.k.a. Squeezebox Server), along with Erland's Trackstat, Playlist Generator and SQL Query plugins, which automatically generates me M3U format playlist files of things like the most recently added tracks, most played, highest rated etc.
  • I wanted to use some of these generated playlists to specify which files to copy to a destination folder, and so I wrote the script below with a lot of help from sources on Google!
  • Challenges included preserving the directory structure when copying each file, and handling directory names and file names with characters such as square brackets, which apparently have special meaning in PowerShell.





$sourcePlayList="X:\playlists\My Music - Last 500 Added.m3u"
$outputPath="M:\S6\music\"
$regExKeep="volume1\/music\/.*"
$regExExclude="#EXTURL:.*"
$rawPlayListValues = Get-Content $sourcePlayList



foreach ($item in $rawPlayListValues) {
  #First extract the path
  if($item -match $regExExclude){
  }
  elseif($item -match $regExKeep){
   $item = $matches[0] -replace "volume1\/music\/",""
   
   $sourceitem = "X:\$item"
   $sourceitem = Get-Item -Literalpath $sourceitem
   
   $dir = $sourceitem.DirectoryName.Replace("X:\",$outputPath)
   
   $target = $sourceitem.FullName.Replace("X:\",$outputPath)

            if (!(test-path -LiteralPath $dir)) 
   {
    mkdir $dir
   }
   
   if (!(test-path -LiteralPath $target))
   {
    Copy-Item -Literalpath $sourceitem.FullName -Destination $target -Recurse -Force
   }

   Write-Host $item
  } 
}

Sunday 25 January 2015

Rendering images showing the weather forecast from an RSS Feed

I created this bash shell script to generate images showing the weather forecast for my region for the next 2 days, using the BBC Weather RSS feed as a source.

The script is scheduled hourly using crond on one of my Raspberry Pi devices, and outputs 2 images to the local Apache instance web root directory. The images are composed by first parsing the XML from the RSS feed, extracting, transforming and formatting the required values, then generating the resultant images using the ImageMagick convert tool.

I then point my SqueezeBox Radio Image Viewer screensaver at an .lst file containing the URLs of the 2 images, and now each day I wake up to a handy 2 day forecast to help me plan my day!

This script is far from optimised, but it does the job - feel free to try it yourself or suggest modifications and additions to improve it.


NOTES

  • The BBC Weather RSS feed only covers the UK, and you will also need to find out your 'area code' and change it in the supplied URL to get the forecast for your specific region.
  • The script heavily relies on the layout and format of the feed always being the same - if the BBC decide to change it I'll have to adapt the script accordingly.
  • The images are created at a fixed size (320 x 240) to allow display on the SqueezeBox radio screen - you can create them at any size required though.


SAMPLE OUTPUT





THE SCRIPT

 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
#!/bin/bash

#CREATE IMAGE FOR TODAY
today_text=`curl -s --location --header "Accept: application/rdf+xml" \
http://open.live.bbc.co.uk/weather/feeds/en/2643123/3dayforecast.rss | xmllint --format --xpath 'string((//channel/item/title)[1])' -`

#text=`echo $text | sed -r 's/, /\\\n/g'`

weekday_today=`echo $today_text | sed -r 's/day:.*/day/g' | colrm 4 | \
sed -e 'y/ABCDEFGHIJKLMNOPQRSTUVWXYZ/abcdefghijklmnopqrstuvwxyz/'`

today_text=`echo $today_text | sed -r 's/Maximum Temperature:/\\\nMax Temp;/g'`
today_text=`echo $today_text | sed -r 's/Minimum Temperature:/\\\nMin Temp;/g'`
today_text=`echo $today_text | sed -r 's/.*://g'`
today_text=`echo $today_text | sed -r 's/;/:/g'`
today_text=`echo $today_text | sed -r 's/,/\\\n/g'`

convert -background black -gravity center -fill white -font Helvetica -size 320x240 -pointsize 22 -interline-spacing 10 \
label:"\n$today_text" /var/www/weather_forecast_today.jpg
convert /var/www/weather_forecast_today.jpg -font Helvetica -pointsize 80 -fill rgba\(173,216,230,0.8\) -gravity northWest \
-annotate 0 "$weekday_today" /var/www/weather_forecast_today.jpg



#CREATE FORECAST IMAGE FOR TOMORROW
tomorrow_text=`curl -s --location --header "Accept: application/rdf+xml" \
http://open.live.bbc.co.uk/weather/feeds/en/2643123/3dayforecast.rss | xmllint --format --xpath 'string((//channel/item/title)[2])' -`

weekday_tomorrow=`echo $tomorrow_text | sed -r 's/day:.*/day/g' | colrm 4 | \
sed -e 'y/ABCDEFGHIJKLMNOPQRSTUVWXYZ/abcdefghijklmnopqrstuvwxyz/'`

tomorrow_text=`echo $tomorrow_text | sed -r 's/Maximum Temperature:/\\\nMax Temp;/g'`
tomorrow_text=`echo $tomorrow_text | sed -r 's/Minimum Temperature:/\\\nMin Temp;/g'`
tomorrow_text=`echo $tomorrow_text | sed -r 's/.*://g'`
tomorrow_text=`echo $tomorrow_text | sed -r 's/;/:/g'`
tomorrow_text=`echo $tomorrow_text | sed -r 's/,/\\\n/g'`

convert -background black -gravity center -fill white -font Helvetica -size 320x240 -pointsize 22 -interline-spacing 10 \
label:"\n$tomorrow_text" /var/www/weather_forecast_tomorrow.jpg
convert /var/www/weather_forecast_tomorrow.jpg -font Helvetica -pointsize 80 -fill rgba\(173,216,230,0.8\) -gravity northWest \
-annotate 0 "$weekday_tomorrow" /var/www/weather_forecast_tomorrow.jpg