Created
January 2, 2017 11:17
-
-
Save tmthrgd/77765c7c7e7782af768c72dc166d7f7e to your computer and use it in GitHub Desktop.
Sorts a list of links into Instagram, Tumblr and Facebook links.
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
/* | |
(c) Tom Thorogood 2016 | |
https://tomthorogood.co.uk | |
Copyright 2016 Tom Thorogood. All rights reserved. | |
Use of this source code is governed by a | |
Modified BSD License license that can be found in | |
the LICENSE file. | |
*/ | |
package main | |
import ( | |
"bufio" | |
"flag" | |
"fmt" | |
"net/url" | |
"os" | |
"strings" | |
) | |
func isInstagram(u *url.URL) bool { | |
return u.Host == "instagram.com" || u.Host == "www.instagram.com" | |
} | |
func isTumblr(u *url.URL) bool { | |
return strings.HasSuffix(u.Host, ".tumblr.com") || strings.HasPrefix(u.Path, "/post/") | |
} | |
func isFacebook(u *url.URL) bool { | |
return u.Host == "facebook.com" || u.Host == "www.facebook.com" || u.Host == "m.facebook.com" | |
} | |
func main() { | |
var inPath string | |
flag.StringVar(&inPath, "in", "-", "the path to read from") | |
var instaPath string | |
flag.StringVar(&instaPath, "insta", "instagram", "the path to write instagram links to") | |
var tumblrPath string | |
flag.StringVar(&tumblrPath, "tumblr", "tumblr", "the path to write tumblr links to") | |
var facebookPath string | |
flag.StringVar(&facebookPath, "facebook", "facebook", "the path to write facebook links to") | |
var otherPath string | |
flag.StringVar(&otherPath, "other", "other", "the path to write other links to") | |
flag.Parse() | |
var inF *os.File | |
if inPath == "-" { | |
inF = os.Stdin | |
} else { | |
var err error | |
if inF, err = os.Open(inPath); err != nil { | |
panic(err) | |
} | |
defer inF.Close() | |
} | |
instaF, err := os.Create(instaPath) | |
if err != nil { | |
panic(err) | |
} | |
defer instaF.Close() | |
tumblrF, err := os.Create(tumblrPath) | |
if err != nil { | |
panic(err) | |
} | |
defer tumblrF.Close() | |
facebookF, err := os.Create(facebookPath) | |
if err != nil { | |
panic(err) | |
} | |
defer facebookF.Close() | |
otherF, err := os.Create(otherPath) | |
if err != nil { | |
panic(err) | |
} | |
defer otherF.Close() | |
scan := bufio.NewScanner(inF) | |
for scan.Scan() { | |
if len(scan.Bytes()) == 0 { | |
continue | |
} | |
u, err := url.Parse(scan.Text()) | |
if err != nil { | |
fmt.Fprintf(os.Stderr, "%v: %s", err, scan.Text()) | |
continue | |
} | |
switch { | |
case isInstagram(u): | |
_, err = fmt.Fprintln(instaF, scan.Text()) | |
case isTumblr(u): | |
_, err = fmt.Fprintln(tumblrF, scan.Text()) | |
case isFacebook(u): | |
_, err = fmt.Fprintln(facebookF, scan.Text()) | |
default: | |
_, err = fmt.Fprintln(otherF, scan.Text()) | |
} | |
if err != nil { | |
panic(err) | |
} | |
} | |
if err := scan.Err(); err != nil { | |
panic(err) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment