main.go

· kitten's pastes · raw

expires: 08 Aug, 2024

  1package main
  2
  3import (
  4	"fmt"
  5	"log"
  6	"os"
  7	"time"
  8	"path/filepath"
  9
 10	"golang.org/x/net/html"
 11)
 12
 13var (
 14	tz *time.Location
 15	dir string
 16)
 17
 18type img struct {
 19	p string
 20	t time.Time
 21}
 22
 23func init() {
 24	l, err := time.LoadLocation("Asia/Riyadh")
 25	if err != nil {
 26		log.Fatal(err)
 27	}
 28	tz = l
 29}
 30
 31func findLink(n *html.Node) string {
 32	for c := n.FirstChild; c != nil; c = c.NextSibling {
 33		if c.Type == html.ElementNode && c.Data == "a" {
 34			for _, a := range c.Attr {
 35				if a.Key == "href" {
 36					return a.Val
 37				}
 38			}
 39		}
 40	}
 41	return ""
 42}
 43
 44func findTime(n *html.Node) time.Time {
 45	for c := n.FirstChild; c != nil; c = c.NextSibling {
 46		if c.Type == html.ElementNode && c.Data == "a" {
 47			for tc := c.FirstChild; tc != nil; tc = tc.NextSibling {
 48				if tc.Type == html.TextNode {
 49					t, err := time.ParseInLocation("Jan 2, 2006, 3:04 PM", tc.Data, tz)
 50					if err != nil {
 51						log.Fatal(err)
 52					}
 53					return t
 54				}
 55			}
 56		}
 57	}
 58	return time.Time{}
 59}
 60
 61func findImg(n *html.Node) img {
 62	var l string
 63	var t time.Time
 64
 65	for c := n.FirstChild; c != nil; c = c.NextSibling {
 66		for _, a := range c.Attr {
 67			if a.Key != "class" {
 68				continue
 69			}
 70
 71			switch a.Val {
 72			case "_3-96 _2let":
 73				l = filepath.Join(dir, findLink(c))
 74			case "_3-94 _2lem":
 75				t = findTime(c)
 76			}
 77
 78			if !t.IsZero() && l != "" {
 79				return img{p: l, t: t}
 80			}
 81		}
 82	}
 83
 84	panic("unreachable")
 85}
 86
 87func findImgs(n *html.Node) (is []img) {
 88	for c := n.FirstChild; c != nil; c = c.NextSibling {
 89		if c.Type != html.ElementNode || c.Data != "div" {
 90			continue
 91		}
 92
 93		for _, a := range c.Attr {
 94			if a.Key == "class" && a.Val == "pam _3-95 _2pi0 _2lej uiBoxWhite noborder" {
 95				i := findImg(c)
 96				is = append(is, i)
 97			}
 98		}
 99	}
100	return
101}
102
103func findMain(n *html.Node) *html.Node {
104	if n.Type == html.ElementNode {
105		f := false
106		for _, a := range n.Attr {
107			if a.Key == "class" && a.Val == "_4t5n" {
108				f = true
109			}
110		}
111		if f {
112			return n
113		}
114	}
115
116	for c := n.FirstChild; c != nil; c = c.NextSibling {
117		main := findMain(c)
118		if main != nil {
119			return main
120		}
121	}
122	return nil
123}
124
125func main() {
126	if len(os.Args) != 3 {
127		fmt.Fprintf(os.Stderr, "usage: %s dir album.html\n", os.Args[0])
128		os.Exit(1)
129	}
130	dir = os.Args[1]
131
132	f, err := os.Open(os.Args[2])
133	if err != nil {
134		log.Fatal(err)
135	}
136
137	doc, err := html.Parse(f)
138	if err != nil {
139		log.Fatal(err)
140	}
141
142	m := findMain(doc)
143	is := findImgs(m)
144
145	for _, i := range is {
146		fmt.Println(i.p, i.t.Format(time.DateTime))
147	}
148}