-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
157 lines (127 loc) · 4.03 KB
/
index.js
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
import PlexAPI from "plex-api"
import querystring from 'node:querystring'
import fetch from "node-fetch"
import * as cheerio from 'cheerio'
import urlJoin from 'url-join'
import fs from 'fs'
import commandLineArgs from 'command-line-args'
const optionDefinitions = [
{ name: 'ip', type: String },
{ name: 'listPath', type: String, multiple: false, defaultOption: true },
{ name: 'username', type: String },
{ name: 'token', type: String }
]
const options = commandLineArgs(optionDefinitions)
const client = new PlexAPI({
hostname : options.ip,
username : options.username,
token : options.token
});
// TODO ratings
const lists = fs.readFileSync(options.listPath, 'utf8').split('\n')
async function getListInfo(url) {
let html = await fetch(url.includes('/detail') ? url : urlJoin(url, 'detail'))
let $ = cheerio.load(await html.text())
const title = $('.list-title-intro h1').text().trim()
const summary = $('.list-title-intro .body-text').text().trim()
const backdropUrl = $('#backdrop')?.data('backdrop2x')
const films = []
while (true) {
$('.film-detail-content').each((i, el) => {
const title = $(el).find('h2 > a').text().trim()
const year = parseInt($(el).find('h2 .metadata').text().trim())
films.push({
title, year
})
})
const urlNextPage = $('.paginate-nextprev .next')?.attr('href')
if (!urlNextPage) break
// Request next page
html = await fetch(new URL(urlNextPage, 'https://letterboxd.com'))
$ = cheerio.load(await html.text())
}
return {
url,
title,
summary,
backdropUrl,
films
}
}
async function processList(url) {
console.log(`Processing list "${url}"`)
const list = await getListInfo(url)
const { machineIdentifier } = (await client.query("/")).MediaContainer
const { Metadata: plexFilms } = (await client.query("/library/sections/1/all")).MediaContainer
const filmKeys = []
for (const listFilm of list.films) {
const foundFilm = plexFilms.find(plexFilm => {
const titleMatches = listFilm.title.localeCompare(plexFilm.title, undefined, { sensitivity: 'accent' }) === 0
const yearMatches = listFilm.year >= plexFilm.year - 1 && listFilm.year <= plexFilm.year + 1
return titleMatches && yearMatches
})
if (foundFilm) {
console.info(`✅ ${listFilm.title} (${listFilm.year})`)
filmKeys.push(foundFilm.key)
} else {
console.warn(`❌ ${listFilm.title} (${listFilm.year})`)
}
}
if (!filmKeys.length) {
console.warn("Empty film list")
return
}
const allCollections = (await client.query(`/library/sections/1/collections`)).MediaContainer.Metadata
let collection = allCollections?.find(collection => collection.summary.includes(list.url))
if (!collection) {
console.log(`Creating collection "${list.title}"`)
// Create collection
collection = (await client.postQuery(`/library/collections?${querystring.stringify({
type: 1,
title: list.title,
smart: 0,
sectionId: 1,
})}`)).MediaContainer.Metadata[0]
}
const { ratingKey } = collection
// Update summary
await client.putQuery(`/library/sections/1/all?${querystring.stringify({
type: 18,
id: ratingKey,
'summary.value': `${list.summary}\n\n${list.url}`,
'summary.locked': 1,
'title.value': list.title,
'title.locked': 1
})}`)
// Set sorting to "Custom"
await client.putQuery(`/library/metadata/${ratingKey}/prefs?${querystring.stringify({
collectionSort: 2 // Custom
})}`)
// Update backdrop
if (list.backdropUrl) {
await client.postQuery(`/library/collections/${ratingKey}/arts?${querystring.stringify({
url: list.backdropUrl
})}`)
}
// Add films
console.log(`Adding ${filmKeys.length} films`)
for (const key of filmKeys) {
await client.putQuery(`/library/collections/${ratingKey}/items?${querystring.stringify({
uri: `server://${machineIdentifier}/com.plexapp.plugins.library${key}`
})}`)
}
// TODO sorting
}
async function run() {
for (const list of lists) {
if (!list.trim()) continue
try {
await processList(list)
} catch (e) {
console.error(`Failed processing list "${list}": ${e.message}`)
console.error(e)
process.exitCode = 1
}
}
}
run()