parsehtmlheadinfo

package module
v1.0.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 25, 2024 License: MIT Imports: 10 Imported by: 0

README

Parse Html Head Info

This is a simple widget that just gets the basic information inside the head tag of the target website

Usage

Start using it

Download and install it:

go get github.com/gotoobe/parse-html-head-info

Import it in your code:

import ParseSite "github.com/gotoobe/parse-html-head-info"

Example

Here's a simple example as follows:

package main

import (
	"github.com/gin-contrib/cors"
	"github.com/gin-gonic/gin"
	ParseSite "github.com/gotoobe/parse-html-head-info"
	"log"
	"net/http"
)

func main() {
	server := gin.Default()

	server.Use(cors.Default())

	server.GET("/", func(c *gin.Context) {
		c.String(http.StatusOK, "Hi~")
	})
	siteUrl := "https://www.example.com/"
	parseSite := ParseSite.ParseInfoConfig{
		URL:     siteUrl,
		Timeout: 5000,
		//ProxyAddress: "http://127.0.0.1:7890",
	}
	server.GET("/site-info", func(c *gin.Context) {
		siteInfo, err := parseSite.GetSiteHeadInfo()
		if err != nil {
			c.JSON(http.StatusOK, gin.H{
				"code":    http.StatusRequestTimeout,
				"message": "timeout",
				"data":    nil,
			})
			log.Printf("Error: %s", err)
		} else {
			c.JSON(http.StatusOK, gin.H{
				"code":    http.StatusOK,
				"message": "ok",
				"data": gin.H{
					"title":           siteInfo.Title,
					"description":     siteInfo.Description,
					"keywords":        siteInfo.Keywords,
					"iconUrl":         siteInfo.IconUrl,
					"requestHtmlCost": siteInfo.RequestSiteCost,
				}})
		}
	})
	server.GET("/ping", func(c *gin.Context) {
		c.JSON(http.StatusOK, gin.H{
			"message": "pong",
		})
	})
	log.Fatalln(server.Run(":8788"))
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ParseInfoConfig

type ParseInfoConfig struct {
	// website url
	URL string
	// timeout in ms
	Timeout      time.Duration
	ProxyAddress string
	// same as *tls.Config
	TLSClientConfig *tls.Config
	OnlyBasicInfo   bool
}

ParseInfoConfig contains options for getting information within the head tag of the target site

func (*ParseInfoConfig) GetSiteHeadInfo

func (info *ParseInfoConfig) GetSiteHeadInfo() (SiteInfo, error)

GetSiteHeadInfo Get information about the head of the website

type SiteInfo

type SiteInfo struct {
	Title, Description, Keywords, IconUrl, RequestSiteCost string
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL