Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Migrated to Confluence 5.3

Below is a small piece of Ruby code that I wrote to harvest mp3 files from an RSS feed. The code was designed to be run as a cron on a local machine once a day. It checks an RSS RSS2 feed for new content, looks for any mp3s listed in the enclosure tags, and if they exist, it downloads each of them to my computer, creates a SIP that DSPACE can accept, uploads the SIP to my DSpace machine, and imports it using the batch importer. I thought that someone else might be able want to use or adapt something like this.

To use the code, create a file with a name like "mp3harvester.rb". Put the code in it. Change the appropriate paths, usernames, and passwords. Ensure that you have Ruby and the referenced Gems installed. Then, just run the file on a cron job once a day.

Code Block
\# Creator: Jason Fowler
\# This   script is designed to harvest mp3 files and their metadata from the RSS feed and add it to DSpace

\# get required libraries
require "rss/1.0"
require "rss/2.0"
require "rss"
require "mp3info"
require "shared-mime-info"
require 'net/sftp'
require 'net/ssh'

\# supply local directory for storage here
def go_home
    	Dir.chdir("/PathToStagingDirectoryOnMyLocalMachine")
end

\# supply your Dspace server's URL, your username, and your password below.
def upload(package)
    	# Upload the package to DSpace
    	Net::SFTP.start('my.dspace-server.edu', 'dspace-username', :password => 'dspace-password') do \|sftp\|
        		sftp.upload\!("#{package}", "/PathToBatchDirOnMyServer/batch_uploads/#{package}")
    	end

\# Import the package into DSpace
    	Net::SSH.start('my.dspace-server.edu', 'dspace-username', :password => 'dspace-password') do \|ssh\|
        		ssh.exec 'rm /PathToBatchDirOnMyServer/batch_uploads/mapfile.txt'
        		ssh.exec 'cd /PathToDspace/bin/'
        		ssh.exec "/PathToDspace/bin/dsrun org.dspace.app.itemimport.ItemImport \-a \-e myemail@myinstitution.edu \-c 12345/1 \-s /mntPathToBatchDirOnMyServer/storage/batch_uploads/#{package} \-m /mntPathToBatchDirOnMyServer/storage/batch_uploads/mapfile.txt"
    	end
end


\# Get RSS Feed and parse it
source = "http://MyBlog.com/resources/feed/rss2"         # replace this with your blog's RSS url
content = "" # raw content of rss feed will be loaded here
open(source) do \|s\| content = s.read end
rss = RSS::Parser.parse(content, false)

\# Create Temporary Directory for storing files
go_home
@time = Time.now.strftime("%Y%m%d")
@directory = "#{@time}"
Dir.mkdir("#{@directory}")
Dir.chdir("#{@directory}")


\
# Download the mp3s
@dirname = 0
rss.items.each do \|s\|
       |
        if s.enclosure.nil? == false
&nbsp;&nbsp; &nbsp;	if Time.now - 86400 < s.date
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;date 
                #The following puts statements are just for debugging purposes.                @author = s.dc_creator
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;puts @author
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;		puts @author
		@title = s.title
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;puts @title
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;		puts @title
                @date = s.date
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;puts @date
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;		puts @date
                @url = s.enclosure.url
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;puts @url
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;		puts @url
                @categories = s.categories
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; def contents
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; @array=\[\]
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;                def contents
                  @array=[]
                  for c in @categories do
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;                    @array.push(c.content)
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; end
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; end
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; contents
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; puts @array
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;                  end
                end
                contents
                puts @array

		#Determine if the file is an mp3, and if so continue&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;
		file = open(s.enclosure.url)
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;		if MIME\['audio/mpeg'\].match_file?(file) == true
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;
		@filename = File.basename("#{s.enclosure.url}")
\#&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;@dirname = File.basename(@filename, '.mp3')
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;		Dir.mkdir("#{@dirname}")
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;		Dir.chdir("#{@dirname}")
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;
		open("#{@filename}","w").write(open("#{s.enclosure.url}").read)
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;
			#Write the Dublin Core
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;			File.open("./dublin_core.xml", 'w') do \|x\|
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;|
				x.puts '<?xml version="1.0" encoding="UTF-8"?>'
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;				x.puts "<dublin_core schema='dc'>"
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;				x.puts("\t" + '<dcvalue element="title" qualifier="none">' + "#{@title}" + '</dcvalue>')
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;				x.puts("\t" + '<dcvalue element="contributor" qualifier="author">' + "#{@author}" + '</dcvalue>')
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;				for c in @array do
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;					x.puts("\t" + '<dcvalue element="subject" qualifier="none" language="en">' + "#{c}" + '</dcvalue>')
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;end
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;				end
				x.puts("<\/dublin_core>")
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;end
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;			end

			#Write the contents list
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;			File.open("./contents", 'w') do \|x\|
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;|
				x.puts "#{@filename}\tbundle:ORIGINAL"
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;				x.puts"license.txt\tbundle:LICENSE"
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;end
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;			end

			#Write the license&nbsp;&nbsp; &nbsp;
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;
			File.open("./license.txt", 'w') do \|x\|
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;|
				x.puts "This is a dummy license."
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;end
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;			end
		puts "\n"
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;		Dir.chdir("../")
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;		@dirname = @dirname + 1&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;
&nbsp;&nbsp; &nbsp; &nbsp;&nbsp; &nbsp;end
&nbsp;&nbsp; &nbsp;

&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;end
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;
	 	end


		end
		end

end

go_home

upload(@directory)