Skip to content

Instantly share code, notes, and snippets.

@wangsy
Last active June 14, 2024 07:10
Show Gist options
  • Save wangsy/68fd61d0c505bfa98501 to your computer and use it in GitHub Desktop.
Save wangsy/68fd61d0c505bfa98501 to your computer and use it in GitHub Desktop.
one drive file name Unicode NFD fix
require 'rest-client'
require 'awesome_print'
# get token from
# https://dev.onedrive.com/auth/msa_oauth.htm
header = "bearer EwBwAq1DBAAUGCCXc8wU/zFu9QnLdZXy+YnElFkAAe2M7OiS2LTXw28pefcVeF/lD7lbUyoJWhgVcWoI5ohI304OPVW3u1QXZOx9xQpGe6B7OHGdg3BPsMntYjh6QX4/Faf4EOxr2HdkxiVhh823e1IsxmLg+2oZN6U0sQIDzAuu4kZVgiFfKcKnNn0ilfl71NvNuX2WXQ7jOM8n4G9B1OkAoL0UvbyR96nstGdB98BHB+VhXtuv/ZABZpnOh1gH94YCzdCoE3jRwuvL+oHU58xQAhcnHVhz0xamaeipek2reTIba5784dEn2STgPEawbQTFBmaFoQKs/TWc33IeMj96uitZKcZLijctxk8tyG2ZFidPlTA945jLV4MqTysDZgAACOuffgkcouWsQAFXcrUgo6fLwCK7I4G7bgDjW1t5oT42ve4XveA0IbsGwyS78/LVuNjQk6kPulxaErj7mN9B/1znpUc5ggQr/sY86RgQvKJUwgE7vQg1E4vNsubHKoDWF9tdQZmOXw0NS/JVLPEE0g9NOsIeG5qlcz234fg+v92BXSmkTA6bVphIAXxG2KsZlnKOH8XwrZCEJSL+gvDaXeQ+VS8Omcv39Aws+U0TTQkAfUmc+KZ9o/1oF1cjJR4pa+cEqM4xJd/8VhZBTTwJx5R33x7jpHCOUFRH91o3IRq28I8qAT01UcE2eLbKXAvgl1w7Kh6IepqeIw65Wu2vfkPhZboXQiRPf2+pcLostsbsT6qKaRnXXYROyLMI86Q/Rbf8XnM8VFIVDcOEaRvaafHwhRAapF1HQU2y4mMo3wjzeRSEk41j9UZZd18B"
pass = {content_type: "application/json", accept: :json, authorization: header }
host = "https://api.onedrive.com/v1.0"
url = "/drive"
response = RestClient.get "#{host}#{url}", pass
drive_id = JSON(response)["id"]
p "drive_id : #{drive_id}"
folder_ids = []
url = "/drives/#{drive_id}/root/children"
response = RestClient.get "#{host}#{url}", pass
JSON(response)["value"].each do |item|
if item["folder"] != nil
folder_ids << item["id"]
end
end
while folder_ids.count > 0
item_id = folder_ids.shift
url = "/drive/items/#{item_id}/children"
begin
response = RestClient.get "#{host}#{url}", pass
rescue Exception => e
puts e.message
next
end
JSON(response)["value"].each do |item|
if item["name"] != item["name"].unicode_normalize
p "chaging #{item["name"]}"
url = "/drive/items/#{item["id"]}"
begin
response = RestClient.patch "#{host}#{url}", {name: item["name"].unicode_normalize}.to_json, pass
rescue Exception => e
puts e.message
next
end
end
if item["folder"] != nil
folder_ids << item["id"]
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment