<commit_before># encoding: utf-8
# Sync

# All users sync
post '/full_sync' do
	authorize_basic! :sync
	content_type :json, 'charset' => 'utf-8'

	urls = Feed.all.map{ |feed| feed.url }
	urls.uniq!

	feeds = Feedzirra::Feed.fetch_and_parse(urls, {:max_redirects => 3, :timeout => 30})

	updated_count = 0
	new_items = 0
	errors = 0
	feeds.each do |url, xml|
		if xml.kind_of?(Fixnum)
			errors+=1
		end

		Feed.all(:url => url).each do |feed|
			old_count = feed.items.count

			feed.update_feed!(xml)
			updated_count+=1

			new_items += feed.items.count - old_count
		end
	end
	{ :updated => updated_count, :new_items => new_items, :errors => errors }.to_json
end

# Single user sync
namespace '/sync' do
	before do
		authorize_basic! :user
		content_type :json, 'charset' => 'utf-8'
	end

	post '/all' do
		urls = Feed.all(:user => @user).map{ |feed| feed.url }
		urls.uniq!

		feeds = Feedzirra::Feed.fetch_and_parse(urls, {:max_redirects => 3, :timeout => 30})

		puts feeds.keys.inspect

		updated_count = 0
		new_items = 0
		errors = 0
		feeds.each do |url, xml|
			if xml.kind_of?(Fixnum)
				errors+=1
			end

			Feed.all(:user => @user, :url => url).each do |feed|
				old_count = feed.items.count

				feed.update_feed!(xml)

				updated_count+=1
				new_items += feed.items.count - old_count
			end
		end
		{ :updated => updated_count, :new_items => new_items, :errors => errors }.to_json
	end

	post '/folder/:id' do |id|
		folder = Folder.first(:user => @user, :id => id)

		urls = folder.feeds.map{ |feed| feed.url }
		urls.uniq!

		feeds = Feedzirra::Feed.fetch_and_parse(urls, {:max_redirects => 3, :timeout => 30})

		updated_count = 0
		new_items = 0
		errors = 0
		feeds.each do |url, xml|
			if xml.kind_of?(Fixnum)
				errors+=1
			end

			folder.feeds.all(:user => @user, :url => url).each do |feed|
				old_count = feed.items.count

				feed.update_feed!(xml)

				updated_count+=1
				new_items += feed.items.count - old_count
			end
		end
		{ :updated => updated_count, :new_items => new_items, :errors => errors }.to_json
	end

	post '/feed/:id' do |id|
		feed = Feed.first(:user => @user, :id => id)
		old_count = feed.items.count

		feed.sync!

		new_items = feed.items.count - old_count
		{ :updated => 1, :new_items => new_items }.to_json
	end
end
<commit_msg>Fix feed sync and remove debug output<commit_after># encoding: utf-8
# Sync

# All users sync
post '/full_sync' do
	authorize_basic! :sync
	content_type :json, 'charset' => 'utf-8'

	urls = Feed.all.map{ |feed| feed.url }
	urls.uniq!

	feeds = Feedzirra::Feed.fetch_and_parse(urls, {:max_redirects => 3, :timeout => 30})

	updated_count = 0
	new_items = 0
	errors = 0
	feeds.each do |url, xml|
		if xml.kind_of?(Fixnum)
			errors+=1
			next
		end

		Feed.all(:url => url).each do |feed|
			old_count = feed.items.count

			feed.update_feed!(xml)
			updated_count+=1

			new_items += feed.items.count - old_count
		end
	end
	{ :updated => updated_count, :new_items => new_items, :errors => errors }.to_json
end

# Single user sync
namespace '/sync' do
	before do
		authorize_basic! :user
		content_type :json, 'charset' => 'utf-8'
	end

	post '/all' do
		urls = Feed.all(:user => @user).map{ |feed| feed.url }
		urls.uniq!

		feeds = Feedzirra::Feed.fetch_and_parse(urls, {:max_redirects => 3, :timeout => 30})

		updated_count = 0
		new_items = 0
		errors = 0
		feeds.each do |url, xml|
			if xml.kind_of?(Fixnum)
				errors+=1
				next
			end

			Feed.all(:user => @user, :url => url).each do |feed|
				old_count = feed.items.count

				feed.update_feed!(xml)

				updated_count+=1
				new_items += feed.items.count - old_count
			end
		end
		{ :updated => updated_count, :new_items => new_items, :errors => errors }.to_json
	end

	post '/folder/:id' do |id|
		folder = Folder.first(:user => @user, :id => id)

		urls = folder.feeds.map{ |feed| feed.url }
		urls.uniq!

		feeds = Feedzirra::Feed.fetch_and_parse(urls, {:max_redirects => 3, :timeout => 30})

		updated_count = 0
		new_items = 0
		errors = 0
		feeds.each do |url, xml|
			if xml.kind_of?(Fixnum)
				errors+=1
				next
			end

			folder.feeds.all(:user => @user, :url => url).each do |feed|
				old_count = feed.items.count

				feed.update_feed!(xml)

				updated_count+=1
				new_items += feed.items.count - old_count
			end
		end
		{ :updated => updated_count, :new_items => new_items, :errors => errors }.to_json
	end

	post '/feed/:id' do |id|
		feed = Feed.first(:user => @user, :id => id)
		old_count = feed.items.count

		feed.sync!

		new_items = feed.items.count - old_count
		{ :updated => 1, :new_items => new_items }.to_json
	end
end

{'fraction_non_alphanumeric': 0.14896851248642778, 'fraction_numerical': 0.01216069489685125, 'mean_word_length': 3.2648148148148146, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 61, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 18}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5166332', 'n_tokens_mistral': 2031, 'n_tokens_neox': 1771, 'n_words': 427}