Skip to content

Instantly share code, notes, and snippets.

@shavit
Created January 14, 2016 16:37
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save shavit/a075acf15daeeaf58e39 to your computer and use it in GitHub Desktop.
Save shavit/a075acf15daeeaf58e39 to your computer and use it in GitHub Desktop.
Mass video importer from *.csv files to tube sites.
#
# Import videos from CSV
#
# CSV row:
# 0 - embedded_video
# 1 - link
# 2 - categories (Funny;Euro;Cam)
# 3 - rating
# 4 - author username
# 5 - title
# 6 - tags (movie;homemade;european)
# 7 - duration
# 8 - pornstars (First Last;First Last)
# 9 - thumbnail
# 10 - filpbook (url;url;url)
#
def self.import_from_csv
require "csv"
puts "---> Reading from videos1.csv"
csv_data = CSV.read(
Rails.root.join("tmp", "videos", "videos1.csv"),
col_sep: "|",
headers: :first_row,
encoding: "utf-8",
quote_char: "|"
)
puts "---> Iterating through #{csv_data.length} rows"
csv_data.each_with_index do |row, i|
puts "---> row #{i}"
p = Post.find_or_create_by provider_link: row[1]
p.provider_name = "pornhub"
p.post_type = "video"
p.title = row[5].split(" ").map{|n| n.capitalize}.join(" ")
p.slug = p.title.parameterize
if Post.find_by_slug(p.slug).present?
p.title = p.title+" - Watch Online Free"
p.slug = p.title.parameterize
end
p.description = self.generate_description(p.title)
p.remote_picture_url = row[9]
p.embedded_video = row[0]
p.duration = row[7].to_i
p.pornstars_names = row[8]
p.flipbook = row[10]
p.categories_names = row[2]
p.provider_data = row.to_s
# Create tags
post_tags = []
row[6].to_s.split(";").each do |tag|
t = Tag.find_or_create_by slug: tag.parameterize
t.name = tag.capitalize
t.save
post_tags.push t
end
# Create categories
row[2].to_s.split(";").each do |tag|
t = Tag.find_or_create_by slug: tag.parameterize
t.name = tag.capitalize
t.save
post_tags.push t
end
p.tags = post_tags
# Create author
author = Author.find_or_create_by slug: row[4]
author.name = row[4].split(/_|-|\s/).map{|n| n.capitalize}.join(" ")
author.author_type = "user"
author.number_of_videos = author.number_of_videos.to_i + 1
author.save
p.author = author
# Saving
begin
p.save
puts "---> Saving post #{p.id}"
rescue e
p.remove_picture
puts "---> Error: Can't save post #{p.id}."
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment