Skip to content

Instantly share code, notes, and snippets.

@Rybots
Created May 22, 2016 15:45
Show Gist options
  • Save Rybots/d465f1debe741497052871033acf62f3 to your computer and use it in GitHub Desktop.
Save Rybots/d465f1debe741497052871033acf62f3 to your computer and use it in GitHub Desktop.
require 'csv'
require 'json'
tsv = []
total = []
JSON.parse(File.open("1.json").read).each do |hash| #open json to parse
(0..6).each do |t|
begin
if hash["user"]["favorite_manga"][t]["author"] != nil
tsv << hash["user"]["id"].to_s
tsv << hash["user"]["login"].to_s
tsv << hash["user"]["favorite_manga"][t]["id"].to_s
tsv << hash["user"]["favorite_manga"][t]["title"].to_s
tsv << hash["user"]["favorite_manga"][t]["author"].to_s
tsv << hash["user"]["favorite_manga"][t]["mangapedia_url"].to_s
total << tsv
tsv = []
end
rescue
break
end
end
end
JSON.parse(File.open("2.json").read).each do |hash| #open json to parse
(0..6).each do |t|
begin
if hash["user"]["favorite_manga"][t]["author"] != nil
tsv << hash["user"]["id"].to_s
tsv << hash["user"]["login"].to_s
tsv << hash["user"]["favorite_manga"][t]["id"].to_s
tsv << hash["user"]["favorite_manga"][t]["title"].to_s
tsv << hash["user"]["favorite_manga"][t]["author"].to_s
tsv << hash["user"]["favorite_manga"][t]["mangapedia_url"].to_s
total << tsv
tsv = []
end
rescue
break
end
end
total.sort_by!{ |a|
[a[0][/\A\d+\z/] ? "%3s" % a[0] : a[0],
a[2][/\A\d+\z/] ? "%3s" % a[2] : a[2]]
}
header = ["user_id","user_login","manga_id","manga_title","manga_author","mangapedia_title_url"]
CSV.open('total.tsv','w',:encoding => "SJIS",:headers => true,:col_sep => " ",:force_quotes => true) do |file|
file << header
total.each do |line|
file << line
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment