Facebook::getTitle()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 11
c 0
b 0
f 0
rs 9.9
cc 3
nc 3
nop 0
1
<?php
2
3
namespace Irazasyed\VideoDownloader\Providers;
4
5
use Irazasyed\VideoDownloader\Exceptions\VideoDownloaderException;
6
7
/**
8
 * Class Facebook.
9
 */
10
class Facebook extends Provider
11
{
12
    /**
13
     * Generate URL based on Video ID/Link.
14
     *
15
     * @param $url
16
     *
17
     * @return string
18
     */
19
    public function generateUrl($url)
20
    {
21
        $id = '';
22
        if (is_int($url)) {
23
            $id = $url;
24
        } elseif (preg_match('/(?:\.?\d+)(?:\/videos)?\/?(\d+)?(?:[v]\=)?(\d+)?/i', $url, $matches)) {
25
            $id = $matches[1];
26
        }
27
28
        return 'https://www.facebook.com/video.php?v='.$id;
29
    }
30
31
    /**
32
     * Gets Video Download Links with Meta Data.
33
     * Returns HD & SD Quality Links.
34
     *
35
     * @param $url
36
     *
37
     * @throws VideoDownloaderException
38
     *
39
     * @return array
40
     */
41
    public function getVideoInfo($url)
42
    {
43
        $this->getSourceCode($this->generateUrl($url));
44
45
        $title = $this->getTitle();
46
47
        if (strtolower($title) === "sorry, this content isn't available at the moment") {
48
            throw new VideoDownloaderException('Video not available!');
49
        }
50
51
        $description = $this->getDescription();
52
        $owner = $this->getValueByKey('ownerName');
53
        $created_time = $this->getCreatedTime();
54
        $hd_link = $this->getValueByKey('hd_src_no_ratelimit');
55
        $sd_link = $this->getValueByKey('sd_src_no_ratelimit');
56
57
        return compact('title', 'description', 'owner', 'created_time', 'hd_link', 'sd_link');
58
    }
59
60
    /**
61
     * Get Video Title.
62
     *
63
     * @return string|null
64
     */
65
    public function getTitle()
66
    {
67
        $title = null;
68
        if (preg_match('/h2 class="uiHeaderTitle"?[^>]+>(.+?)<\/h2>/', $this->body, $matches)) {
69
            $title = $matches[1];
70
        } elseif (preg_match('/title id="pageTitle">(.+?)<\/title>/', $this->body, $matches)) {
71
            $title = $matches[1];
72
        }
73
74
        return $this->cleanStr($title);
75
    }
76
77
    /**
78
     * Get Description.
79
     *
80
     * @return string|bool
81
     */
82
    public function getDescription()
83
    {
84
        if (preg_match('/span class="hasCaption">(.+?)<\/span>/', $this->body, $matches)) {
85
            return $this->cleanStr($matches[1]);
86
        }
87
88
        return false;
89
    }
90
91
    /**
92
     * Get Created Time in Unix.
93
     *
94
     * @return string
95
     */
96
    public function getCreatedTime()
97
    {
98
        if (preg_match('/data-utime="(.+?)"/', $this->body, $matches)) {
99
            return $matches[1];
100
        }
101
102
        return false;
103
    }
104
105
    /**
106
     * Get Value By Key Name.
107
     *
108
     * @param $key
109
     *
110
     * @return string|bool
111
     */
112
    public function getValueByKey($key)
113
    {
114
        if (preg_match('/"'.$key.'":"(.*?)"/i', $this->body, $matches)) {
115
            $str = $this->decodeUnicode($matches[1]);
116
117
            return stripslashes(rawurldecode($str));
118
        }
119
120
        return false;
121
    }
122
}
123