This repository has been archived by the owner on Nov 25, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.go
200 lines (173 loc) · 5.52 KB
/
app.go
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
package main
import (
"context"
"encoding/json"
"fmt"
"os"
"strings"
"time"
"golang.org/x/crypto/ssh/terminal"
"github.com/mafredri/cdp"
"github.com/mafredri/cdp/protocol/runtime"
"github.com/olekukonko/tablewriter"
)
// App represents an app in the ASUSTOR Developer Corner.
type App struct {
ID int `json:"id"`
Package string `json:"package"`
Name string `json:"name"`
Arch string `json:"arch"`
Version string `json:"version"`
Categories Categories `json:"categories"`
LastUpdate time.Time `json:"lastUpdate"`
Status string `json:"status"`
Beta bool `json:"beta"`
Update *App `json:"update"`
}
// UpdateURL returns the URL for updating the App.
func (a *App) UpdateURL() string {
return fmt.Sprintf("http://developer.asustor.com/app/updateApp?id=%d", a.ID)
}
// Valid checks if the app is valid or not.
func (a *App) Valid() bool {
return a.ID != 0
}
// Categories represents app categories.
type Categories []string
// Contains returns true if the app has the category,
// uses case insensitive comparison.
func (c Categories) Contains(cat string) bool {
cat = strings.TrimSpace(strings.ToLower(cat))
for _, cc := range c {
if cat == strings.ToLower(cc) {
return true
}
}
return false
}
type appSlice []App
func (as appSlice) Find(name, arch string) (a App) {
for _, app := range as {
if app.Package == name && app.Arch == arch {
return app
}
}
return a
}
// Parse application data from the table.
const jsParseApps = `
const appTableRows = '#product-table:first-of-type tr';
const appSelect = [
{name: 'package', sel: 'td:nth-child(2)'},
{name: 'name', sel: 'td:nth-child(3)'},
{name: 'arch', sel: 'td:nth-child(4)'},
{name: 'version', sel: 'td:nth-child(6)'},
{name: 'categories', sel: 'td:nth-child(7)'},
{name: 'lastUpdate', sel: 'td:nth-child(9)'},
{name: 'status', sel: 'td:nth-child(10)'},
{name: 'id', sel: 'a[href*="app/updateApp?id="]'},
];
const appSelectNames = appSelect.map(r => r.name);
const appSelectCols = appSelect.map(r => r.sel).join(',');
const updateTableRows = '#product-table:nth-of-type(2) tr';
const updateSelect = [
{name: 'package', sel: 'td:nth-child(2)'},
{name: 'name', sel: 'td:nth-child(3)'},
{name: 'arch', sel: 'td:nth-child(4)'},
{name: 'version', sel: 'td:nth-child(5)'},
{name: 'categories', sel: 'td:nth-child(6)'},
{name: 'lastUpdate', sel: 'td:nth-child(8)'},
{name: 'status', sel: 'td:nth-child(9)'},
];
const updateSelectNames = updateSelect.map(r => r.name);
const updateSelectCols = updateSelect.map(r => r.sel).join(',');
const zipObject = (entries, cols) => Object.assign({}, ...entries.map((v, i) => ({[cols[i]]: v})));
function parseApp(el, select, names) {
const app = zipObject(columnValues(el, select), names);
app.categories = app.categories.split('\n').map(s => s.trim()).filter(s => s);
// Convert to Go time format (UTC).
app.lastUpdate = app.lastUpdate.replace(' ', 'T') + 'Z';
app.updated = !!el.querySelector('img[src*="images/forms/update.png"]');
app.beta = !!el.querySelector('img[src*="images/beta.jpg"]');
return app;
}
function setUpdatedApp(apps, update) {
const app = apps.find(app => app.updated && app.package === update.package && app.arch === update.arch);
if (!app) {
throw new Error('could not find app: ' + update.package);
}
app.update = update;
}
function columnValues(parent, sel) {
return Array.from(parent.querySelectorAll(sel))
.map(el => {
if (el.href) {
return parseInt(el.href.replace(/.*id=/, ''), 10);
}
if (el.src) {
return true;
}
return el.textContent.trim();
});
}
const apps = Array.from(document.querySelectorAll(appTableRows))
.slice(1)
.map(td => parseApp(td, appSelectCols, appSelectNames));
// Decorate app data with updated apps.
Array.from(document.querySelectorAll(updateTableRows))
.slice(1)
.map(td => parseApp(td, updateSelectCols, updateSelectNames))
.forEach(update => setUpdatedApp(apps, update));
// Filter out the older entries if there are duplicates.
apps.filter(a => !apps.find(b => b !== a && a.package === b.package && a.arch === b.arch && a.lastUpdate < b.lastUpdate));
`
func getApps(ctx context.Context, c *cdp.Client) ([]App, error) {
err := navigate(ctx, c.Page, "http://developer.asustor.com/app/mgt", 10*time.Second)
if err != nil {
return nil, err
}
evalArgs := runtime.NewEvaluateArgs(jsParseApps).SetReturnByValue(true)
eval, err := c.Runtime.Evaluate(ctx, evalArgs)
if err != nil {
return nil, err
}
if eval.ExceptionDetails != nil {
return nil, eval.ExceptionDetails
}
var apps []App
err = json.Unmarshal(eval.Result.Value, &apps)
return apps, err
}
func drawAppTable(apps ...App) {
tableHeader := []string{"Package", "Name", "Arch", "Version", "Categories", "Last update", "Status"}
table := tablewriter.NewWriter(os.Stdout)
table.SetHeader(tableHeader)
table.SetAutoWrapText(true)
table.SetHeaderAlignment(tablewriter.ALIGN_LEFT)
if width, _, err := terminal.GetSize(int(os.Stdin.Fd())); err == nil {
table.SetColWidth(width / len(tableHeader))
}
for _, a := range apps {
table.Append([]string{
a.Package,
a.Name,
a.Arch,
a.Version,
strings.Join(a.Categories, ", "),
a.LastUpdate.Format("2006-01-02 15:04:05"),
a.Status,
})
if a.Update != nil {
table.Append([]string{
"",
a.Update.Name,
a.Update.Arch,
a.Update.Version,
strings.Join(a.Update.Categories, ", "),
a.Update.LastUpdate.Format("2006-01-02 15:04:05"),
a.Update.Status,
})
}
}
table.Render()
}