diff --git a/BiliConfig.rb b/BiliConfig.rb index 689bd62..fc67a43 100644 --- a/BiliConfig.rb +++ b/BiliConfig.rb @@ -1,6 +1,10 @@ module BiliConfig - class Biliconf + require_relative 'BiliFile' + + class BiliConf + + include BiliFile $userHome = `echo $HOME`.gsub(/\n/,"") $configPath = File.join($userHome,".config/BiliGui") @@ -19,8 +23,7 @@ def initialize(name="biligui.conf", path=$configPath) @configEntries = {} unless File.exists?(@config) then - io = File.open(@config, "w") - io.close + biliTouch(@config) else io = File.open(@config, "r") io.each_line do |line| @@ -34,28 +37,14 @@ def initialize(name="biligui.conf", path=$configPath) end - def writeNewConfig(key, value) + def write(key, value) configKey = key configValue = value # if Key exists, then we should delete - if @configEntries.key?(configKey) then - - require 'fileutils' - - tmpfile = @config + ".tmp" - oldfile = @config + ".old" - - open(@config, 'r') do |f0| - open(tmpfile, 'w') do |f1| - f0.each_line do |line| - f1.write(line) unless line.index(configKey) - end - end - end - - FileUtils.mv @config, oldfile - FileUtils.mv tmpfile, @config + if @configEntries.key?(configKey) then + + billMove(@config,"! line.index(configKey)") end @@ -64,7 +53,7 @@ def writeNewConfig(key, value) io.close end - def loadConfigs + def load return @configEntries end diff --git a/BiliFile.rb b/BiliFile.rb new file mode 100644 index 0000000..4b52baf --- /dev/null +++ b/BiliFile.rb @@ -0,0 +1,52 @@ +module BiliFile + + require 'fileutils' + + def biliMove(file, newfile=file, condition) + + tmp = file + ".tmp" + + open(file, 'r') do |f0| + open(tmp, 'w') do |f1| + f0.each_line do |line| + + status = true + code = eval(condition) + + if code != nil then + if code == false then + status = false + else + if code != true then + if code > 0 then + status = true + else + status = false + end + else + status = true + end + end + else + status = false + end + + f1.write(line) if status + end + end + end + + if newfile == file then + oldfile = file + ".old" + FileUtils.mv file, oldfile + end + + FileUtils.mv tmp, newfile + + end + + def biliTouch(file="") + open(file, 'w').close + end + +end diff --git a/BiliPlaylist.rb b/BiliPlaylist.rb index 8e49c23..f4bf605 100644 --- a/BiliPlaylist.rb +++ b/BiliPlaylist.rb @@ -6,10 +6,10 @@ def initialize(videos="") @videos = videos @videosHash = {} - videosSplit + split end - def videosSplit + def split unless @videos.empty? then @@ -50,9 +50,9 @@ def save(filename="") old_playlist = playlist + ".old" if File.exist?(playlist) then - FileUtils.mv playlist, old_playlist + mv playlist, old_playlist else - io = File.open(playlist,"w") + io = open(playlist,"w") io.puts "#EXTM3U" @videosHash.to_a.each do |video| @@ -66,7 +66,7 @@ def save(filename="") def load(playlist="") - playlistHash = {} + hash = {} default_playlist = "#{$configPath}/biliplaylist.m3u8" @@ -79,9 +79,9 @@ def load(playlist="") p "[ERR] No playlist available!" else - io = File.open(playlist, 'r') + io = open(playlist, 'r') - validArray = [] + array = [] i = 0 io.each_line do |line| @@ -97,19 +97,23 @@ def load(playlist="") unless line.index("bilibili") then p "#{line} doesn't seem to be a Bilibili URL!" else - p line - validArray[i] = line + value = line + key = "av" + line.gsub(/^.*\/av/,"").gsub(/\//,"") + hash[key] = value + array[i] = line end end - if validArray.empty? then + if array.empty? then p "This playlist has no URL we support!" end io.close end + return hash + end end diff --git a/BiliWeb.rb b/BiliWeb.rb new file mode 100644 index 0000000..876d353 --- /dev/null +++ b/BiliWeb.rb @@ -0,0 +1,75 @@ +#!/usr/bin/ruby + +module BiliWeb + + require 'open-uri' + require_relative 'BiliConfig' + + class BiliFetch + + include BiliConfig + include BiliFile + + def initialize(url) + @url = url + @cachePath = File.join($configPath,"cache") + unless Dir.exists?(@cachePath) then + Dir.mkdir(@cachePath) + end + @filename = @cachePath + "/" + @url.gsub(/http:\/\//,"") + ".html" + end + + def get + content = open(@url).read + io = File.open(@filename, "w") + io.puts(content) + io.close + end + + def clean(filename="#{@filename}") + + if filename.index("bilibili.tv.html") then + + biliMove(filename,"line.index('/video/') && ! line.index('av271')") + + else + p "[WARN] Don't know what to do!" + end + + end + + def format + end + + def indexLevels + indexFile = @cachePath + "/bilibili.tv.html" + indexLevel1 = indexFile + ".level1" + indexLevel2 = indexFile + ".level2" + + biliMove(indexFile,indexLevel1,"line.index('i-link')") + biliMove(indexFile,indexLevel2,"! line.index('i-link')") + + end + + end + +end + +# Test code below + +#class Test +# include BiliWeb + +# def initialize(url) + #@url = url +# end + +# def get +# BiliFetch.new(@url).get +# BiliFetch.new(@url).clean +# BiliFetch.new(@url).indexLevels +# end + +#end + +#Test.new("http://bilibili.tv").get diff --git a/BiliWidgets.rb b/BiliWidgets.rb index e2c14be..336ddf7 100644 --- a/BiliWidgets.rb +++ b/BiliWidgets.rb @@ -6,16 +6,16 @@ class BiliGuiConfig include BiliConfig - @@config = Biliconf.new + @@config = BiliConf.new def put(key,value) confKey = key confValue = value - @@config.writeNewConfig(confKey,confValue) + @@config.write(confKey,confValue) end def load - @@config.loadConfigs() + @@config.load end end @@ -64,8 +64,8 @@ def initialize super setWindowTitle "BiliGui" - setWindowIcon(Qt::Icon.new("bilibili.svgz")) + #setStyleSheet "QWidget {color: #ff3d6a;}" init_ui @@ -98,8 +98,12 @@ def init_ui biliTabs.addTab webTab, "Bilibili.tv" biliTabs.addTab settingsTab, "BiliGui Settings" + @messageLabel = Qt::Label.new "" + @messageLabel.setStyleSheet("color: #ff0000;") + grid_biliTabs = Qt::GridLayout.new self grid_biliTabs.addWidget biliTabs, 0, 0, 1, 1 + grid_biliTabs.addWidget @messageLabel, 1, 0, 1, 1 grid_biliTabs.setColumnStretch 0, 0 # Playlist Tab @@ -108,8 +112,6 @@ def init_ui biliUrlLabel = Qt::Label.new "Please paste Bilibili URL below", playlistTab biliWebButton = Qt::PushButton.new 'Visit bilibili.tv (experimental)', playlistTab @urlArea = Qt::TextEdit.new playlistTab - @messageLabel = Qt::Label.new "", playlistTab - @messageLabel.setStyleSheet("color: #ff0000;") ctlPanel = Qt::Widget.new playlistTab okButton = Qt::PushButton.new 'Play', playlistTab clearButton = Qt::PushButton.new 'Clear', playlistTab @@ -118,7 +120,6 @@ def init_ui grid_Playlist.addWidget biliWebButton, 0, 1, 1, 1 grid_Playlist.addWidget @urlArea, 1, 0, 1, 4 grid_Playlist.addWidget ctlPanel, 1, 4, 1, 1 - grid_Playlist.addWidget @messageLabel, 2, 0, 1, 2 grid_Playlist.addWidget okButton, 2, 2, 1, 1 grid_Playlist.addWidget clearButton, 2, 3, 1, 1 grid_Playlist.setColumnStretch 0, 0 @@ -186,12 +187,12 @@ def bilidan command = "#{pathText} #{hashvalue}" Open3.popen3(command) do |stdin, stdout, stderr, wait_thr| stderr.each_line do |line| - # 99% is a common error can safely ignore - unless line.index("99%") then + # common error + unless line.index("Cache") then @messageLabel.setText(line) end - stdout.each_line {|line| p line} + #stdout.each_line {|line| p line} end @@ -235,7 +236,12 @@ def biliWeb def biliLoad playlist = Qt::FileDialog.getOpenFileName(self, "Please choose your playlist", "#{$configPath}", "Playlist file (*.m3u8)") unless playlist == nil then - BiliGuiPlaylist.new.load(playlist) + hash = BiliGuiPlaylist.new.load(playlist) + str = "" + hash.each_value do |value| + str += value + "\n" + end + @urlArea.setText(str) end end diff --git a/ChangeLog b/ChangeLog index a275fbc..842b8e8 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,8 @@ +0.0.6 +* fix: forgot to have BiliPlaylist::BiliLoad functional +* initialize BiliWeb module, handling communication w/ bilibili.tv +* split common file functions to BiliFile module +* logo: add drop shadow 0.0.5 * switch to Tab layout for future features * load/save bilibili playlist file. diff --git a/TODO b/TODO index 6d9db0d..4572a16 100644 --- a/TODO +++ b/TODO @@ -1,8 +1,8 @@ * let error message fade out * autoload playlists -* capture more useful informations from console output -* more error detecting -* Bangou (avXXXX) paste support +* capture more useful informations from console output (to fill BiliPlaylist metadata) +* more error detection (use raise maybe) +* Bangou (avXXXX) paste support (use "\n",",",";" to separate) * custom look and feel * play history diff --git a/bilibili.png b/bilibili.png index 193bbfc..a2cc357 100644 Binary files a/bilibili.png and b/bilibili.png differ diff --git a/bilibili.svgz b/bilibili.svgz index bc7d0cd..87a728b 100644 Binary files a/bilibili.svgz and b/bilibili.svgz differ