Skip to content

Instantly share code, notes, and snippets.

@janithl
Last active October 5, 2018 06:12
Show Gist options
  • Save janithl/7a047e524bfd5abc0900593be35434de to your computer and use it in GitHub Desktop.
Save janithl/7a047e524bfd5abc0900593be35434de to your computer and use it in GitHub Desktop.
A simple Go utility for work, where we need to get the version of a site through its generator meta tag
package main
import (
"bufio"
"fmt"
"io/ioutil"
"net/http"
"os"
"regexp"
"strconv"
)
// Site type holds information about sites
type Site struct {
name, url string
}
var sites = []Site{
{"Example 1", "example-site.com"},
{"Example 2", "example-site-2.com"},
{"Example 3", "example-site-3.com"},
}
// Parser type holds information about the parsing
type Parser struct {
url, body string
}
func (p *Parser) setURL(url string) {
p.url = url
p.body = ""
}
func (p *Parser) getMatches(regexString string, index int) string {
regex, _ := regexp.Compile(regexString)
matches := regex.FindStringSubmatch(p.body)
if len(matches) == index+1 {
return matches[index]
}
return "N/A"
}
func (p *Parser) getPage() {
if len(p.url) > 5 && p.url[:4] != "http" {
p.url = "http://" + p.url
}
res, err := http.Get(p.url)
if err != nil {
fmt.Println("\nError: Could not fetch " + p.url)
return
}
body, err := ioutil.ReadAll(res.Body)
res.Body.Close()
if err != nil {
fmt.Println("\nError: Could not parse page")
fmt.Println(err)
return
}
p.body = string(body)
title := p.getMatches("(<title>)([^<]+)", 2)
version := p.getMatches("(<meta name=\"generator\" content=\")([^\"]+)", 2)
fmt.Printf("\n%10s : %s\n", "URL", p.url)
fmt.Printf("%10s : %s\n", "Title", title)
fmt.Printf("%10s : %s\n\n", "Version", version)
}
func getInput() {
reader := bufio.NewReader(os.Stdin)
parser := Parser{}
for {
for index, site := range sites {
fmt.Printf("[%02d] %-18s ", index, site.name)
if index%3 == 2 {
fmt.Println("")
}
}
fmt.Print("\n\nEnter selection, a URL, or q/quit/exit to quit: ")
userInput, _ := reader.ReadString('\n')
if userInput == "exit\n" || userInput == "quit\n" || userInput == "q\n" {
return
}
userInput = userInput[:len(userInput)-1]
if num, err := strconv.Atoi(userInput); err == nil && num < len(sites) {
parser.setURL(sites[num].url)
parser.getPage()
} else if userInput != "" {
parser.setURL(userInput)
parser.getPage()
}
}
}
func main() {
if len(os.Args) > 1 {
parser := Parser{url: os.Args[1]}
parser.getPage()
} else {
getInput()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment