Git Product home page Git Product logo

opengraph's Introduction

A Swift wrapper for Open Graph protocol (OGP)

Build Status Carthage compatible Platform
OpenGraph is a Swift wrapper for OGP (Open Graph protocol). You can fetch OpenGraph and get access to the attributes using subscript and enum cases as follows.

OpenGraph.fetch(url: url) { result in
    switch result {
    case .success(let og):
        print(og[.title]) // => og:title of the web site
        print(og[.type])  // => og:type of the web site
        print(og[.image]) // => og:image of the web site
        print(og[.url])   // => og:url of the web site
    case .failure(let error):
        print(error)
    }
}

For macOS 10.15, iOS 13, watchOS 6, tvOS 13, and above, you can use the async/await syntax:

do {
    let og = try await OpenGraph.fetch(url: url)
    print(og[.title]) // => og:title of the web site
    print(og[.type])  // => og:type of the web site
    print(og[.image]) // => og:image of the web site
    print(og[.url])   // => og:url of the web site
} catch {
    print(error)
}

All metadatas are defined here.
This library doesn't provide any platform specific views to display OGP data for high portability.

Furthermore, please copy the extension below to your own project if you want to use this library with the Rx interface.

extension Reactive where Base: OpenGraph {
    static func fetch(url: URL?) -> Observable<OpenGraph> {
        return Observable.create { observer in
            guard let url = url else {
                observer.onCompleted()
                return Disposables.create()
            }

            OpenGraph.fetch(url: url) { result in
                switch result {
                case .success(let og):
                    observer.onNext(og)
                case .failure(let error):
                    observer.onError(error)
                }
                observer.onCompleted()
            }

            return Disposables.create()
        }
    }
}

Requirements

  • Xcode 11.x / Swift 5.x (If you use Xcode 10.x, you can use 1.1.0.)
  • iOS 10.0 or later
  • macOS 10.9 or later
  • tvOS 9.0 or later
  • watchOS 2.0 or later

If you use Swift 2.2 or 2.3, use older version of OpenGraph.

Installation

CocoaPods

Insert pod 'OpenGraph' to your Podfile and run pod install.

Carthage

Insert github "satoshi-takano/OpenGraph" to your Cartfile and run carthage update.

How to handle redirections

In most cases, the OpenGraph can handle server-side redirections automatically. But some web pages provoke redirections on their front-end JavaScript. You might be able to handle these kind of redirections by changing the User-Agent as follows.
satoshi-takano#43

License

This library is under the MIT License.

opengraph's People

Contributors

akadateppei avatar cozzin avatar fumiki avatar hstdt avatar jsryudev avatar khanlou avatar mt-hodaka avatar p-x9 avatar puyanlin avatar robbiet480 avatar rudrankriyam avatar satoshi-takano avatar wendellli99 avatar

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.