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
|
--[[
* Copyright (C) 2015 Bastien Nocera
*
* Contact: Bastien Nocera <hadess@hadess.net>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public License
* as published by the Free Software Foundation; version 2.1 of
* the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA
*
--]]
-- Documented at:
-- http://getpocket.com/developer/docs/v3/retrieve
--
-- We only get videos here because if we didn't filter ahead of time
-- we'd need to check whether each URL was supported through
-- totem-pl-parser/quvi, which would be too slow
POCKET_GET_URL = 'https://getpocket.com/v3/get?consumer_key=%s&access_token=%s&sort=newest&contentType=video&detailType=complete&count=%d&offset=%d'
HAS_VIDEO = '1'
IS_VIDEO = '2'
---------------------------
-- Source initialization --
---------------------------
source = {
id = "grl-pocket-lua",
name = 'Pocket',
description = 'A source for browsing Pocket videos',
goa_account_provider = 'pocket',
goa_account_feature = 'read-later',
supported_keys = { 'id', 'thumbnail', 'title', 'url', 'favourite', 'creation-date' },
supported_media = 'video',
icon = 'resource:///org/gnome/grilo/plugins/pocket/pocket.svg',
tags = { 'net:internet' }
}
------------------
-- Source utils --
------------------
function grl_source_browse(media_id)
local count = grl.get_options("count")
local skip = grl.get_options("skip")
local operation_id = grl.get_options('operation-id')
local url = string.format(POCKET_GET_URL, grl.goa_consumer_key(), grl.goa_access_token(), count, skip)
grl.debug ("Fetching URL: " .. url .. " (count: " .. count .. " skip: " .. skip .. ")")
grl.fetch(url, "pocket_fetch_cb")
end
------------------------
-- Callback functions --
------------------------
-- Newest first
function sort_added_func(itema, itemb)
return itema.time_added > itemb.time_added
end
-- From http://lua-users.org/wiki/StringRecipes
function string.starts(String,Start)
return string.sub(String,1,string.len(Start))==Start
end
-- return all the media found
function pocket_fetch_cb(results)
local count = grl.get_options("count")
if not results then
grl.callback ()
return
end
json = grl.lua.json.string_to_table(results)
-- Put the table in an array so we can sort it
local array = {}
for n, item in pairs(json.list) do table.insert(array, item) end
table.sort(array, sort_added_func)
for i, item in ipairs(array) do
local media = create_media(item)
if media then
count = count - 1
grl.callback(media, count)
end
-- Bail out if we've given enough items
if count == 0 then
return
end
end
grl.callback()
end
-------------
-- Helpers --
-------------
function create_media(item)
local media = {}
if not item.has_video or
(item.has_video ~= HAS_VIDEO and item.has_video ~= IS_VIDEO) then
grl.debug("We filtered for videos, but this isn't one: " .. grl.lua.inspect(item))
return nil
end
if item.has_video == HAS_VIDEO then
if not item.videos then
grl.debug('Item has no video, skipping: ' .. grl.lua.inspect(item))
return nil
end
if #item.videos > 1 then
grl.debug('Item has than one video, skipping: ' .. grl.lua.inspect(item))
return nil
end
end
media.type = "video"
media.id = item.resolved_id
if media.id == '' then
media.id = item.item_id
end
local url = item.resolved_url
if url == '' then
url = item.given_url
end
if item.has_video == HAS_VIDEO then
url = item.videos['1'].src
-- BUG: Pocket puts garbage like:
-- src = "//player.vimeo.com/video/75911370"
-- FIXME: this should be https instead but then
-- quvi doesn't detect it
if string.starts(url, '//') then url = 'http:' .. url end
end
if grl.is_video_site(url) then
media.external_url = url
else
media.url = url
end
media.title = item.resolved_title
if media.title == '' then
media.title = item.given_title
end
if media.title == '' then
media.title = media.url
end
media.favourite = (item.favorite and item.favorite == '1')
if item.image then
media.thumbnail = item.image.src
end
media.creation_date = item.time_added
media.modification_date = item.time_updated
return media
end
|