skipjac / pull-zendesk-forums

This will pull zendesk forums to your local computer so you can upload to a github repro
6 stars 2 forks source link

Images not loaded #5

Open StPlante opened 9 years ago

StPlante commented 9 years ago

Hi skipjac,

I contacted Zendesk in this regard, but they could not help with the script so I am turning to you, hoping you can find the time to help me.

I have tried using the script and followed the instructions provided but for some reason, attachments are never downloaded. The text content is fully downloaded, but the image folder is simply empty.

The response I get once the script is done is this :

numsapmt005:pull-zendesk-forums-master splante$ ruby pull-zendesk-forum.rb failed download https://assistancelp.zendesk.com/attachments/token/tlpofa7yoe89ctw/?name=profil_zendesk.png failed download https://assistancelp.zendesk.com/attachments/token/eptlqa91bt690eh/?name=Logo_LaPresse_web.jpg numsapmt005:pull-zendesk-forums-master splante$

Here is a copy of the script as I configured it.

require 'rubygems' require 'httparty' require 'pp' require 'FileUtils' require 'json' require 'nokogiri' require 'crack' require 'uri'

login = '**' password = '******' zendeskURL = 'assistancelp' infile = 'Backup-forums'

class Zenuser include HTTParty headers 'content-type' => 'application/json' def initialize(u, p, y) @auth = {:username => u, :password => p} self.class.base_uri 'https://' + y + '.zendesk.com' end def get_entries(count) options = {:basic_auth => @auth} self.class.get(count.to_s, options) end def get_name(forum_id, rdir) options = {:basic_auth => @auth} response = self.class.get('/api/v2/forums/' + forum_id.to_s + '.json', options) name = JSON.parse(response.body) if name['forum']['category_id'] != nil cat = self.class.get('/api/v2/categories/' + name['forum']['category_id'].to_s + '.json', options) catName = JSON.parse(cat.body) dir = rdir + "/" + catName['category']['name'] + "/" Dir.mkdir(dir) unless File.exists?(dir) forumName = catName['category']['name'] + "/" + forum_id.to_s + "-" + name['forum']['name'].to_s.gsub(/\s/,'-') else dir = rdir + "/No-Category" Dir.mkdir(dir) unless File.exists?(dir) forumName = 'No-Category' + "/" + forum_id.to_s + "-" + name['forum']['name'].to_s.gsub(/\s/,'-') end return forumName end def get_attach(file, rdir) dir = rdir +"/images/" options = {:basic_auth => @auth} Dir.mkdir(dir) unless File.exists?(dir) begin imgURL = self.class.base_uri + file response = self.class.get(imgURL, options) if response.code == 200 writeOut = open(dir + file.split('=')[-1], "wb") writeOut << self.class.get(imgURL, options) writeOut.close else puts "failed download " + imgURL end rescue puts "failed download " + imgURL end end
end

x = Zenuser.new(login, password, zendeskURL) rootDir = infile count = '/api/v2/topics.json' while count != nil do test = x.get_entries(count) testBody = JSON.parse(test.body) testBody['topics'].each do |topic| dir = rootDir + "/" + x.get_name(topic['forum_id'], rootDir) + "/" Dir.mkdir(dir) unless File.exists?(dir) File.open(dir + topic['id'].to_s + "-" + topic['title'].to_s.gsub(/\s/,'-').gsub(/\//,'-')[0,27] + ".html", 'w+') do |the_file| the_file.puts topic['body'] end topic['attachments'].each do |attachment| forumAttach = URI(attachment['content_url']).path + "?" + URI(attachment['content_url']).query x.get_attach(forumAttach, rootDir) end ntest = Nokogiri::HTML(topic['body'])

ntest.css('img').each do |img|

#   if img['src'].split('/')[1] == 'attachments'
#     x.get_attach(img['src'], rootDir)
#   end
#end

end count = testBody['next_page'] end

Thanks for your help !

skipjac commented 9 years ago

Thanks I will take a look at it. On Oct 21, 2014 6:37 AM, "StPlante" notifications@github.com wrote:

Hi skipjac,

I contacted Zendesk in this regard, but they could not help with the script so I am turning to you, hoping you can find the time to help me.

I have tried using the script and followed the instructions provided but for some reason, attachments are never downloaded. The text content is fully downloaded, but the image folder is simply empty.

The response I get once the script is done is this :

numsapmt005:pull-zendesk-forums-master splante$ ruby pull-zendesk-forum.rb failed download https://assistancelp.zendesk.com/attachments/token/tlpofa7yoe89ctw/?name=profil_zendesk.png failed download https://assistancelp.zendesk.com/attachments/token/eptlqa91bt690eh/?name=Logo_LaPresse_web.jpg numsapmt005:pull-zendesk-forums-master splante$

Here is a copy of the script as I configured it.

require 'rubygems' require 'httparty' require 'pp' require 'FileUtils' require 'json' require 'nokogiri' require 'crack' require 'uri'

login = '**' password = '******' zendeskURL = 'assistancelp' infile = 'Backup-forums'

class Zenuser include HTTParty headers 'content-type' => 'application/json' def initialize(u, p, y) @auth https://github.com/auth = {:username => u, :password => p} self.class.base_uri 'https://' + y + '.zendesk.com' end def get_entries(count) options = {:basic_auth => @auth https://github.com/auth} self.class.get(count.to_s, options) end def get_name(forum_id, rdir) options = {:basic_auth => @auth https://github.com/auth} response = self.class.get('/api/v2/forums/' + forum_id.to_s + '.json', options) name = JSON.parse(response.body) if name['forum']['category_id'] != nil cat = self.class.get('/api/v2/categories/' + name['forum']['category_id'].to_s + '.json', options) catName = JSON.parse(cat.body) dir = rdir + "/" + catName['category']['name'] + "/" Dir.mkdir(dir) unless File.exists?(dir) forumName = catName['category']['name'] + "/" + forum_id.to_s + "-" + name['forum']['name'].to_s.gsub(/\s/,'-') else dir = rdir + "/No-Category" Dir.mkdir(dir) unless File.exists?(dir) forumName = 'No-Category' + "/" + forum_id.to_s + "-" + name['forum']['name'].to_s.gsub(/\s/,'-') end return forumName end def get_attach(file, rdir) dir = rdir +"/images/" options = {:basic_auth => @auth https://github.com/auth} Dir.mkdir(dir) unless File.exists?(dir) begin imgURL = self.class.base_uri + file response = self.class.get(imgURL, options) if response.code == 200 writeOut = open(dir + file.split('=')[-1], "wb") writeOut << self.class.get(imgURL, options) writeOut.close else puts "failed download " + imgURL end rescue puts "failed download " + imgURL end end

end

x = Zenuser.new(login, password, zendeskURL) rootDir = infile count = '/api/v2/topics.json' while count != nil do test = x.get_entries(count) testBody = JSON.parse(test.body) testBody['topics'].each do |topic| dir = rootDir + "/" + x.get_name(topic['forum_id'], rootDir) + "/" Dir.mkdir(dir) unless File.exists?(dir) File.open(dir + topic['id'].to_s + "-" + topic['title'].to_s.gsub(/\s/,'-').gsub(/\//,'-')[0,27] + ".html", 'w+') do |the_file| the_file.puts topic['body'] end topic['attachments'].each do |attachment| forumAttach = URI(attachment['content_url']).path + "?" + URI(attachment['content_url']).query x.get_attach(forumAttach, rootDir) end ntest = Nokogiri::HTML(topic['body'])

ntest.css('img').each do |img|

if img['src'].split('/')[1] == 'attachments'

x.get_attach(img['src'], rootDir)

end

end

end count = testBody['next_page'] end

Thanks for your help !

— Reply to this email directly or view it on GitHub https://github.com/skipjac/pull-zendesk-forums/issues/5.