Skip to content

Instantly share code, notes, and snippets.

@TeddyDD
Last active September 4, 2018 11:43
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 TeddyDD/0905bbacb89b6904769395668f461928 to your computer and use it in GitHub Desktop.
Save TeddyDD/0905bbacb89b6904769395668f461928 to your computer and use it in GitHub Desktop.
most viewed yt wideos from firefox history
#!/usr/bin/env crun
# ---
# sqlite3:
# github: crystal-lang/crystal-sqlite3
# ...
# Lists most visited youtube wideos from Firefox history
# Example: Backup videos watched > 10 times
# youtube-dl $(mozyt.cr -p ~/.mozilla/firefox/PROFILEID.default/)
require "option_parser"
min_count = 10
profile = ""
OptionParser.parse! do |parser|
parser.banner = "Usage: #{File.basename __FILE__} -p PROFILE [flags]"
parser.on("-p PROFILE", "--profile PROFILE", "Mozilla profile directory") { |dir| profile = dir }
parser.on("-c COUNT", "--count COUNT", "Minimal visit count, default 10") do |count|
begin
min_count = count.to_i
rescue e
STDERR.puts "Error: count must be number\n#{e}"
end
end
parser.on("-h", "--help", "Show this help") { puts parser; exit 0 }
parser.invalid_option do |flag|
STDERR.puts "ERROR: #{flag} is not a valid option."
STDERR.puts parser
exit(1)
end
end
if profile == ""
STDERR.puts "You have to provide profile path with -p flag"
exit 1
end
query = "
SELECT url FROM (
SELECT COUNT(moz_historyvisits.place_id) AS vist_count, moz_places.url
FROM moz_historyvisits
INNER JOIN moz_places ON moz_historyvisits.place_id = moz_places.id
WHERE url LIKE '%youtube.com/watch%' AND visit_count > #{min_count}
GROUP BY moz_historyvisits.place_id
ORDER BY visit_count DESC )
"
require "sqlite3"
begin
DB.open "sqlite3://#{profile}/places.sqlite" do |db|
db.query query do |rs|
rs.each do
puts rs.read(String)
end
end
end
rescue e
STDERR.puts "Database error: #{e}"
end
import os
import ospaths
import parseopt
import strutils
import db_sqlite
var
profile: string
count: int = 10
proc printUsage =
let usage: string = "Usage: " & getAppFilename().extractFileName() & " -p=PROFILE [-c=COUNT]"
echo usage
quit 1
proc getOptions =
for kind, key, value in getOpt():
case kind
of cmdShortOption:
case key:
of "p":
profile = value
of "c":
count = value.parseInt()
else: printUsage()
else: printUsage()
if profile == nil:
printUsage()
proc readDb =
let path = profile.expandTilde().joinPath("places.sqlite")
let database = open(path, nil, nil, nil)
let query = sql"""
SELECT url
FROM
(SELECT COUNT(moz_historyvisits.place_id) AS vist_count,
moz_places.url
FROM moz_historyvisits
INNER JOIN moz_places ON moz_historyvisits.place_id = moz_places.id
WHERE url LIKE '%youtube.com/watch%'
AND visit_count > ?
GROUP BY moz_historyvisits.place_id
ORDER BY visit_count DESC)
"""
for r in database.fastRows(query, count):
echo $r[0]
database.close()
proc main =
getOptions()
readDb()
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment