aboutsummaryrefslogtreecommitdiff
path: root/bridges/UberNewsroomBridge.php
blob: 333200cd08e5f35a7c04a53547ce1f1440f40488 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
<?php

class UberNewsroomBridge extends BridgeAbstract
{
    const NAME = 'Uber Newsroom Bridge';
    const URI = 'https://www.uber.com';
    const URI_API_DATA = 'https://newsroomapi.uber.com/wp-json/newsroom/v1/data?locale=';
    const URI_API_POST = 'https://newsroomapi.uber.com/wp-json/wp/v2/posts/';
    const DESCRIPTION = 'Returns news posts';
    const MAINTAINER = 'VerifiedJoseph';
    const PARAMETERS = [[
        'region' => [
            'name' => 'Region',
            'type' => 'list',
            'values' => [
                'Africa' => [
                    'Egypt' => 'ar-EG',
                    'Ghana' => 'en-GH',
                    'Kenya' => 'en-KE',
                    'Morocco' => 'fr-MA',
                    'Nigeria' => 'en-NG',
                    'South Africa' => 'en-ZA',
                    'Tanzania' => 'en-TZ',
                    'Uganda' => 'en-UG',
                ],
                'Asia' => [
                    'Bangladesh' => 'en-BD',
                    'Cambodia' => 'km-KH',
                    'China' => 'zh-CN',
                    'Hong Kong' => 'zh-HK',
                    'India' => 'en-IN',
                    'Indonesia' => 'en-ID',
                    'Japan' => 'ja-JP',
                    'Korea' => 'ko-KR',
                    'Macau' => 'zh-MO',
                    'Malaysia' => 'en-MY',
                    'Myanmar' => 'en-MM',
                    'Philippines' => 'en-PH',
                    'Singapore' => 'en-SG',
                    'Sri Lanka' => 'en-LK',
                    'Taiwan' => 'zh-TW',
                    'Thailand' => 'th-TH',
                    'Vietnam' => 'vi-VN',
                ],
                'Central America' => [
                    'Costa Rica' => 'es-CR',
                    'Dominican Republic' => 'es-DO',
                    'El Salvador' => 'es-SV',
                    'Guatemala' => 'es-GT',
                    'Honduras' => 'es-HN',
                    'Mexico' => 'es-MX',
                    'Nicaragua' => 'es-NI',
                    'Panama' => 'es-PA',
                    'Puerto Rico' => 'es-PR',
                ],
                'Europe' => [
                    'Austria' => 'de-AT',
                    'Azerbaijan' => 'az',
                    'Belarus' => 'ru-BY',
                    'Belgium' => 'fr-BE',
                    'Bulgaria' => 'bg',
                    'Croatia' => 'hr',
                    'Czech Republic' => 'cs-CZ',
                    'Denmark' => 'da-DK',
                    'Estonia' => 'et-EE',
                    'Finland' => 'fi',
                    'France' => 'fr',
                    'Germany' => 'de',
                    'Greece' => 'el-GR',
                    'Hungary' => 'hu',
                    'Ireland' => 'en-IE',
                    'Italy' => 'it',
                    'Kazakhstan' => 'ru-KZ',
                    'Lithuania' => 'lt',
                    'Netherlands' => 'nl',
                    'Norway' => 'nb-NO',
                    'Poland' => 'pl',
                    'Portugal' => 'pt',
                    'Romania' => 'ro',
                    'Russia' => 'ru',
                    'Slovakia' => 'sk',
                    'Spain' => 'es-ES',
                    'Sweden' => 'sv-SE',
                    'Switzerland' => 'fr-CH',
                    'Turkey' => 'tr',
                    'Ukraine' => 'uk-UA',
                    'United Kingdom' => 'en-GB',
                ],
                'Middle East' => [
                    'Bahrain' => 'en-BH',
                    'Israel' => 'he-IL',
                    'Jordan' => 'en-JO',
                    'Kuwait' => 'en-KW',
                    'Lebanon' => 'en-LB',
                    'Pakistan' => 'en-PK',
                    'Qatar' => 'en-QA',
                    'Saudi Arabia' => 'ar-SA',
                    'United Arab Emirates' => 'en-AE',
                ],
                'North America' => [
                    'Canada' => 'en-CA',
                    'United States' => 'en-US',
                ],
                'Pacific' => [
                    'Australia' => 'en-AU',
                    'New Zealand' => 'en-NZ',
                ],
                'South America' => [
                    'Argentina' => 'es-AR',
                    'Bolivia' => 'es-BO',
                    'Brazil' => 'pt-BR',
                    'Chile' => 'es-CL',
                    'Colombia' => 'es-CO',
                    'Ecuador' => 'es-EC',
                    'Paraguay' => 'es-PY',
                    'Peru' => 'es-PE',
                    'Trinidad & Tobago' => 'en-TT',
                    'Uruguay' => 'es-UY',
                    'Venezuela' => 'es-VE',
                ],
            ],
            'defaultValue' => 'en-US',
        ]
    ]];

    const CACHE_TIMEOUT = 3600;

    private $regionName = '';

    public function collectData()
    {
        $json = getContents(self::URI_API_DATA . $this->getInput('region'));
        $data = json_decode($json);

        $this->regionName = $data->region->name;

        foreach ($data->articles as $article) {
            $json = getContents(self::URI_API_POST . $article->id);
            $post = json_decode($json);

            $item = [];
            $item['title'] = $post->title->rendered;
            $item['timestamp'] = $post->date;
            $item['uri'] = $post->link;
            $item['content'] = $this->formatContent($post->content->rendered);
            $item['enclosures'][] = $article->image_full;

            $this->items[] = $item;
        }
    }

    public function getURI()
    {
        if (is_null($this->getInput('region')) === false) {
            return self::URI . '/' . $this->getInput('region') . '/newsroom';
        }

        return parent::getURI() . '/newsroom';
    }

    public function getName()
    {
        if (is_null($this->getInput('region')) === false) {
            return $this->regionName . ' - Uber Newsroom';
        }

        return parent::getName();
    }

    private function formatContent($html)
    {
        $html = str_get_html($html);

        foreach ($html->find('div.wp-video') as $div) {
            $div->style = '';
        }

        foreach ($html->find('video') as $video) {
            $video->width = '100%';
            $video->height = '';
        }

        return $html;
    }
}