Created
November 5, 2010 22:59
-
-
Save fritz0705/665011 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| #!/usr/bin/env ruby | |
| # Wie man es nutzt: ruby extract_youtube_links.rb <VerzeichnisMitLogDateien> | |
| require "open-uri" | |
| require "uri" | |
| require "erb" | |
| if $*[0] == "--help" || $*[0] == "help" || $*[0] == "-h" | |
| puts <<HELP | |
| ruby extract_youtube_links.rb <VerzeichnisMitLogDateien> | |
| HELP | |
| exit | |
| end | |
| dir = $*[0] | |
| tmp_file = File.new(".blablablubrandom#{rand}.tmp", "a+b") | |
| Dir.new(dir).each do |file| | |
| if file == ".." || file == "." || File::directory?(File.join(dir, file)) | |
| next | |
| end | |
| tmp_file.puts(File.new(File.join(dir, file)).read) | |
| end | |
| $stdin = tmp_file | |
| $stdin.seek(0) | |
| def strip_html_tags(string) | |
| string.gsub(/\<[^\>]+\>/, "") | |
| end | |
| lineno = 1 | |
| urls = [] | |
| until $stdin.eof? | |
| line = strip_html_tags $stdin.readline.strip | |
| unless line.include?("youtube.com") || line.include?("youtu.be") | |
| next | |
| end | |
| $stderr.puts "Processing Line #{lineno}..." | |
| URI::extract(line).each do |uri| | |
| if uri[0, 7] == "http://" | |
| urls << URI::parse(uri) | |
| end | |
| end | |
| lineno += 1 | |
| end | |
| video_ids = [] | |
| urls.each do |url| | |
| $stderr.puts "Processing URL #{url}..." | |
| if url.host.include? "youtube.com" | |
| unless url.path.include? "/watch" | |
| next | |
| end | |
| next if url.query == nil | |
| query = url.query.split("&") | |
| query.each do |q| | |
| key, value = q.split("=", 2) | |
| if key == "v" | |
| video_ids << value | |
| end | |
| end | |
| elsif url.host.include? "youtu.be" | |
| video_ids << url.path.gsub("/", "") | |
| end | |
| end | |
| videos = {} | |
| video_ids.each do |video_id| | |
| $stderr.puts "Load title of #{video_id}" | |
| content = open("http://www.youtube.com/watch?v=#{video_id}").read | |
| left, right = content.split('<span id="eow-title" class="" dir="ltr" title="', 2) | |
| if right == nil | |
| next | |
| end | |
| left, right = right.split('>', 2) | |
| title, right = right.split('</span>', 2) | |
| title = title.strip | |
| videos[video_id] = title | |
| end | |
| $stderr.puts "Generating html output..." | |
| template = ERB.new <<ERB_TPL | |
| <?xml version="1.0" encoding="utf-8"?> | |
| <html xmlns="http://www.w3.org/1999/xhtml"> | |
| <head> | |
| <title>Extracted Youtube-Links from Chatlogs</title> | |
| </head> | |
| <body> | |
| <p>Generated from many Chatlogs with a little Ruby tool ;)</p> | |
| <ol> | |
| <% videos.each do |video_id, title| %> | |
| <li><a href="http://www.youtube.com/watch?v=<%= video_id %>"><%= title %></a></li> | |
| <% end %> | |
| </ol> | |
| </body> | |
| </html> | |
| ERB_TPL | |
| File.new("index.html", "w+b").write template.result | |
| File.unlink tmp_file |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment