![image](https://private-user-images.githubusercontent.com/5696002/311062725-9ac04e28-cbab-4803-ac34-90a73df300b8.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MjMzMzQ0MTgsIm5iZiI6MTcyMzMzNDExOCwicGF0aCI6Ii81Njk2MDAyLzMxMTA2MjcyNS05YWMwNGUyOC1jYmFiLTQ4MDMtYWMzNC05MGE3M2RmMzAwYjgucG5nP1gtQW16LUFsZ29yaXRobT1BV1M0LUhNQUMtU0hBMjU2JlgtQW16LUNyZWRlbnRpYWw9QUtJQVZDT0RZTFNBNTNQUUs0WkElMkYyMDI0MDgxMCUyRnVzLWVhc3QtMSUyRnMzJTJGYXdzNF9yZXF1ZXN0JlgtQW16LURhdGU9MjAyNDA4MTBUMjM1NTE4WiZYLUFtei1FeHBpcmVzPTMwMCZYLUFtei1TaWduYXR1cmU9NDY2NDkwNGJiMmJmNGE5MTYzMjE0MjZiMmQxMTQ0MWZkNWRlYzAyMjMxN2NkZTk2MTJlNDdmMzYyNjM2YzhhZiZYLUFtei1TaWduZWRIZWFkZXJzPWhvc3QmYWN0b3JfaWQ9MCZrZXlfaWQ9MCZyZXBvX2lkPTAifQ.AHmIum6iFgB_jQlg6BpxLcePTSU5v25uDSmvbrHQOxU)
Our goal was to rename PDF files stored in the ~/Library/Mobile Documents/iCloud~com~apple~iBooks/Documents/
directory. Each file was named with its unique arXiv ID, but we aimed to rename each file using its corresponding arXiv paper title for straightforward identification.
- We developed a Python function
rename_pdfs()
, which:- Parses a list of file paths.
- Retrieves the arXiv IDs from the names of the files.
- Sends a HTTP request to the arXiv API to get the title of the paper associated with each arXiv ID.
- Renames each file with its paper title within the same directory.
- We introduced checks and error-handling to ensure smooth execution even when a file doesnt exist, the arXiv title is unavailable, or renaming fails.
We accomplished this by sending an HTTP GET request to root_url + id_list= + arxiv_id
. The root_url
was the arXiv API URL, and our arXiv ID was appended to it. The request response held the metadata for the respective paper, which included the paper title. We extracted this title for renaming our PDF files.
We passed all our PDF files within the ~/Library/Mobile Documents/iCloud~com~apple~iBooks/Documents/
directory to rename_pdfs()
. Among these files, we successfully renamed those with a valid arXiv ID, and ignored those with no valid arXiv ID or whose arXiv ID didnt have an associated title.
Our approach enables automatic and consistent naming of files, making it highly adaptable for similar future tasks.