Skip to content

Instantly share code, notes, and snippets.

@Micronarrativ
Created January 1, 2016 19:51
Show Gist options
  • Save Micronarrativ/5bf455bae7779ca8fb65 to your computer and use it in GitHub Desktop.
Save Micronarrativ/5bf455bae7779ca8fb65 to your computer and use it in GitHub Desktop.
MOCP Logging into Log-file or playlist
#!/usr/bin/env ruby
# 2016-01-01
# Script to log the played songs from MOCP into a logfile or into a playlist.
#
require 'rubygems'
require 'thor'
module MocpLogger
class MocpLogger < Thor
class_option :file, :aliases => '-f', :type => :string, :required => true
class_option :log, :aliases => '-l', :type => :string, :required => true
class_option :playlist, :aliases => '-p', :type => :boolean, :required => false, :lazy_default => true
default_task :default
$metadata = [
:artist => '',
:title => '',
:duration => '',
]
$time_now = Time.new
desc 'Default task', 'default', :hide => true
def default
if not options[:file].nil? and File.exists?(options[:file])
$metadata = readExifData(options[:file])
$metadata[:md5sum] = getMd5sum(options[:file])
outputdata = {
:timestamp => $time_now,
:artist => $metadata[:artist],
:title => $metadata[:title],
:duration => $metadata[:duration],
:file => options[:file],
:md5sum => $metadata[:md5sum],
}
if options[:playlist] and options[:log]
writePlaylist(options[:log], outputdata)
elsif options[:log]
writeLogFile(options[:log], outputdata)
end
end
end
# Helper method to get the md5sum hash of a file
desc 'Create md5sum', 'Create md5sum', :hide => true
def getMd5sum(file)
`md5sum #{file}`.split(' ').first
end
# Helper method to write the logfile
desc 'Write Logfile', 'Write the logfile', :hide => true
def writeLogFile(logfile, dataHash)
dataHash.each do |key,value|
dataHash[key.to_sym] = '"' + value.to_s + '"'
end
File.open(logfile,'a') { |file| file.write( dataHash.values.join(';').to_s + "\n" ) }
end
# Helper method to write the playlist
desc 'Write playlist', 'Write the playlist', :hide => true
def writePlaylist(playlistfile, dataHash)
# Adjust file-extension if necessary
if not File.extname(playlistfile) == '.m3u'
playlistfile = playlistfile + '.m3u'
end
if not File.exists?(playlistfile)
File.open(playlistfile,'w') { |file| file.write ( "#EXTM3U\n" ) }
end
File.open(playlistfile,'a') { |file| file.write ( "#EXTINF:#{dataHash[:duration]},#{dataHash[:title]} - #{dataHash[:artist]}\n" + dataHash[:file] + "\n" ) }
end
# Helper Method to get some stuff from the file.
desc 'Read ExifData', 'Read Exif Data', :hide => true
def readExifData(inputFile)
data = Hash.new
exifData = `exiftool #{inputFile}`
exifData.each_line do |line|
matches = line.match(/^(?<key>\D+)\ \:\ (?<value>.*)$/)
if not matches.nil?
case matches[:key]
when /artist/i
data[:artist] = matches[:value].chomp
when /title/i
data[:title] = matches[:value].chomp
when /duration/i
timematches = matches[:value].match(/(?<hours>\d{1,3})\:(?<minutes>\d{2})\:(?<seconds>\d{2})/)
data[:duration] = timematches[:hours].to_i * 3600 + timematches[:minutes].to_i * 60 + timematches[:seconds].to_i
end
end
end
data
end
end
end
MocpLogger::MocpLogger.start
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment