Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
fbermel committed Nov 30, 2016
1 parent 8a4e2b2 commit 25311d1
Show file tree
Hide file tree
Showing 5 changed files with 148 additions and 0 deletions.
13 changes: 13 additions & 0 deletions .project
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>PlauschangriffDownloader</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
</buildSpec>
<natures>
<nature>com.aptana.projects.webnature</nature>
<nature>com.aptana.ruby.core.rubynature</nature>
</natures>
</projectDescription>
62 changes: 62 additions & 0 deletions lib/includes/gui.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
class Gui < FXMainWindow

def initialize(app)
@xml = Xml.new
@prepare = Prepare.new

# Initialize base class first
super(app, "Plauschangriff Downloader", :opts => DECOR_ALL, :width => 460, :height => 165)

# Create GUI

# Frames
topFrame = FXHorizontalFrame.new(self)
topFrameLeft = FXVerticalFrame.new(topFrame)
topFrameRight = FXVerticalFrame.new(topFrame, :padLeft => 25)


lbl1 = FXLabel.new(topFrameLeft, 'Select podcasts', nil, LAYOUT_LEFT)
@list1 = FXList.new(topFrameLeft, :opts => LAYOUT_FIX_WIDTH|LAYOUT_FIX_HEIGHT|LIST_EXTENDEDSELECT|LIST_MULTIPLESELECT, :width => 350, :height => 80)
@list1.fillItems(@xml.getPodcastNames)
lbl2 = FXLabel.new(topFrameRight, ' ', nil, LAYOUT_LEFT)
btnDownload = FXButton.new(topFrameRight, 'Download', :opts => FRAME_RAISED|FRAME_THICK|LAYOUT_FILL_X)
btnExit = FXButton.new(topFrameRight, 'Exit', :opts => FRAME_RAISED|FRAME_THICK|LAYOUT_FILL_X)
@lbl3 = FXLabel.new(topFrameLeft, 'Waiting...', nil, JUSTIFY_LEFT|LAYOUT_FIX_WIDTH, :width => 350)
@progressbar = FXProgressBar.new(topFrameLeft, :opts => LAYOUT_FIX_WIDTH|PROGRESSBAR_HORIZONTAL|PROGRESSBAR_NORMAL|PROGRESSBAR_PERCENTAGE, :width => 350)
@progressbar.barColor = Fox.FXRGB(152, 251, 152)

# Commands
btnExit.connect(SEL_COMMAND) { exit }
btnDownload.connect(SEL_COMMAND) { Thread.new {start} }

end

def download(url, name)
open(name, 'wb') do |file|
open(url, :progress_proc => proc { |step|
@progressbar.progress = step
}) do |uri|
file.write(uri.read)
end
end
end


def start
@list1.each do |item|
if(item.selected?)
url = @xml.getDownloadLink(item)
@progressbar.total = @prepare.getSize(url)
name = "Rocket Beans Plauschangriff - " + item.to_s + ".mp3"
@lbl3.text = "Downloading: " + item.to_s + ".mp3"
download(url, @prepare.sanitize(name))
end
end
end


def create
super
show(PLACEMENT_SCREEN)
end
end
24 changes: 24 additions & 0 deletions lib/includes/prepare.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
class Prepare

def initialize()

end

def sanitize(filename)
bad_chars = [ '/', '\\', '?', '%', '*', ':', '|', '"', '<', '>' ]
bad_chars.each do |bad_char|
filename.gsub!(bad_char, ' ')
end
filename
end

def getSize(url)
res = nil
urlbase = url.split('/')[2]
urlpath = '/'+url.split('/')[3..-1].join('/')
Net::HTTP.start(urlbase, 80) do |http|
res = http.head(urlpath)
end
res['content-length'].to_f
end
end
31 changes: 31 additions & 0 deletions lib/includes/xml.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
class Xml

def initialize
@doc = Nokogiri::XML(open("http://rocketbeans.tv/plauschangriff.xml"))
end

def getPodcastNames
@podcastnames = Array.new
@doc.xpath('//item').each do |parent|
@podcastnames.push(parent.xpath('./title').text)
end
@podcastnames
end

def escape_single_quotes_for_xpath(string)
if string =~ /'/
%[concat('] + string.gsub(/'/, %[', "'", ']) + %[')] # horribly, this is how you escape single quotes in XPath.
else
%['#{string}']
end
end

def getDownloadLink(podcastname)
pod = escape_single_quotes_for_xpath(podcastname.to_s)
expression = "//title[text() = #{pod}]/.."
node = @doc.xpath(expression)
node.xpath('./enclosure/@url').to_s

end

end
18 changes: 18 additions & 0 deletions lib/main.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
$:.unshift File.dirname(__FILE__)

require 'fox16'
include Fox

require 'nokogiri'
require 'open-uri'
require 'includes/gui'
require 'includes/xml'
require 'includes/prepare'

if __FILE__ == $0
FXApp.new("Plauschangriff Downloader", "FXRuby") do |app|
Gui.new(app)
app.create
app.run
end
end

0 comments on commit 25311d1

Please # to comment.