Giter Club home page Giter Club logo

link-preview's People

Contributors

benwilkins avatar dusterio avatar edueo avatar ejunker avatar gcalmels avatar j3j5 avatar jdrieghe avatar kasp3r avatar service-paradis 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  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  avatar

link-preview's Issues

Class not found

Hello,

i try to use it in blade and i added app.php as alias

'LinkPreview' => Dusterio\LinkPreview\Client::class,

and in the blade file

$linkPreview = new LinkPreview($link);

But i get

ErrorException
Class 'Dusterio\LinkPreview\Client' not found

Setting a user agent, how to?

Hi,

Any way to set a custom user agent?
I see the HttpReader can take a config array, but I need to override that from the Client()

Any ideas?

Class not found on Laravel 7

Hi,

I have installed plugin via composer and try to use it on Laravel 7, but I get class not found error if I using example code:

"Class 'Dusterio\LinkPreview\Client' not found"

I have checked other issues and followed the suggestion in the comment:
#22 (comment)

$client = new \Dusterio\LinkPreview\LinkPreview($url);

Then I got another class not found error:

"Class 'Dusterio\LinkPreview\LinkPreview' not found"

I have tried to clear cache and etc but no luck.
Any help much appreciated. Thanks

Throwing 500 Error for some specific urls

For the following link 500 error thrown.
https://www.revolution.watch/introducing-the-audemars-piguet-remaster-01/

But almost other links works fine. No issues. Can anyone help?

From this issue:
guzzle/guzzle#1841 (comment)

$guzzleResult->getBody()

GuzzleHttp\Psr7\Stream {#4825 ▼
  -stream: stream resource @2055 ▼
    wrapper_type: "PHP"
    stream_type: "TEMP"
    mode: "w+b"
    unread_bytes: 0
    seekable: true
    uri: "php://temp"
    options: []
  }
  -size: 3391
  -seekable: true
  -readable: true
  -writable: true
  -uri: "php://temp"
  -customMetadata: []
}

Can anyone help? Thanks

`getPreviews()` fails with a YouTube or Vimeo URL

When trying to get all previews for a YouTube or Vimeo URL, the request fails. It appears that the error is in Guzzle rather than your script, but perhaps there should be a way in the canParseLink method of the HtmlParser to check to see if it's a YouTube or Vimeo request first before trying to parse it.

Steps to reproduce (in Laravel):

Preview::setUrl('https://www.youtube.com/watch?v=mjMZwaqGPrc');
Preview::getPreviews(); // <-- script just dies without throwing exception.

Tracing this, I found that it is the HtmlParser that is causing the script to fail. Particularly, Guzzle dies when trying to make the request in HttpReader:93. The same thing happens with a Vimeo URL.

Symfony 4 and 5 compatibility

Hi,

I cannot used your library with Symfony > 3.4 (4 and 5) because dom-crawler and css-selector are in ~3.0...
Would it be possible to update these dependencies ?

Thanks !

Unexpected cover and images value in response

Thanks in advance for this nice plugin.

$url = 'https://http.cat/404.jpg';
$linkPreview   = new Client($url);
$parsedGeneral = $linkPreview->getPreview('general')->toArray();

Following is the response I get:

array:7 [▼
  "cover" => GuzzleHttp\Psr7\Uri {#2588 ▼
    -scheme: "https"
    -userInfo: ""
    -host: "http.cat"
    -port: null
    -path: "/404.jpg"
    -query: ""
    -fragment: ""
  }
  "images" => array:1 [▼
    0 => GuzzleHttp\Psr7\Uri {#2588 ▶}
  ]
  "title" => null
  "description" => null
  "video" => null
  "videoType" => null
  "url" => "https://http.cat/404.jpg"
]

Expected response:
cover: expected null or string, but GuzzleHttp\Psr7\Uri returned
images: expected [] empty array, but array of GuzzleHttp\Psr7\Uri returned

Can we expect expected data type? Thanks

Fetching data from Facebook

Awesome library! I like the simplicity of it and the ability to modify it with my own parsers and what not.

I noticed that the default parser doesn't fetch data from facebook due to the meta tags using "name" instead of "content"

A quick fix:

protected function parseHtml($html)
{
    $data = [
        'image' => '',
        'title' => '',
        'description' => ''
    ];

    libxml_use_internal_errors(true);
    $doc = new \DOMDocument();
    $doc->loadHTML($html);

    /** @var \DOMElement $meta */
    foreach ($doc->getElementsByTagName('meta') as $meta) {
        if($meta->hasAttribute('name')){
            $prop = 'name';
        }else{
            $prop = 'property';
        }
        if ($meta->getAttribute($prop) === 'image') {
            $data['image'] = $meta->getAttribute('content');
        } elseif ($meta->getAttribute($prop) === 'og:image') {
            $data['image'] = $meta->getAttribute('content');
        } elseif ($meta->getAttribute($prop) === 'twitter:image') {
            $data['image'] = $meta->getAttribute('value');
        }

        if ($meta->getAttribute($prop) === 'name') {
            $data['title'] = $meta->getAttribute('content');
        } elseif ($meta->getAttribute($prop) === 'og:title') {
            $data['title'] = $meta->getAttribute('content');
        } elseif ($meta->getAttribute($prop) === 'twitter:title') {
            $data['title'] = $meta->getAttribute('value');
        }

        if ($meta->getAttribute($prop) === 'description') {
            $data['description'] = $meta->getAttribute('content');
        }else if ($meta->getAttribute($prop) === 'og:description') {
            $data['description'] = $meta->getAttribute('content');
        }
    }

    if (empty($data['title'])) {
        /** @var \DOMElement $title */
        foreach ($doc->getElementsByTagName('title') as $title) {
            $data['title'] = $title->nodeValue;
        }
    }

    return $data;
}

I double checked this against this Go script which does the same thing: https://github.com/badoux/goscraper/blob/master/goscraper.go

This also fixes an issue where the title is overwritten by the description because of a typo, and disables the need to loop through the meta tags again to find a description if the meta tag attribute is 'name' instead of 'property'.

Relative Images

URL: http://google.com

Response:

{
  "cover": "/images/branding/googleg/1x/googleg_standard_color_128dp.png",
  "images": [],
  "title": "Google",
  "description": "",
  "video": "",
  "videoType": ""
}

Code:

Preview::setUrl("http://google.com");
return Preview::getPreview('general')->toArray();

Some sites as demonstrated above give "strange" links, these are relative and prefixing with the url doesn't work either see:
http://google.com/images/branding/googleg/1x/googleg_standard_color_128dp.png

Is there any way to prevent this behaviour and make sure all links are absolute (since there is little to no use for having relative links with this library)

Guzzlehttp/guzzle ^7.0.0 compatibility

Hi,

I have problem running this package on Laravel 7 with guzzlehttp/guzzle 7.0.1. Could you please update it and make compatible with guzzlehttp/guzzle ^7.0.0. Thanks

Error:

- dusterio/link-preview v1.2.14 requires guzzlehttp/guzzle ^6.1 -> satisfiable by guzzlehttp/guzzle[6.1.0, 6.1.1, 6.2.0, 6.2.1, 6.2.2, 6.2.3, 6.3.0, 6.3.1, 6.3.2, 6.3.3, 6.4.0, 6.4.1, 6.5.0, 6.5.1, 6.5.2, 6.5.3, 6.5.4, 6.5.5, 6.5.x-dev]

Use of `trim()`

Hi,
What do you think about adding a trim() for the title and description attributes?
Sometimes the string parsed has some extra white spaces before and after...

Class 'DOMDocument' not found in symfony/dom-crawler/Crawler.php:192

Will try to look up what's happening in the next few days, but I did a composer update and now I get this:

Fatal error: Uncaught Error: Class 'DOMDocument' not found in vendor/symfony/dom-crawler/Crawler.php:192
Stack trace:
#0 vendor/symfony/dom-crawler/Crawler.php(170): Symfony\Component\DomCrawler\Crawler->addHtmlContent('<!doctype html>...', 'UTF-8')
#1 vendor/symfony/dom-crawler/Crawler.php(121): Symfony\Component\DomCrawler\Crawler->addContent('<!doctype html>...')
#2 vendor/symfony/dom-crawler/Crawler.php(70): Symfony\Component\DomCrawler\Crawler->add('<!doctype html>...')
#3 vendor/dusterio/link-preview/src/Parsers/HtmlParser.php(132): Symfony\Component\DomCrawler\Crawler->__construct('<!doctype html>...')
#4 vendor/dusterio/link-preview/src/Parsers/HtmlParser.php(100): Dusterio\LinkPreview\Parsers\HtmlParser->parseHtml(Object(Dusterio                                            \LinkPreview\Models\Link))
#5 vendor/dusterio/link-preview/src/Client.php(61):
Dusteri in vendor/symfony/dom-crawler/Crawler.php on line 192

Not sure why it says "Dusteri" that last time. :P

This documents not working!

I installed this library by composer in Laravel project.

I found the document is totally different and not working at all!

Error: Class 'Dusterio\LinkPreview\Client' not found!

Seems like composer still installing old kasp3r/link-preview package.

Also Laravel extra classes not working!

Can you please help? Thanks

403 Forbidden. Please advise a good workaround for this.

Encountered this error on a certain link I tried. I would like to know if there is a work around for websites that give this error when you try to get a link preview from it. I have tested the link in both FB and LinkedIn, and both were able to retrieve the previews perfectly.

This is the code I am using to test:

  $previewClient = new Client($url);
  $previewClient->getParser('general')->getReader()->config(['connect_timeout' => 10, 'allow_redirects' => ['max' => 5]]);
  try {
      $preview = $previewClient->getPreview('general');
  } catch (\Dusterio\LinkPreview\Exceptions\ConnectionErrorException $e) {
      $preview = $e->getMessage();
  } catch (\Dusterio\LinkPreview\Exceptions\MalformedUrlException $e) {
      $preview = $e->getMessage();
  } catch (\GuzzleHttp\Exception\ClientException $e) {
      $preview = $e->getMessage();
  }
  
  $this->p($preview);

Thank you!

Symfony 7 compatibility

Hey there, thank you for this amazing lib :-) Link preview is not working anymore on Symfony 7 because it relies on dom-crawler v6 max. Is it possible to make it compatible please? Thank you!

Inconsistent examples

Sometimes the examples refer to $client, sometimes to $previewClient.

I'm trying to test output with this:

$previewClient = new Client($url);
$previewClient->getParser('general')->getReader()->config(['connect_timeout' => 10, 'allow_redirects' => ['max' => 5]]);
try {
    $preview = $previewClient->getPreview('general');
} catch (\Dusterio\LinkPreview\Exceptions\ConnectionErrorException $e) {
    $preview = false;
} catch (\Dusterio\LinkPreview\Exceptions\MalformedUrlException $e) {
    $preview = false;
}

... and it's giving "Fatal error: Uncaught Error: Call to undefined method Dusterio\LinkPreview\Client::getParser()". It also gives fatals for uncaught exceptions. :(

Timeout

I was sort of blindly hoping this would be a nice tool for detecting if a site exists at an address, but it just takes a long time and then times out. Would that be an in-scope feature to add, or should I use more traditional means for that?

Support for video

Would you be interested in adding support for a video tag on the HTMLParser?

I have a branch where I added a couple of tags to look for video tags (such as og:video) for pages like imgur .gifv gifs, which are just mp4 using the HTML5 <video> tag, in such cases, you can embed the mp4 video instead of the much heavier gif or the inanimated jpg.

If it's fine with you let me know and I'll open a PR.

Preview response non support utf-8

<pre>Array
(
    [cover] => https://i.ytimg.com/vi/ycGfvA1vkR8/hqdefault.jpg
    [images] => Array
        (
        )

    [title] => Như Quỳnh - Duyên Phận (Thái Th�nh) PBN 99
    [description] => DUYEN PHAN (Thai Thinh) NHU QUYNH PARIS BY NIGHT 99 Phận là con gái, chưa m�t lần yêu ai Nhìn v� tương lai mà thấy như sông r�ng �ư�ng dài Cảnh nhà neo �ơn, ...
    [video] => 
    [videoType] => text/html
)
</pre>
php

Html parser error even if the url is correct

Hello,

I have used link preview to fetch data as it was working well as i have some logical changes in my code due to requirement..previously i was checking through regular expression as if the so called text has link then with the help of preg_match i will retrieve it and it will parse to your function to get all info and after the logical changes in my code for the scenario if user will enter two urls if there text contains normal url and youtube url..

$fn_url="https://www.example.com";

if($fn_url != "")
{
$previewClient = new Client($fn_url);
$preview = $previewClient->getPreview('general');
$preview = $preview->toArray();

}

if i pass variable in new Client it throwing me error all the time and if i passed the same value as static string then it is giving output with so called info in array.previously it was working well
as the code was like..and it was working well..

if(preg_match(($reg_exUrl,$text,$match))
{
$previewClient = new Client($match[0]);

            $preview = $previewClient->getPreview('general');
            $preview = $preview->toArray();

}

so please guide me if i am making any mistake..
if it can be implemented in laravel 5.4 can i get example for it..

Thanks in advance

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.