Giter Site home page Giter Site logo

opengraph's Introduction

Open Graph Parser for Golang

Go implementation of https://ogp.me/

reference Go codecov Maintainability Go Report Card License: MIT FOSSA Status GitHub tag (latest SemVer)

Code Example

package main

import (
	"fmt"
	"github.com/otiai10/opengraph/v2"
)

func main() {
	ogp, err := opengraph.Fetch("https://github.com/")
	fmt.Println(ogp, err)
}

You can try CLI as a working example

% go get github.com/otiai10/opengraph/ogp
% ogp --help
% ogp -A otiai10.com

Just for fun ๐Ÿ˜‰

Advanced usage

Set an option for fetching:

intent := opengraph.Intent{
	Context:     ctx,
	HTTPClient:  client,
	Strict:      true,
	TrustedTags: []string{"meta", "title"},
}
ogp, err := opengraph.Fetch("https://ogp.me", intent)

Use any io.Reader as a data source:

f, _ := os.Open("my_test.html")
defer f.Close()
ogp := &opengraph.OpenGraph{}
err := ogp.Parse(f)

or if you already have parsed *html.Node:

err := ogp.Walk(node)

Do you wanna make absolute URLs?:

ogp.Image[0].URL // /logo.png
ogp.ToAbs()
ogp.Image[0].URL // https://ogp.me/logo.png

Issues

opengraph's People

Contributors

aywa avatar fossabot avatar go-u avatar kaboc avatar kainosnoema avatar mbleigh avatar otiai10 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

opengraph's Issues

Only parse meta tags?

@mbleigh @otiai10
Hey, I went through the implementation and seems like the fetch method parses title meta and link elements to create the opengraph object. Any way I could just check for meta tags?

opengraph.Fetch returns nothing for a few domains

I have been using this package to fetch opengraph info about websites and articles, but for a few websites, e.g. FastCompany, the Fetch() method returns nothing. After some research, I found that few websites block bots from scraping their content. However, when I try Raycast preview, or even macOS preview, it successfully fetches the metadata with the image and title. How can I achieve that? Here's how my code looks:

package api

import (
	"net/http"
	"mypackage/read-it-later/structs"
	"mypackage/read-it-later/utils"

	"github.com/gin-gonic/gin"
	"github.com/oklog/ulid/v2"
	"github.com/otiai10/opengraph"
)

func StoreEntity(c *gin.Context) {
	var requestBody structs.RequestURL
	if err := c.BindJSON(&requestBody); err != nil {
		return
	}

	c.Header("User-Agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/116.0.0.0 Safari/537.36")

	ogp, err := opengraph.Fetch(requestBody.URL)

	if err != nil {
		c.JSON(http.StatusInternalServerError, err)
	}

	c.JSON(http.StatusOK, gin.H{
		"id":          ulid.Make(),
		"description": ogp.Description,
		"favicon":     ogp.Favicon,
		"image":       utils.FetchImageURL(ogp.Image, ogp.Favicon, ogp.URL.Host, ogp.URL.Scheme),
		"siteName":    ogp.SiteName,
		"title":       ogp.Title,
		"type":        ogp.Type,
		"URL":         ogp.URL,
		"all_info":    ogp,
	})
}

Retrieval of Twitter card information

Hi, I was just wondering if it's possible to retrieve Twitter card information such as in the following example html, or if it's a feature you would consider adding (or allow me to submit a PR for)?
Thanks

<meta name="twitter:card" content="summary"><meta name="twitter:title" content="">
<meta name="twitter:description" content="All Genre Music Party">

Expose Expose function

Sometimes I want to just call the walk function when I already parsed the html. But it is not possible for now.

It would be good if Walk is expose

[Feature Request] User-Agent Option

Hello OCHIAI,
@otiai10

Thank you for very nice app. This post is about feature request and who will code it ?

[ Why User-Agent Option ? ]
Some web sites are rendering OGP Tags Dynamically with javascript.
But javascript don't work in simple crawler like twitter bot or this golang app.

So, many web sites are rendering OGP Meta tag on server side.
Popular way is SSR(Node.js), But more easier way is "Prerendering with Netlify"

But Netlify check User-Agent for prerendering. So this app can't get OGP meta tags.
[Common Issue] Understanding and debugging prerendering
(I think potentially other sites check User-Agent too, for reduce rendering cost)

[ Who code it ? ]
I'm not professional. But If you are wellcome pull request about this feature, I want to write code.
But if you don't want to merge this feature or you want to code your ownself, I don't send pull request.
So, please tell me your feelings.

Thanks,

og.url is not mapped

@otiai10
In the current implementation, opengraph.URL defaults to the parsed request URL and doesn't maps the og.url meta tag.
Is it okay to add a new field called Value in opengraph.URL struct which maps the og.url meta tag value?

Or if I am missing something here, please let me know. I want fetch the og.url meta tag from the parsed HTML.
Thank you.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.