Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
wemgl committed Oct 7, 2022
0 parents commit 50e8510
Show file tree
Hide file tree
Showing 7 changed files with 451 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/target
.idea
*.iml
.DS_Store
146 changes: 146 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 16 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
[package]
name = "instamark"
version = "1.0.0"
authors = ["Wembley Leach <[email protected]>"]
edition = "2021"
license = "MIT"
description = "Converts the Instapaper CSV export into the Netscape Bookmark file format"
homepage = "https://github.com/wemgl/instamark"
readme = "README.md"
keywords = ["instapaper", "bookmark", "html", "netscape", "cli"]
categories = ["command-line-utilities"]

[dependencies]
csv = "1.1.6"
itertools = "0.10.5"
serde = { version = "1.0.145", features = ["derive"] }
21 changes: 21 additions & 0 deletions LICENSE.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2022 Wembley G. Leach, Jr.

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
25 changes: 25 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Instamark
Converts an Instapaper CSV export of bookmarks into the Netscape Bookmark file format.

## Usage
Login to your Instapaper account on a desktop computer (the CSV export functionality isn't available
on mobile). Click your username in the top-right hand corner of the screen, then select settings.
Select "Download .CSV file" in the Export section at the bottom of the page. Navigate to the location
the export was saved to in a terminal window and then run:

```shell
instamark instapaper-export.csv > instapaper-bookmarks.html
```

You exported Instapaper data is now saved in the `instapaper-bookmarks.html` file, which you can
import into Safari, Chrome, Firefox, or any other browser which supports the Netscape bookmark file
format.

## Installation
This GitHub repository contains executable binaries for Linux and macOS in the Release
section for download.

Additionally, download the source code from this repository, navigate to the root directory, and run
```shell
cargo build --release
```
72 changes: 72 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
mod netscape_bookmark;

use std::{env, io, panic, path, process};
use std::collections::HashMap;
use std::error::Error;
use std::fs::File;
use std::io::Write;

use itertools::Itertools;
use netscape_bookmark::Bookmark;
use crate::netscape_bookmark::NetscapeBookmarks;

fn main() {
let args: Vec<String> = env::args().collect();
if args.len() != 2 {
print_usage();
process::exit(0);
}

panic::set_hook(Box::new(|_| {}));

if let Err(e) = run(args) {
eprintln!("{}", e.to_string().to_lowercase().replace("\"", ""));
print_usage();
process::exit(1);
}
}

fn print_usage() {
println!("usage: instamark export.csv");
}

fn run(args: Vec<String>) -> Result<(), Box<dyn Error>> {
let csv_export = args.into_iter().last().unwrap_or_else(|| String::new());
if csv_export.is_empty() {
return Err("no Instapaper export provided".into());
}

let file_path = File::open(path::Path::new(&csv_export))?;
let mut csv_file = csv::Reader::from_reader(file_path);
let folders = csv_file.deserialize()
.filter_map(|record| record.ok())
.collect::<Vec<Bookmark>>()
.into_iter()
.group_by(|bookmark| bookmark.folder.clone())
.into_iter()
.fold(HashMap::<String, Vec<Bookmark>>::new(), |mut folders, group| {
let (folder, items) = group;
if !folders.contains_key(&folder) {
folders.insert(folder, items.collect());
} else {
folders.get_mut(&folder).and_then(|group| {
group.extend(items);
Some(group)
});
}
folders
});

let bookmarks_html = NetscapeBookmarks::new()
.doctype()
.title("Instapaper")
.header("Instapaper")
.description_lists(folders)
.render();

let mut stdout = io::stdout().lock();
stdout.write_all(bookmarks_html.as_bytes())?;
stdout.flush()?;

Ok(())
}
Loading

0 comments on commit 50e8510

Please sign in to comment.