-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
851 lines (672 loc) · 29.2 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
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
const TelegramBot = require('node-telegram-bot-api');
const nodeCron = require("node-cron");
const twitterText = require('twitter-text');
const request = require('request').defaults({ encoding: null });
// SWAPI
const swapi = require('swapi-node');
let TOTAL_STARWARS_CHARACTERS_COUNT = 83
let TOTAL_STARWARS_STARSHIPS_COUNT = 37
let TOTAL_STARWARS_SPECIES_COUNT = 37
// MARVEL
const api = require('marvel-api');
const marvel = api.createClient({
publicKey: process.env.MARVEL_KEY,
privateKey: process.env.MARVEL_KEY_SECRET
});
let TOTAL_MARVEL_CHARACTERS_COUNT = 1562
let TOTAL_MARVEL_STORY_COUNT = 123823
let TOTAL_MARVEL_EVENT_COUNT = 75
// TELEGRAM
const token = process.env.TELEGRAM_API_KEY_DALLE2;
const bot = new TelegramBot(token, {polling: true});
// OPENAI
const { Configuration, OpenAIApi } = require("openai");
const configuration = new Configuration({
apiKey: process.env.OPEN_AI_KEY,
});
const openai = new OpenAIApi(configuration);
const openaiTextModels = ['text-ada-001', 'text-babbage-001', 'text-curie-001', 'text-davinci-003', 'text-ada-001', 'text-babbage-001', 'text-curie-001']
const modelToHashtag = new Map([
['text-ada-001', '#textada001'],
['text-babbage-001', '#textbabbage001'],
['text-curie-001', '#textcurie001'],
['text-davinci-003', '#textdavinci003'],
]);
// END OPENAI
// NYTIMES
// https://api.nytimes.com/svc/books/v3/lists/full-overview.json?api-key=amKvmaWADFUgzl4EEjHNaZauh7E6c4Sm
const nytimesKey = process.env.NY_TIMES_KEY
const nytimesKeySecret = process.env.NY_TIMES_KEY_SECRET
// TWITTER
const config = {
consumer_key: process.env.TWITTER_CONSUMER_API_KEY,
consumer_secret: process.env.TWITTER_CONSUMER_API_SECRET,
access_token_key: process.env.TWITTER_ACCESS_TOKEN,
access_token_secret: process.env.TWITTER_ACCESS_TOKEN_SECRET
}
const twitter = require('twitter-lite');
const client = new twitter(config);
config.newClient = function (subdomain = 'api') {
return new twitter({
subdomain,
consumer_key: process.env.TWITTER_CONSUMER_API_KEY,
consumer_secret: process.env.TWITTER_CONSUMER_API_SECRET,
access_token_key: process.env.TWITTER_ACCESS_TOKEN,
access_token_secret: process.env.TWITTER_ACCESS_TOKEN_SECRET
});
}
const uploadClient = config.newClient('upload');
const tweet = async (textToTweet, imageUrl) => {
request.get(imageUrl, function (error, response, body) {
if (!error && response.statusCode == 200) {
uploadClient.post('media/upload', { media_data: Buffer.from(body).toString('base64') })
.then(media => {
// console.log('You successfully uploaded media');
const media_id = media.media_id_string;
client.post('statuses/update', { status: textToTweet, media_ids: media_id })
.then(tweet => {
// console.log('Your image tweet is posted successfully');
console.log('successfully tweeted this : "' + tweet.text + '"');
}).catch(console.error);
}).catch(console.error);
}
});
}
const fixTweetDescription = (originalTweet, fixedLenghText, variableDescription, originalDescription, hashtags) => {
try {
// fixedLenghText + variableDescription + hashtags = 280 chars
/**
* idea is to get the lenght of fixedLenghText + hashtags
* and use the remaining chars to crop description with '...'
*
* */
let mainLengh = `${fixedLenghText} ${hashtags}`.length
let delta = (true === originalDescription ? 273 : 265) - mainLengh; // because of desc | desc (by AI) and spaces
if (delta < 0) {
//doSomething
console.log(`@@@@@@@@@@@@@@@@@@ delta < 0; delta=[${delta}]`)
return originalTweet
}
let fixedDescription = variableDescription.substring(0, (delta - 3)) + '...'
let wholeTweet = `${fixedLenghText} ${(true === originalDescription ? 'desc:' : 'desc (by AI):')} ${fixedDescription} ${hashtags}`
if (twitterText.parseTweet(wholeTweet).valid) {
let length = wholeTweet.length
console.log(`fixed tweet=[${wholeTweet}] length=[${length}]`)
return wholeTweet;
} else {
//doSomething
let length = wholeTweet.length
console.log(`@@@@@@@@@@@@@@@@@@ NOT VALID tweet=[${wholeTweet}] length=[${length}]`)
return originalTweet
}
} catch(err) {
console.log(err)
return originalTweet
}
}
// END TWITTER
/**
*
* OPENAI
*
* */
const generateRandomModel = () => {
const rndInt = Math.floor(Math.random() * (7 - 0)) + 0;
const model = openaiTextModels[rndInt];
return model;
}
const generateDescriptionByPrompt = async (prompt, model, maxTokens) => {
try {
const response = await openai.createCompletion({
model: model,
prompt: prompt,
temperature: 0,
max_tokens: maxTokens,
top_p: 1,
frequency_penalty: 0.2,
presence_penalty: 0,
});
// console.log(response.data.choices[0].text)
return response;
} catch(err) {
console.log(`error generating prompt. err=[${err}] returning prompt=[${prompt}]`)
return prompt;
}
}
const generateImage = async (text) => {
console.log(`generateImage prompt=[${text}]`)
const response = await openai.createImage({
prompt: text,
n: 1,
size: "512x512",
});
let image_url = response.data.data[0].url;
// console.log(`url of generated image = [${image_url}]`)
return image_url;
}
/**
*
* END OPENAI
* */
/**
*
* CONTENT GENERATOR
*
* USING;
* MARVEL; characters, stories, events
* STAR WARS; characters, species, starships
*
* */
// ************************* GUTTENBERG *************************
/**
* GUTTENBERG
* */
const getAuthorBirthYear = (book) => {
if (book.authors && book.authors.length >= 0 && book.authors[0]?.birth_year) {
return book.authors[0]?.birth_year
} else {
return 'unknown'
}
}
const getBookAuthor = (book) => {
if (book.authors && book.authors.length >= 0 && book.authors[0]?.name) {
return book.authors[0]?.name
} else {
return 'unknown'
}
}
const fixGuttenbergTweet = (text, book, hashtags) => {
try {
let suffix = `- author: ${getBookAuthor(book)} - birthYear: ${getAuthorBirthYear(book)} #dalle2 #dalle #openai ${hashtags}`
let delta = 240 - suffix.replace(/[^a-z]/gi, "").length;
let bookTitle = book.title;
let fixed = bookTitle.substring(0, (delta - 3)) + '...'
let fixedTweet = `Book: ${fixed} ${suffix}`;
let length = fixedTweet.replace(/[^a-z]/gi, "").length
console.log(`fixed tweet=[${fixedTweet}] length=[${length}]`)
return fixedTweet;
} catch(err) {
console.log(err)
return text
}
}
const generateRandomBook = async () => {
const url = 'http://gutendex.com/books'
const res = await fetch(url);
const data = await res.json();
// console.log(data)
let count = data.count;
const rndInt = Math.floor(Math.random() * count) + 1;
console.log(`fetching book id=[${rndInt}] count=[${count}]`);
const bookUrl = `http://gutendex.com/books/${rndInt}`;
const bookResponse = await fetch(bookUrl);
const book = await bookResponse.json();
// console.log(book);
return book;
}
const guttenberbTweetWorker = async () => {
let book = await generateRandomBook()
// console.log(`book name=[${book.title}] author=[${book.authors[0].name}]`);
console.log(`book name=[${book.title}] - author: [${getBookAuthor(book)}]`);
let model = generateRandomModel()
let prompt = `I want to generate a description of a book cover based on the book title ${book.title}, author ${getBookAuthor(book)}`
let description = await generateDescriptionByPrompt(prompt, model, 80);
// console.log(description.data.choices[0].text)
let imageDescription;
if (description.data && description.data.choices.length >= 0) {
imageDescription = `A very good book cover image for a book named ${book.title}, with the description: ${description.data.choices[0].text}`
} else {
imageDescription = `A very good book cover image for a book named ${book.title}, with the description: ${prompt}`
}
// console.log(imageDescription)
let url = await generateImage(imageDescription);
// console.log(`url=[${url}]`)
let tweetText = `Book: ${book.title} - author: ${getBookAuthor(book)} - birthYear: ${getAuthorBirthYear(book)} #dalle2 #dalle #openai ${modelToHashtag.get(model)}`
if (!twitterText.parseTweet(tweetText).valid) {
fixed = fixGuttenbergTweet(tweetText, book, modelToHashtag.get(model))
tweetText = fixed
}
await tweet(tweetText, url)
}
// * * 1 * *
// const guttenberJob = nodeCron.schedule("0 */23 * * *", () => {
// try {
// guttenberbTweetWorker()
// console.log(`job=[guttenberJob] timestamp=[${new Date().toLocaleString()}]`);
// } catch(err) {
// console.log(err)
// }
// });
/**
*
* END GUTTENBERG
* */
// ************************* MARVEL *************************
/**
* MARVEL
* */
/**
*
* STORIES
* */
const generateRandomMarvelStory = async (callback) => {
const random = Math.floor(Math.random() * (TOTAL_MARVEL_STORY_COUNT - 0)) + 0
marvel.stories.findAll(1, random)
.then((result) => {
// console.log(result)
let updatedTotal = +result.meta.total+1
console.log(`TOTAL_MARVEL_STORY_COUNT =[${TOTAL_MARVEL_STORY_COUNT}] updated=[${updatedTotal}]`)
TOTAL_MARVEL_STORY_COUNT = updatedTotal
callback(result.data[0])
})
.fail(console.error)
.done();
}
const marvelStoriesTweetWorker = async () => {
generateRandomMarvelStory(async (story) => {
// console.log(`story title=[${story.title}] description=[${story.description}]`)
if (story.description && '' != story.description) {
let imageDescription = `An high definition wallpaper image of an marvel story titled ${story.title} portrayed as ${story.description}`
// console.log(imageDescription)
let url = await generateImage(imageDescription);
// console.log(`url=[${url}]`)
let originalTweet = `Marvel story: ${story.title} desc: ${story.description} #marvel #marvelapi #dalle2 #dalle #openai`
if (!twitterText.parseTweet(originalTweet).valid) {
let startText = `Marvel story: ${story.title}`
let hashtags = `#marvel #marvelapi #dalle2 #dalle #openai`
let variableDescription = story.description
let fixedTweet = fixTweetDescription(originalTweet, startText, variableDescription, true, hashtags)
originalTweet = fixedTweet
}
await tweet(originalTweet, url)
} else {
let model = generateRandomModel()
let prompt = `I want to generate the description of an image of an epic story titled ${story.title}`
// console.log(prompt);
let description = await generateDescriptionByPrompt(prompt, model, 50);
// console.log(description.data.choices[0].text)
let imageDescription = `An high definition wallpaper image of an marvel story titled ${story.title} portrayed as ${description.data.choices[0].text.replace(/[\r\n]/gm, '')}`
// console.log(imageDescription)
let url = await generateImage(imageDescription);
// console.log(`url=[${url}]`)
let originalTweet = `Marvel story: ${story.title} desc (by AI): ${description.data.choices[0].text.replace(/[\r\n]/gm, '')} #marvel #marvelapi #dalle2 #dalle #openai ${modelToHashtag.get(model)}`
if (!twitterText.parseTweet(originalTweet).valid) {
let startText = `Marvel story: ${story.title}`
let hashtags = `#marvel #marvelapi #dalle2 #dalle #openai ${modelToHashtag.get(model)}`
let variableDescription = description.data.choices[0].text.replace(/[\r\n]/gm, '')
let fixedTweet = fixTweetDescription(originalTweet, startText, variableDescription, false, hashtags)
originalTweet = fixedTweet
}
await tweet(originalTweet, url)
}
})
}
const marvelStoriesJob = nodeCron.schedule("0 */4 * * *", () => {
try {
marvelStoriesTweetWorker()
console.log(`job=[marvelStoriesJob] timestamp=[${new Date().toLocaleString()}]`);
} catch(err) {
console.log(err)
}
});
/**
*
* END STORIES
* */
/**
*
* EVENTS
* */
const generateRandomMarvelEvent = async (callback) => {
const random = Math.floor(Math.random() * (TOTAL_MARVEL_EVENT_COUNT - 0)) + 0
marvel.events.findAll(1, random)
.then((result) => {
// console.log(result)
let updatedTotal = +result.meta.total+1
console.log(`TOTAL_MARVEL_EVENT_COUNT =[${TOTAL_MARVEL_EVENT_COUNT}] updated=[${updatedTotal}]`)
TOTAL_MARVEL_EVENT_COUNT = updatedTotal
callback(result.data[0])
})
.fail(console.error)
.done();
}
const marvelEventsTweetWorker = async () => {
generateRandomMarvelEvent(async (event) => {
// console.log(`event title=[${event.title}] description=[${event.description}]`)
if (event.title && event.description && '' != event.description) {
let imageDescription = `An high definition wallpaper image of an marvel event titled ${event.title} portrayed as ${event.description}`
// console.log(imageDescription)
let url = await generateImage(imageDescription);
// console.log(`url=[${url}]`)
let originalTweet = `Marvel event: ${event.title} desc: ${event.description} #marvel #marvelapi #dalle2 #dalle #openai`
if (!twitterText.parseTweet(originalTweet).valid) {
let startText = `Marvel event: ${event.title}`
let hashtags = `#marvel #marvelapi #dalle2 #dalle #openai`
let variableDescription = event.description
let fixedTweet = fixTweetDescription(originalTweet, startText, variableDescription, true, hashtags)
originalTweet = fixedTweet
}
await tweet(originalTweet, url)
}
})
}
const marvelEventsJob = nodeCron.schedule("0 */10 * * *", () => {
try {
marvelEventsTweetWorker()
console.log(`job=[marvelEventsJob] timestamp=[${new Date().toLocaleString()}]`);
} catch(err) {
console.log(err)
}
});
/**
*
* END EVENTS
* */
/**
*
* CHARACTER
* */
const generateRandomMarvelCharacter = async (callback) => {
const random = Math.floor(Math.random() * (TOTAL_MARVEL_CHARACTERS_COUNT - 0)) + 0
marvel.characters.findAll(1, random)
.then((result) => {
// console.log(result)
let updatedTotal = +result.meta.total+1
console.log(`TOTAL_MARVEL_CHARACTERS_COUNT =[${TOTAL_MARVEL_CHARACTERS_COUNT}] updated=[${updatedTotal}]`)
TOTAL_MARVEL_CHARACTERS_COUNT = updatedTotal
callback(result.data[0])
})
.fail(console.error)
.done();
}
const marvelCharacterTweetWorker = async () => {
generateRandomMarvelCharacter(async (character) => {
// console.log(character)
if (character.description && '' != character.description) {
let imageDescription = `An high definition wallpaper image of an marvel character named ${character.name} portrayed as ${character.description}`
// console.log(imageDescription)
let url = await generateImage(imageDescription);
// console.log(`url=[${url}]`)
let originalTweet = `Marvel character: ${character.name} desc: ${character.description} #marvel #marvelapi #dalle2 #dalle #openai`
if (!twitterText.parseTweet(originalTweet).valid) {
let startText = `Marvel character: ${character.name}`
let hashtags = `#marvel #marvelapi #dalle2 #dalle #openai`
let variableDescription = character.description
let fixedTweet = fixTweetDescription(originalTweet, startText, variableDescription, true, hashtags)
originalTweet = fixedTweet
}
await tweet(originalTweet, url)
} else {
let model = generateRandomModel()
let prompt = `I want to generate the description of an image of an epic character named ${character.name}`
// console.log(prompt);
let description = await generateDescriptionByPrompt(prompt, model, 50);
// console.log(description.data.choices[0].text)
let imageDescription = `An high definition wallpaper image of an marvel character named ${character.name} portrayed as ${description.data.choices[0].text.replace(/[\r\n]/gm, '')}`
// console.log(imageDescription)
let url = await generateImage(imageDescription);
// console.log(`url=[${url}]`)
let originalTweet = `Marvel character: ${character.name} desc (by AI): ${description.data.choices[0].text.replace(/[\r\n]/gm, '')} #marvel #marvelapi #dalle2 #dalle #openai ${modelToHashtag.get(model)}`
if (!twitterText.parseTweet(originalTweet).valid) {
let startText = `Marvel character: ${character.name}`
let hashtags = `#marvel #marvelapi #dalle2 #dalle #openai ${modelToHashtag.get(model)}`
let variableDescription = description.data.choices[0].text.replace(/[\r\n]/gm, '')
let fixedTweet = fixTweetDescription(originalTweet, startText, variableDescription, false, hashtags)
originalTweet = fixedTweet
}
await tweet(originalTweet, url)
}
})
}
const marvelCharacterJob = nodeCron.schedule("0 */6 * * *", () => {
try {
marvelCharacterTweetWorker()
console.log(`job=[marvelCharacterJob] timestamp=[${new Date().toLocaleString()}]`);
} catch(err) {
console.log(err)
}
});
/**
*
* END CHARACTER
* */
// ************************* STAR WARS *************************
/**
* STAR WARS
*
* USING SWAPI
* */
/**
*
* CHARACTER
* */
const generateRandomStarWarsCharacter = async (callback) => {
const random = Math.floor(Math.random() * (TOTAL_STARWARS_CHARACTERS_COUNT - 0)) + 0
swapi.people({ id: random }).then((result) => {
// console.log(result);
callback(result)
});
}
const starWarsCharacterTweetWorker = async () => {
generateRandomStarWarsCharacter(async (character) => {
// console.log(`character name=[${character.name}]`)
if (character.name) {
let model = generateRandomModel()
let prompt = `I want to generate the description of an image of an epic star wars character named ${character.name} and characteristics as ${character}`
// console.log(prompt);
let description = await generateDescriptionByPrompt(prompt, model, 50);
// console.log(description.data.choices[0].text)
let imageDescription = `An high definition wallpaper image of an star wars character named ${character.name} portrayed as ${description.data.choices[0].text.replace(/[\r\n]/gm, '')}`
// console.log(imageDescription)
let url = await generateImage(imageDescription);
// console.log(`url=[${url}]`)
let originalTweet = `StarWars character: ${character.name} desc (by AI): ${description.data.choices[0].text.replace(/[\r\n]/gm, '')} #dalle2 #dalle #openai #swapiapi ${modelToHashtag.get(model)}`
if (!twitterText.parseTweet(originalTweet).valid) {
let startText = `StarWars character: ${character.name}`
let hashtags = `#dalle2 #dalle #openai #swapiapi ${modelToHashtag.get(model)}`
let variableDescription = description.data.choices[0].text.replace(/[\r\n]/gm, '')
let fixedTweet = fixTweetDescription(originalTweet, startText, variableDescription, false, hashtags)
originalTweet = fixedTweet
}
await tweet(originalTweet, url)
}
})
}
const starWarsCharacterJob = nodeCron.schedule("0 */9 * * *", () => {
try {
starWarsCharacterTweetWorker()
console.log(`job=[starWarsCharacterJob] timestamp=[${new Date().toLocaleString()}]`);
} catch(err) {
console.log(err)
}
});
/**
*
* END CHARACTER
* */
/**
*
* STARSHIP
* */
const generateRandomStarWarsStarship = async (callback) => {
const random = Math.floor(Math.random() * (TOTAL_STARWARS_STARSHIPS_COUNT - 0)) + 0
swapi.get(`https://swapi.dev/api/starships/${random}/`).then((result) => {
// console.log(result);
callback(result)
}).catch((err) => {
console.log(err);
});
}
const starWarsStarshipTweetWorker = async () => {
generateRandomStarWarsStarship(async (starship) => {
// console.log(`starship model=[${starship.model}]`)
if (starship.name && starship.model && starship.starship_class && starship.manufacturer) {
let model = generateRandomModel()
let prompt = `I want to generate the description of an image of an epic star wars starship named ${starship.name}, model ${starship.model} manufacturer ${starship.manufacturer} and starship_class ${starship.starship_class}`
// console.log(prompt);
let description = await generateDescriptionByPrompt(prompt, model, 50);
// console.log(description.data.choices[0].text)
let imageDescription = `An high definition wallpaper image of an star wars starship named ${starship.name}, model ${starship.model}, manufacturer ${starship.manufacturer} portrayed as ${description.data.choices[0].text.replace(/[\r\n]/gm, '')}`
// console.log(imageDescription)
let url = await generateImage(imageDescription);
// console.log(`url=[${url}]`)
let originalTweet = `StarWars starship: ${starship.name} - model ${starship.model} and manufacturer ${starship.manufacturer} desc (by AI): ${description.data.choices[0].text.replace(/[\r\n]/gm, '')} #dalle2 #dalle #openai #swapiapi ${modelToHashtag.get(model)}`
if (!twitterText.parseTweet(originalTweet).valid) {
let startText = `StarWars starship: ${starship.name} - model ${starship.model} and manufacturer ${starship.manufacturer}`
let hashtags = `#dalle2 #dalle #openai #swapiapi ${modelToHashtag.get(model)}`
let variableDescription = description.data.choices[0].text.replace(/[\r\n]/gm, '')
let fixedTweet = fixTweetDescription(originalTweet, startText, variableDescription, false, hashtags)
originalTweet = fixedTweet
}
await tweet(originalTweet, url)
}
})
}
const starWarsStarshipJob = nodeCron.schedule("0 */22 * * *", () => {
try {
starWarsStarshipTweetWorker()
console.log(`job=[starWarsStarshipJob] timestamp=[${new Date().toLocaleString()}]`);
} catch(err) {
console.log(err)
}
});
/**
*
* END STARSHIP
* */
/**
*
* SPECIES
* */
const generateRandomStarWarsSpecies = async (callback) => {
const random = Math.floor(Math.random() * (TOTAL_STARWARS_SPECIES_COUNT - 0)) + 0
swapi.get(`https://swapi.dev/api/species/${random}/`).then((result) => {
// console.log(result);
callback(result)
}).catch((err) => {
console.log(err);
});
}
const starWarsSpeciesTweetWorker = async () => {
generateRandomStarWarsSpecies(async (species) => {
// console.log(`species name=[${species.name}]`)
if (species.name) {
let model = generateRandomModel()
let prompt = `I want to generate the description of an image of an epic star wars species named ${species.name}, classification ${species?.classification}, average height ${species?.average_height}, skin colors ${species?.skin_colors}, hair colors ${species?.hair_colors}, eye colors ${species?.eye_colors}`
// console.log(prompt);
let description = await generateDescriptionByPrompt(prompt, model, 50);
// console.log(description.data.choices[0].text)
let imageDescription = `An high definition wallpaper image of an star wars species named ${species.name}, portrayed as ${description.data.choices[0].text.replace(/[\r\n]/gm, '')}`
// console.log(imageDescription)
let url = await generateImage(imageDescription);
// console.log(`url=[${url}]`)
let originalTweet = `StarWars species: ${species.name} - classification ${species?.classification} desc (by AI): ${description.data.choices[0].text.replace(/[\r\n]/gm, '')} #dalle2 #dalle #openai #swapiapi ${modelToHashtag.get(model)}`
if (!twitterText.parseTweet(originalTweet).valid) {
let startText = `StarWars species: ${species.name} - classification ${species?.classification}`
let hashtags = `#dalle2 #dalle #openai #swapiapi ${modelToHashtag.get(model)}`
let variableDescription = description.data.choices[0].text.replace(/[\r\n]/gm, '')
let fixedTweet = fixTweetDescription(originalTweet, startText, variableDescription, false, hashtags)
originalTweet = fixedTweet
}
await tweet(originalTweet, url)
}
})
}
const starWarsSpeciesJob = nodeCron.schedule("0 */22 * * *", () => {
try {
starWarsSpeciesTweetWorker()
console.log(`job=[starWarsSpeciesJob] timestamp=[${new Date().toLocaleString()}]`);
} catch(err) {
console.log(err)
}
});
/**
*
* END SPECIES
* */
// TELEGRAM
/**
* /start
*
* */
const startMsg = `Bem vindo ao <a href="https://github.com/fcavalcantirj/dalle2-famous-stuff/">DALLE 2 Digital Artist</a> <b>BETA</b>.
Escreva uma mensagem, o mais detalhada possível
Exemplo; <b>an astronaut lounging in a tropical resort in space in a vaporwave style</b>
ou
<b>Um cowboy do espaço em cima de um alien, estilo desenho, arte pitoresca</b>
utilizamos openapi's dalle2 AI para gerar as imagens.
`;
// bot menu
bot.setMyCommands([
{ command: "start", description: "informações sobre o bot" }
]);
// handles start message
bot.onText(/\/start/, (msg) => {
bot.sendMessage(msg.chat.id, startMsg, {parse_mode: 'HTML'});
});
// handles ALL MESSAGES
bot.on('message', async (msg) => {
// console.log(msg);
if (!msg.text) { return; }
// commands handled in other methods
if (!msg.text.includes('/start') && !msg.text.includes('/recentes')
&& !msg.text.includes('/ajuda') && !msg.text.includes('/help')
&& !msg.text.includes('/busca')) {
// console.log(msg.text)
// console.log('@')
// ingore unknown commands
if('/' == msg.text.charAt(0)) { return };
const chatId = msg.chat.id;
// console.log(msg);
let username = '';
if (msg.from.username) {
username = msg.from.username;
} else {
username = `${msg.from.first_name} ${msg.from.last_name}`
}
if (username == 'TrippyPlaces' || username == '𒅱𒌓𒉿𒊩𒈹𒌆𒀸 𒅱𒌓𒉿𒊩𒈹𒌆𒀸') {
console.log(`prompt not handled. username=[${username}] user blocked`)
bot.sendMessage(chatId, `blocked`, {parse_mode: 'HTML'});
return
}
if (username != 'fcavalcantirj' && username != 'fabiomarinho' && username != 'Rone_araujo'
&& username != 'marqssss') {
console.log(`prompt not handled. username=[${username}]`)
bot.sendMessage(chatId, `unlock 50 images by buying me a beer <a href="https://www.buymeacoffee.com/fcavalcantirj"><b>here</b></a>`, {parse_mode: 'HTML'});
return;
}
let searchTerm = msg.text;
// if inside group and @mention...
if ('group' == msg.chat.type && msg.reply_to_message
&& '@dalle2_dart_creator_bot'.toUpperCase() === msg.text.toUpperCase()) {
searchTerm = msg.reply_to_message.text;
} else if ('group' == msg.chat.type) {
// console.log('ignoring group message. only replies');
return;
}
let arr = searchTerm.split('#');
let numberOfResults = 3; // default
if (arr && arr.length > 1) {
numberOfResults = arr[1];
searchTerm = arr[0];
}
try {
bot.sendMessage(chatId, `aguarde, estamos processando sua requisição...`, {parse_mode: 'HTML'});
let imageUrl = await generateImage(searchTerm);
bot.sendMessage(chatId, imageUrl, {parse_mode: 'HTML'});
console.log(`handled prompt=[${searchTerm}] username=[${username}]`)
}
catch(error) {
// console.log(error)
let msg = 'erro gerando a imagem'
if (error.response) {
console.log(`code=[${error.response.status}] error=[${error.response.data.error.message} prompt=[${searchTerm}] username=[${username}]`);
msg = error.response.data.error.message
} else {
console.log(error.message);
msg = error.message
}
bot.sendMessage(chatId, msg, {parse_mode: 'HTML'});
}
}
});