-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Refactor sync script #175
Merged
+4,258
−1,999
Merged
Refactor sync script #175
Changes from 6 commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
8658516
Refactor dev.to sync script
5fd5612
Update workflow file
93225ae
Prepare classes for http adapters
3924291
Added tests. Changed code to use http adapters
e3b477a
Update coderabbit ignore rules
01b97f3
fix coderabbit warnings
bfdf395
Update bin/sync/images_downloader.rb
dgorodnichy c0bec13
Update bin/sync/sync.rb
dgorodnichy b2b9ba5
Update test/integration/sync_with_devto_test.rb
dgorodnichy aa6b268
Update test/integration/sync_with_devto_test.rb
dgorodnichy 73007ad
Move retries to module
36243df
chore: fix code style
pftg cd1b888
sync posts
pftg 9bd42da
run sync
pftg 72b4220
run sync
pftg ecd19c0
run sync
pftg 34fe72a
run sync
pftg 90818cb
fix tests
9ee9298
Skip canonical URL update on dev.to for articles with matching local …
a9a4c50
synced dev.to
pftg fc65bc4
Merge remote-tracking branch 'origin/master' into refactor-sync-script
pftg File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
require 'fileutils' | ||
require 'yaml' | ||
|
||
module ArticleCleaner | ||
SYNC_STATUS_FILE = 'sync_status.yml'.freeze | ||
ARTICLE_FILE = 'index.md'.freeze | ||
|
||
def cleanup_renamed_articles | ||
raise ArgumentError, "Working directory doesn't exist" unless Dir.exist?(working_dir) | ||
|
||
deleted_folders = [] | ||
slugs = load_slugs_from_yaml | ||
|
||
Dir.glob("#{working_dir}/*").each do |folder_path| | ||
next unless File.directory?(folder_path) && File.exist?("#{folder_path}/#{ARTICLE_FILE}") | ||
|
||
folder_name = File.basename(folder_path) | ||
unless slugs.include?(folder_name) | ||
begin | ||
FileUtils.rm_rf(folder_path) | ||
deleted_folders << folder_name | ||
puts "Deleted folder: #{folder_name}" | ||
rescue StandardError => e | ||
puts "Failed to delete folder #{folder_name}: #{e.message}" | ||
end | ||
end | ||
end | ||
deleted_folders | ||
end | ||
dgorodnichy marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
private | ||
|
||
def load_slugs_from_yaml | ||
yaml_path = File.join(working_dir, SYNC_STATUS_FILE) | ||
|
||
begin | ||
yaml_data = YAML.load_file(yaml_path) | ||
raise "Invalid YAML structure" unless yaml_data.is_a?(Hash) | ||
|
||
yaml_data.values.map do |article| | ||
raise "Invalid article data structure" unless article.is_a?(Hash) && article[:slug] | ||
article[:slug] | ||
end | ||
rescue StandardError => e | ||
logger.error "Failed to load slugs from YAML: #{e.message}" | ||
[] | ||
end | ||
end | ||
dgorodnichy marked this conversation as resolved.
Show resolved
Hide resolved
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
require 'json' | ||
|
||
module ArticleSyncChecker | ||
USERNAME = 'jetthoughts'.freeze | ||
dgorodnichy marked this conversation as resolved.
Show resolved
Hide resolved
|
||
SYNC_STATUS_FILE = 'sync_status.yml'.freeze | ||
USELESS_WORDS = %w[and the a but to is so].freeze | ||
dgorodnichy marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
dgorodnichy marked this conversation as resolved.
Show resolved
Hide resolved
|
||
def update_sync_status | ||
ensure_sync_status_file_exists | ||
@sync_status = sync_status | ||
dgorodnichy marked this conversation as resolved.
Show resolved
Hide resolved
|
||
update_status(fetch_articles) | ||
save_sync_status | ||
end | ||
dgorodnichy marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
private | ||
|
||
def ensure_sync_status_file_exists | ||
sync_file_path = File.join(working_dir, SYNC_STATUS_FILE) | ||
|
||
unless File.exist?(sync_file_path) | ||
File.open(sync_file_path, 'w') { |file| file.write({}.to_yaml) } | ||
end | ||
end | ||
|
||
def save_sync_status | ||
File.open(File.join(working_dir, SYNC_STATUS_FILE), 'w') do |file| | ||
file.write(@sync_status.to_yaml) | ||
end | ||
dgorodnichy marked this conversation as resolved.
Show resolved
Hide resolved
|
||
end | ||
dgorodnichy marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
def fetch_articles | ||
response = http_client.get_articles(USERNAME, 0) | ||
JSON.parse(response.body) | ||
end | ||
dgorodnichy marked this conversation as resolved.
Show resolved
Hide resolved
dgorodnichy marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
def slug(article) | ||
slug_parts = article['slug'].split('-')[0..-2] | ||
tags = article['tags'] ? article['tags'].split(", ") : [] | ||
selected_tags = tags.first(2) | ||
[slug_parts, selected_tags] | ||
.flatten | ||
.uniq | ||
.reject { |segment| USELESS_WORDS.include?(segment) } | ||
.compact | ||
.join('-') | ||
end | ||
dgorodnichy marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
def update_status(articles) | ||
articles.each do |article| | ||
id = article['id'] | ||
dgorodnichy marked this conversation as resolved.
Show resolved
Hide resolved
|
||
edited_at = article["edited_at"] || article["created_at"] | ||
|
||
@sync_status[id] ||= { edited_at: edited_at, slug: slug(article), synced: false } | ||
|
||
if @sync_status[id][:edited_at] != edited_at | ||
@sync_status[id][:edited_at] = edited_at | ||
@sync_status[id][:synced] = false | ||
end | ||
end | ||
end | ||
dgorodnichy marked this conversation as resolved.
Show resolved
Hide resolved
|
||
end |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
let's avoid to store data files from content, it should be located in data folder as I remember, please check hugo docs about this