Skip to content

Instantly share code, notes, and snippets.

@kuyseng
Last active July 23, 2021 04:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kuyseng/53e6ebbab78ce7979c579bdd70319594 to your computer and use it in GitHub Desktop.
Save kuyseng/53e6ebbab78ce7979c579bdd70319594 to your computer and use it in GitHub Desktop.
Script to download
#!/bin/bash
# For Unix OS such as: Ubuntu, Mac
## install
# - curl -o linkedin_download.sh https://gist.githubusercontent.com/kuyseng/53e6ebbab78ce7979c579bdd70319594/raw/e6552ebddc398025c7f4f58cd5fad5c22a7a5c43/linkedin_download.sh
# - chmod +x linkedin_download.sh
# - mv linkedin_download.sh /usr/local/bin/.
# restart your terminal
## Usage:
# linked_download.sh COOKIE_PATH [URL | LIST_PATH]
## Feature
# - download from course url or text file with a list of course urls.
# - download with saving archive. It help to prevent the same video multiple times.
COOKIE_PATH=$1
COOKIE_DIR=$(dirname "$COOKIE_PATH")
DOWNLOADED_PATH=$COOKIE_DIR/downloaded.txt
# create downloaded.txt if not exist
if [ ! -f $DOWNLOADED_PATH ]; then
touch $DOWNLOADED_PATH
fi
function download() {
name=$1
url=$2
echo "== Start download: $url ==\n"
youtube-dl --no-overwrites --download-archive $DOWNLOADED_PATH --cookies $COOKIE_PATH --autonumber-start 1 -o "$name" "$url"
}
LIST_PATH=$2
if [ -f $LIST_PATH ]; then
# it's a text file. then read it into array lines
IFS=$'\n' read -d '' -r -a lines < $LIST_PATH
for index in "${!lines[@]}"; do
count=$((index+1))
line=${lines[$index]}
download "$count - %(playlist)s/%(chapter_number)s - %(chapter)s/%(playlist_index)02d - %(title)s.mp4" "$line"
done
else
# it's a URL
download "%(playlist)s/%(chapter_number)s - %(chapter)s/%(playlist_index)02d - %(title)s.mp4" "$LIST_PATH"
fi
#!/usr/bin/ruby
## install
# - curl -o rename_course_titles.rb https://gist.githubusercontent.com/kuyseng/53e6ebbab78ce7979c579bdd70319594/raw/3ca8c2ec4b3d3bf35394a04b242548ae1f948a93/rename_course_titles.rb
# - chmod +x rename_course_titles.rb
## Usage:
# ./rename_course_titles.rb DIR TITLE_FILE_PATH
current_dir = ARGV[0] || '.'
title_path = ARGV[1] || "#{current_dir}/titles.txt"
ext = '.mp4'
def key(title)
title.scan(/[a-zA-Z]+/).map(&:downcase).join('-')
end
unless File.exists?(title_path)
puts "No title file"
exit 1
end
titles = {}
File.readlines(title_path).map(&:chomp).each do |title|
titles[key(title)] = title
end
def renmae(filepath, new_filepath)
File.rename()
end
count = 0
Dir.glob("#{current_dir}/**/*#{ext}") do |path|
filename = File.basename(path).sub(/#{ext}$/, '')
title = titles[key(filename)]
dir = File.dirname(path)
new_path = "#{dir}/#{title}#{ext}"
puts "#{path} => #{new_path}"
File.rename(path, new_path)
count += 1
end
puts "Total file renamed: #{count}"
(function get_course(selector='.classroom-toc-item__title', filename='titles.txt') {
const elems=$(selector)
const arr = []
for(let i = 0, index = 1, title, prefix; i<elems.length; i++) {
title = elems[i].textContent.trim().split('\n')[0]
if((/quiz/i).test(title)) continue;
prefix = String(index++).padStart(2, '0'); // 01, 02,..,10,11,..
title = `${prefix} - ${title}`
console.log(title)
arr.push(title)
}
const data = arr.join('\n')
save(data, filename);
download_excercise_file()
function download_excercise_file() {
const button = $('button[aria-label="Show all exercise files"]')
if(button.length == 0) return;
button[0].click();
const elem = $('.classroom-exercise-files-modal__exercise-file-download')
if(elem.length == 0) return;
elem[0].click();
}
function save(data, filename) {
if (!data) {
console.error('Console.save: No data');
return;
}
if (!filename) {
filename = 'console.json';
}
if (typeof data === 'object') {
data = JSON.stringify(data, undefined, 4);
}
var blob = new Blob([data], { type: 'text/json' }),
e = document.createEvent('MouseEvents'),
a = document.createElement('a');
a.download = filename;
a.href = window.URL.createObjectURL(blob);
a.dataset.downloadurl = ['text/json', a.download, a.href].join(':');
e.initMouseEvent('click', true, false, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null);
a.dispatchEvent(e);
}
})()
(function get_course(selector='[data-test-entity-link-entity-type="COURSE"]', filename='list.txt') {
const elements = $(selector)
const arr = []
for(let i =0; i< elements.length; i++) {
let elem = elements[i];
let url = "https://www.linkedin.com" + elem.getAttribute('href').split('?')[0];
if(arr.indexOf(url) < 0) arr.push(url);
}
const data = arr.join('\n')
console.log(data)
save(data, filename);
function save(data, filename) {
if (!data) {
console.error('Console.save: No data');
return;
}
if (!filename) {
filename = 'console.json';
}
if (typeof data === 'object') {
data = JSON.stringify(data, undefined, 4);
}
var blob = new Blob([data], { type: 'text/json' }),
e = document.createEvent('MouseEvents'),
a = document.createElement('a');
a.download = filename;
a.href = window.URL.createObjectURL(blob);
a.dataset.downloadurl = ['text/json', a.download, a.href].join(':');
e.initMouseEvent('click', true, false, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null);
a.dispatchEvent(e);
}
})()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment