Projects STRLCPY wb Commits adcfb56b
🤬
  • ■ ■ ■ ■ ■ ■
    .gitignore
     1 +wb
     2 +.idea/*
  • ■ ■ ■ ■ ■ ■
    README.md
     1 +<h1>wb</h1>
     2 +<p> A wizard who goes back and brings old files!</p>
     3 +<p>
     4 + <a href="https://opensource.org/licenses/MIT">
     5 + <img src="https://img.shields.io/badge/license-MIT-_red.svg">
     6 + </a>
     7 + <a href="https://goreportcard.com/badge/github.com/riza/wb">
     8 + <img src="https://goreportcard.com/badge/github.com/riza/wb">
     9 + </a>
     10 + <a href="https://github.com/riza/wb/releases">
     11 + <img src="https://img.shields.io/github/release/riza/wb">
     12 + </a>
     13 + <a href="https://twitter.com/rizasabuncu">
     14 + <img src="https://img.shields.io/twitter/follow/rizasabuncu.svg?logo=twitter">
     15 + </a>
     16 +</p>
     17 + 
     18 +# Installation
     19 + 
     20 +linx requires **go1.17** to install successfully. Run the following command to get the repo -
     21 + 
     22 +```sh
     23 +go install github.com/riza/wb@latest
     24 +```
     25 + 
     26 +# Usage
     27 + 
     28 +```sh
     29 +echo "https://akamai.airbnb.com/robots.txt"|wb
     30 +```
     31 + 
     32 +# TODOs
     33 + 
     34 +* [ ] Timestamp selection
     35 + 
     36 + 
  • ■ ■ ■ ■ ■
    go.mod
     1 +module github.com/riza/wb
  • ■ ■ ■ ■ ■ ■
    main.go
     1 +package main
     2 + 
     3 +import (
     4 + "bufio"
     5 + "encoding/json"
     6 + "errors"
     7 + "flag"
     8 + "fmt"
     9 + "io"
     10 + "net/http"
     11 + "os"
     12 + "time"
     13 +)
     14 + 
     15 +const (
     16 + wbSnapshotApiURL = "https://web.archive.org/cdx/search/xd?output=json&url=%s&fl=timestamp,original&collapse=digest&gzip=false&filter=statuscode:200"
     17 + wbFileURL = "https://web.archive.org/web/%sid_/%s"
     18 +)
     19 + 
     20 +func main() {
     21 + var url string
     22 + 
     23 + flag.Parse()
     24 + 
     25 + if flag.NArg() > 0 {
     26 + url = flag.Arg(0)
     27 + } else {
     28 + sc := bufio.NewScanner(os.Stdin)
     29 + for sc.Scan() {
     30 + url = sc.Text()
     31 + }
     32 + 
     33 + if err := sc.Err(); err != nil {
     34 + fmt.Fprintf(os.Stderr, "failed to read input: %s\n", err)
     35 + }
     36 + }
     37 + 
     38 + client := http.Client{
     39 + Timeout: time.Second * 5,
     40 + }
     41 + 
     42 + snapshots, err := getSnapshots(client, url)
     43 + if err != nil {
     44 + fmt.Fprintf(os.Stderr, "failed to getting snapshots %s\n", err)
     45 + }
     46 + 
     47 + lastSnapshot := snapshots[len(snapshots)-1]
     48 + lastSnapshotTs := lastSnapshot[0]
     49 + lastSnapshotURL := lastSnapshot[1]
     50 + 
     51 + request, err := http.NewRequest("GET", fmt.Sprintf(wbFileURL, lastSnapshotTs, lastSnapshotURL), nil)
     52 + if err != nil {
     53 + fmt.Fprintf(os.Stderr, "failed to generate request waybackmachine api: %s\n", err)
     54 + }
     55 + 
     56 + request.Header.Add("Accept-Encoding", "plain")
     57 + 
     58 + response, err := client.Do(request)
     59 + if err != nil {
     60 + fmt.Fprintf(os.Stderr, "failed to send request waybackmachine api: %s\n", err)
     61 + }
     62 + 
     63 + defer response.Body.Close()
     64 + 
     65 + io.Copy(os.Stdout, response.Body)
     66 +}
     67 + 
     68 +func getSnapshots(c http.Client, url string) ([][]string, error) {
     69 + request, err := http.NewRequest("GET", fmt.Sprintf(wbSnapshotApiURL, url), nil)
     70 + if err != nil {
     71 + return [][]string{}, fmt.Errorf("getSnapshots: failed to generate request waybackmachine api: %w", err)
     72 + }
     73 + 
     74 + response, err := c.Do(request)
     75 + if err != nil {
     76 + return [][]string{}, fmt.Errorf("getSnapshots: failed to send request waybackmachine api: %w", err)
     77 + }
     78 + defer response.Body.Close()
     79 + 
     80 + var r [][]string
     81 + dec := json.NewDecoder(response.Body)
     82 + 
     83 + err = dec.Decode(&r)
     84 + if err != nil {
     85 + return [][]string{}, fmt.Errorf("getSnapshots: error while decoding response %w", err)
     86 + }
     87 + 
     88 + if len(r) < 1 {
     89 + return [][]string{}, errors.New("getSnapshots: no results found for this url")
     90 + }
     91 + 
     92 + return r[1:], nil
     93 +}
     94 + 
Please wait...
Page is in error, reload to recover