Created
October 18, 2010 18:54
-
-
Save smacker/632795 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# coding: utf-8 | |
require 'net/http' | |
require 'open-uri' | |
require 'nokogiri' | |
require "cgi" | |
require 'json' | |
require 'zip/zip' | |
# Config | |
FILES_DIR = '/home/www/chart/htdocs/files' | |
TEMPLATES_DIR = '/home/cyxapeff/bbcchart/template' | |
FILES_URL = 'http://chart.cyxapeff.org/files' | |
OUTPUT_DIR = '/home/www/chart/htdocs' | |
def retryable(options = {}, &block) | |
opts = { :tries => 1, :on => Exception, :on2 => 0, :on3 => 0 }.merge(options) | |
retry_exception, retry_exception2, retry_exception3, retries = opts[:on], opts[:on2], opts[:on3], opts[:tries] | |
begin | |
return yield | |
rescue retry_exception | |
retry if (retries -= 1) > 0 | |
rescue retry_exception2 | |
retry if (retries -= 1) > 0 | |
rescue retry_exception3 | |
retry if (retries -= 1) > 0 | |
end | |
yield | |
end | |
def quote(str) | |
str.gsub(/\\|'/) { |c| "\\#{c}" } | |
end | |
class ProstoPleer | |
def initialize | |
retryable(:tries => 5, :on => SocketError, :on2 => OpenURI::HTTPError, :on3 => Errno::ETIMEDOUT) do | |
open("http://prostopleer.com/") do |res| | |
@cookies = res.meta['set-cookie'].split('; ',2)[0] | |
end | |
end | |
end | |
def search(artist, track) | |
e_artist = artist.gsub(/[^a-zA-z0-9 ]/, " ").gsub(/feat/, " ").squeeze(" ").strip | |
e_track = track.gsub(/[^a-zA-z0-9 ]/, " ").gsub(/feat/, " ").squeeze(" ").strip | |
url = CGI::escape(e_artist+" "+e_track) | |
retryable(:tries => 5, :on => SocketError, :on2 => OpenURI::HTTPError, :on3 => Errno::ETIMEDOUT) do | |
open("http://prostopleer.com/search?q="+url, | |
"Cookie" => @cookies, | |
"Refer" => 'http://prostopleer.com', | |
"X-Requested-With" => 'XMLHttpRequest') do |res| | |
json = JSON.parse res.read | |
data = json["html"] | |
left = data.index("icon\"> <a") | |
if left | |
data = data.slice((data.index("icon\"> <a")+17)..data.length) | |
link = data.slice(0..(data.index(">")-2)) | |
name = artist+" - "+track+".mp3" | |
download(link, name) | |
return name | |
end | |
end | |
end | |
return 0 | |
end | |
def download(link, name) | |
retryable(:tries => 5, :on => SocketError, :on2 => OpenURI::HTTPError, :on3 => Errno::ETIMEDOUT) do | |
open("http://prostopleer.com/"+link, | |
"Cookie" => @cookies, | |
"Refer" => 'http://prostopleer.com') do |res| | |
File.open(FILES_DIR+"/"+name, "wb") do |file| | |
file.write res.read | |
end | |
end | |
end | |
end | |
end | |
class BBCParser | |
def initialize | |
con = Net::HTTP.new('www.bbc.co.uk', 80) | |
path = '/radio1/chart/singles' | |
headers = { 'Refer' => 'http://www.bbc.co.uk', | |
'User-Agent' => 'Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.1.7) Gecko/20100105 Shiretoko/3.5.7'} | |
res = con.get(path, headers) | |
@doc = Nokogiri::HTML(res.body) | |
end | |
def parse | |
result = Array.new | |
@doc.css('.chart-item').each do |entry| | |
pos = entry.css('.position').first.content.strip | |
artist = entry.css('.artist').first.content.strip | |
track = entry.css('.track').first.content.strip | |
status = entry.css('.status').first.inner_html.strip | |
cover = entry.css('.cover').first['src'] | |
result << { :position => pos, :artist => artist, | |
:track => track, :status => status, | |
:cover => cover} | |
end | |
return result | |
end | |
end | |
def create_zip(items) | |
File.delete(FILES_DIR+"/bbc_chart.zip") if File.exists?(FILES_DIR+"/bbc_chart.zip") | |
Zip::ZipFile.open(FILES_DIR+"/bbc_chart.zip", Zip::ZipFile::CREATE) do |zipfile| | |
items.each do |item| | |
name = item[:artist]+" - "+item[:track]+".mp3" | |
zipfile.add(name, FILES_DIR+"/"+name) if File.exists? FILES_DIR+"/"+name | |
end | |
end | |
end | |
if __FILE__ == $0 | |
parser = BBCParser.new | |
prostopleer = ProstoPleer.new | |
chart = parser.parse | |
chart.each do |item| | |
name = item[:artist]+" - "+item[:track]+".mp3" | |
prostopleer.search(item[:artist], item[:track]) if !File.exists? FILES_DIR+"/"+name | |
end | |
create_zip chart | |
open(TEMPLATES_DIR+"/index.html") do |file| | |
template = file.read | |
first = chart.first | |
template = template.gsub(/\{position\}/, first[:position]) | |
template = template.gsub(/\{artist\}/, first[:artist]) | |
template = template.gsub(/\{track\}/, first[:track]) | |
template = template.gsub(/\{status\}/, first[:status]) | |
template = template.gsub(/\{image\}/, first[:cover]) | |
link_output = '' | |
name = first[:artist]+" - "+first[:track]+".mp3" | |
if File.exists? FILES_DIR+"/"+name | |
template_link = open(TEMPLATES_DIR+"/big_link.html").read | |
link_output = template_link.sub(/\{link\}/, FILES_URL+"/"+name) | |
end | |
template = template.sub(/\{link\}/, link_output) | |
player_output = '' | |
if File.exists? FILES_DIR+"/"+name | |
template_player = open(TEMPLATES_DIR+"/big_player.html").read | |
player_output = template_player.sub(/\{e_link\}/, FILES_URL+"/"+CGI::escape(name)) | |
end | |
template = template.sub(/\{player\}/, player_output) | |
chart.delete first | |
items = "" | |
template_link = open(TEMPLATES_DIR+"/link.html").read | |
open(TEMPLATES_DIR+"/item.html") do |file| | |
template_item = file.read | |
chart.each do |item| | |
output = template_item | |
output = output.gsub(/\{position\}/, item[:position]) | |
output = output.gsub(/\{artist\}/, item[:artist]) | |
output = output.gsub(/\{track\}/, item[:track]) | |
output = output.gsub(/\{status\}/, item[:status]) | |
output = output.gsub(/\{image\}/, item[:cover]) | |
link_output = '' | |
name = item[:artist]+" - "+item[:track]+".mp3" | |
if File.exists? FILES_DIR+"/"+name | |
link_output = template_link.gsub(/\{e_link\}/, FILES_URL+"/"+CGI::escape(name)) | |
link_output = link_output.gsub(/\{link\}/, FILES_URL+"/"+name) | |
end | |
output = output.sub(/\{link\}/, link_output) | |
items += output | |
end | |
end | |
open(OUTPUT_DIR+"/index.html", "w") do |file| | |
file.write template.sub(/\{items\}/, items) | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment