-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
185 lines (185 loc) · 6.79 KB
/
index.js
File metadata and controls
185 lines (185 loc) · 6.79 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
"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.notifier = void 0;
const express_1 = __importDefault(require("express"));
const dotenv_1 = __importDefault(require("dotenv"));
const api_1 = __importStar(require("@atproto/api"));
const axios_1 = __importDefault(require("axios"));
dotenv_1.default.config();
var session = null;
const YouTubeNotifier = require('youtube-notification');
const Parser = require('rss-parser');
const parser = new Parser();
const app = (0, express_1.default)();
const port = process.env.PORT;
const baseUrl = "http://" + process.env.CALLBACK_IP + ":" + port;
const hubCallback = `${baseUrl}/youtube/notifications`;
let channelId = process.env.CHANNEL_ID;
const agent = new api_1.default({
service: 'https://bsky.social',
persistSession: (evt, sess) => {
session = sess !== null && sess !== void 0 ? sess : null;
}
});
console.log('Starting YouTube Notifier on url ' + hubCallback);
exports.notifier = new YouTubeNotifier({
hubCallback: hubCallback,
middleware: true
});
app.use("/youtube/notifications", exports.notifier.listener());
app.listen(port, () => {
console.log(`App listening at http://localhost:${port}`);
});
exports.notifier.subscribe(channelId);
exports.notifier.on('subscribe', (data) => {
console.log('Subscribed');
console.log(data);
});
exports.notifier.on('notified', (data) => {
console.log('New Video');
console.log(data);
processVideo(data);
});
function processVideo(videoObj) {
return __awaiter(this, void 0, void 0, function* () {
// login or refresh session
if (session === null) {
console.log('Logging in');
yield agent.login({
identifier: process.env.BLUESKY_USERNAME,
password: process.env.BLUESKY_PASSWORD
});
}
else {
console.log('Refreshing session');
yield agent.resumeSession(session);
}
const videoUrl = videoObj.video.link;
console.log('Video URL: ' + videoUrl);
const cardobj = yield getCardData(videoUrl);
yield postToBlueSky(videoObj, cardobj);
});
}
function getCardData(videoUrl) {
return __awaiter(this, void 0, void 0, function* () {
const baseCardyUrl = 'https://cardyb.bsky.app/v1/extract?';
const cardyUrl = baseCardyUrl + 'url=' + encodeURIComponent(videoUrl);
const response = yield axios_1.default.get(cardyUrl)
.catch((error) => {
console.log(error.message);
});
if (response) {
let cardObj = response.data;
const buffer = yield downloadImage(cardObj.image);
console.log('Uploading ... ');
const { data } = yield agent.uploadBlob(buffer);
cardObj.thumb = data.blob.original;
return cardObj;
}
;
});
}
function downloadImage(url) {
return __awaiter(this, void 0, void 0, function* () {
const response = yield (0, axios_1.default)({
url,
method: 'GET',
responseType: 'arraybuffer'
}).catch((error) => {
console.log(error.message);
});
if (response) {
console.log('response received from url ' + url);
const buffer = Buffer.from(response.data, 'binary');
return buffer;
}
});
}
function postToBlueSky(videoObj, cardobj) {
return __awaiter(this, void 0, void 0, function* () {
const videoUrl = videoObj.video.link;
const createdAt = videoObj.published.toISOString();
const rt = new api_1.RichText({
text: `#afcb ${videoUrl}`,
});
yield rt.detectFacets(agent);
var postRecord = {
$type: 'app.bsky.feed.post',
text: rt.text,
facets: rt.facets,
createdAt: createdAt,
embed: {}
};
var embed = {
"$type": "app.bsky.embed.external#main",
'external': {
"$type": "app.bsky.embed.external#external",
"uri": videoUrl,
"title": videoObj.video.title,
"description": cardobj.description,
"thumb": cardobj.thumb
}
};
postRecord.embed = embed;
console.log('agent posting ' + videoUrl);
yield agent.post(postRecord).catch((error) => {
console.error(error.message);
});
});
}
function testPostVideo() {
return __awaiter(this, void 0, void 0, function* () {
processVideo({
video: {
id: "Y84K8rzjWTo",
title: "Brooks bags SUBLIME strike to sink the Toffees | AFC Bournemouth 1-0 Everton",
link: "https://www.youtube.com/watch?v=Y84K8rzjWTo",
},
channel: {
id: "",
name: "",
link: "",
},
published: new Date(),
updated: new Date()
});
});
}
if (process.argv[2] == 'test') {
testPostVideo();
}