-
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathindex.js
More file actions
194 lines (180 loc) · 7.2 KB
/
Copy pathindex.js
File metadata and controls
194 lines (180 loc) · 7.2 KB
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
const MovieDB = require('node-themoviedb');
const { Telegraf } = require('telegraf')
const emoji = require('node-emoji')
require('dotenv').config()
const mdb = new MovieDB(process.env.TMD_TOKEN, {})
const bot = new Telegraf(process.env.TELEGRAM_TOKEN)
// Gets item image link
// Caveat: between '[' and ']' in the link U+200C zero-width character
const getItemImageLink = (item) => {
return `[](http://image.tmdb.org/t/p/original${item.poster_path || item.profile_path})`
}
// Gets item production countries flags
const getProductionFlags = (item) => {
return item.production_countries ? item.production_countries.map(c => emoji.get(`flag-${c.iso_3166_1.toLowerCase()}`)) : ''
}
// Prints a decimal vote as number of stars out of ten
const printRating = (vote) => {
return emoji.get(vote < 5 ? "confused" : vote < 6.5 ? "expressionless" : "grin") + " " + vote;
}
// Gets link to a person
const getPersonLink = (person) => {
return `[${person.name}](https://www.themoviedb.org/person/${person.id})`
}
// Gets link to a movie/tv item
const getMovieLink = (movie) => {
return `[${movie.name || movie.title}](https://www.themoviedb.org/${movie.media_type}/${movie.id})`
}
// Get link to a genre item
const getGenreLink = (genre, media_type) => {
return `[${genre.name}](https://www.themoviedb.org/genre/${genre.id}/${media_type})`
}
// Get icon for a media type
const getMediaTypeIcon = (media_type) => {
let icons = {
tv: 'tv',
movie: 'film_projector',
person: 'smiley'
}
return emoji.get(icons[media_type] || 'question')
}
const renderTvMarkdown = async (item) => {
try {
let details = (await mdb.tv.getDetails({ pathParameters: { tv_id: item.id } })).data
let result = getItemImageLink(item)
result += `*${item.title || item.name}* ${getProductionFlags(details)}\n\n`
result += `*Original title*: [${item.original_name || item.name}](https://www.themoviedb.org/tv/${item.id})\n`
if (details.first_air_date) {
result += ` (${details.first_air_date} - {${details.last_air_date || '?'}})`
}
result += "\n"
if (details.created_by) {
result += `*Created by*: ${details.created_by.map(p => getPersonLink(p)).join(', ')}\n`
}
if (details.number_of_seasons) {
result += `*Seasons*: ${details.number_of_seasons}\n`
}
if (details.number_of_episodes) {
result += `*Episodes*: ${details.number_of_episodes}\n`
}
if (details.genres) {
result += `*Genres*: ${details.genres.map((genre) => getGenreLink(genre, item.media_type)).join(', ')}\n`
}
result += "\n"
if(item.vote_count) {
result += "*Vote average*: " + printRating(item.vote_average) + "\n"
result += "\n"
}
result += `\n${item.overview}`
return result
} catch (e) {
console.error(e)
return `*Errore nel recupero dettagli TV*: ${item.title || item.name}`
}
}
const renderMovieMarkdown = async (item) => {
try {
let details = (await mdb.movie.getDetails({ pathParameters: { movie_id: item.id }, query: { append_to_response: "credits" }})).data
let result = getItemImageLink(item)
result += `*${item.title}* ${getProductionFlags(details)}\n\n`
result += `*Original title*: [${item.original_title || item.title}](https://www.themoviedb.org/movie/${item.id})`
if (details.release_date) {
let release_date = details.release_date.substr(0, 4);
result += ` (${release_date})`
}
result += "\n"
if (details.runtime) {
result += `*Runtime*: ${details.runtime}mins\n`
}
if (details.genres) {
result += `*Genres*: ${details.genres.map((genre) => getGenreLink(genre, item.media_type)).join(', ')}\n`
}
if (details.credits && details.credits.crew) {
let directors = [...new Set(details.credits.crew.filter(p => p.job === 'Director' || p.job === 'Co-Director' ))];
let writers = [...new Set(details.credits.crew.filter(p => p.department === 'Writing'))]
let cast = [...new Set(details.credits.cast.filter(p => p.order < 10))]
result += `*Directed by*: ${directors.map(p => getPersonLink(p)).join(', ')}\n`
result += `*Story by*: ${writers.map(p => getPersonLink(p)).join(', ')}\n`
result += "\n"
result += `*Cast*: ${cast.map(p => getPersonLink(p)).join(', ')}\n`
result += "\n"
if(item.vote_count) {
result += "*Vote average*: " + printRating(item.vote_average) + "\n"
result += "\n"
}
}
result += `*Other sites:* [Letterboxd](http://letterboxd.com/tmdb/${item.id})`
if (details.imdb_id) {
result += `, [IMDb](https://www.imdb.com/title/${details.imdb_id})`
}
result += "\n"
result += `\n${item.overview}`
return result
} catch (e) {
console.error(e)
return `*Errore nel recupero dettagli Film*: ${item.title}`
}
}
const renderPersonMarkdown = async (item) => {
try {
let details = (await mdb.person.getDetails({ pathParameters: { person_id: item.id } })).data
let result = getItemImageLink(item)
result += `*${item.name}*\n\n`
result += `[TMDB page](https://www.themoviedb.org/person/${item.id})\n\n`
if (details.birthday) {
result += `*Birth:* ${details.birthday}` + (details.place_of_birth ? ` (${details.place_of_birth})` : '') + "\n"
}
if (details.deathday) {
result += `*Death:* ${details.deathday}\n`
}
if (details.imdb_id) {
result += `Other sites: [IMDb](https://www.imdb.com/name/${details.imdb_id})`
}
result += `\n*Known for:* ${item.known_for.map(el => getMovieLink(el)).join(', ')}`
return result
} catch (e) {
console.error(e)
return `*Errore nel recupero dettagli Persona*: ${item.name}`
}
}
const tmdbMultiItemToMarkdown = async (item) => {
if (item.media_type === 'tv') {
return await renderTvMarkdown(item)
}
if (item.media_type === 'movie') {
return await renderMovieMarkdown(item)
}
if (item.media_type === 'person') {
return await renderPersonMarkdown(item)
}
}
// Parses TMDB data to get a Telegram inline query response
const tmdbSearchResultsToQueryResponse = (results) => results.map(result => (async () => {
let message_text = await tmdbMultiItemToMarkdown(result)
let title_date = result.release_date || result.first_air_date || undefined;
return {
type: 'article',
id: result.media_type + result.id,
title: `${getMediaTypeIcon(result.media_type)} ${result.name || result.title} ${title_date ? '(' + title_date.substr(0,4) + ')' : ''}`,
description: result.overview,
thumb_url: `http://image.tmdb.org/t/p/w92${result.profile_path || result.poster_path}`,
input_message_content: {
'message_text': message_text,
'parse_mode': 'Markdown'
}
}
})())
bot.on('inline_query', async (ctx) => {
try {
let response = [];
if (ctx.inlineQuery.query) {
let results = await mdb.search.multi({ query: { query: ctx.inlineQuery.query } });
response = await Promise.all(tmdbSearchResultsToQueryResponse(results.data.results))
}
return await ctx.answerInlineQuery(response)
} catch (e) {
console.error(e)
return await ctx.answerInlineQuery([])
}
})
bot.launch()