Skip to content

Instantly share code, notes, and snippets.

@loicpirez
Last active June 24, 2018 16:53
Show Gist options
  • Save loicpirez/ea33fa7d07e929203bcffab6e865cab3 to your computer and use it in GitHub Desktop.
Save loicpirez/ea33fa7d07e929203bcffab6e865cab3 to your computer and use it in GitHub Desktop.
Quick and dirty ugly script to get YouTube History playlist into a JSON. You need to extract the history page as an HTM file with Chrome and rename it 'history.htm' in same folder of script. Note that this is for French version of YouTube. Tweak it to fill your need, needed to have a quick result.
#!/usr/bin/env ruby
require 'json'
require 'nokogiri'
html = File.read('history.htm')
page = Nokogiri::HTML(html)
uniq_results = []
results = []
page.css('#video-title').each do |video|
element = Nokogiri::HTML(video.to_s).at_css('a')
label = element['aria-label'].to_s.reverse!.match(/seuv(?<upload_date>.*?)a y li(?<author>.*?)ed(?<title>.*)/)
if label.nil?
label = element['aria-label'].to_s.reverse!.match(/(?<upload_date>.*?)a y li(?<author>.*?)ed(?<title>.*)/)
upload_date = label['upload_date'].to_s.strip.reverse!
views = 0
else
views = element['aria-label'].to_s.split('il y a')[1].to_s.split('vues')[0].split(' ').reverse![0].to_s.strip.gsub(/[[:space:]]/, '')
upload_date = label['upload_date'].to_s.strip.reverse!.split(' ')[0...-1].join(' ')
end
link = element['href'].to_s.split('&t=')[0]
title = label['title'].to_s.strip.reverse!
author = label['author'].to_s.strip.reverse!
data = {
'link' => link,
'title' => title,
'author' => author,
'upload_date' => upload_date,
'views' => views
}
@collection = data
results.push(@collection)
end
results.each_with_index do |result, index|
occurence = results.count(result)
result["number_of_personnal_views"] = occurence
uniq_results.push(result)
end
results = uniq_results.uniq
sorted_results = results.sort_by{ |e| e['number_of_personnal_views'].to_i }
puts sorted_results.map { |o| Hash[o.each_pair.to_a] }.to_json
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment