Youtube music and video downloader

links.js 1.2KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. import { Meteor } from 'meteor/meteor';
  2. import { Mongo } from 'meteor/mongo';
  3. import { check, Match } from 'meteor/check';
  4. import { convertVideo, convertAudio} from './converter';
  5. export const Converted = new Mongo.Collection('links');
  6. const YTExp = /http(?:s?):\/\/(?:www\.)?youtu(?:be\.com\/watch\?v=|\.be\/)([\w\-_]*)/;
  7. if (Meteor.isServer) {
  8. Meteor.publish('links', () => Converted.find());
  9. }
  10. Meteor.methods({
  11. 'links.add'({video, type}) {
  12. check(video.url, Match.Where(url => YTExp.test(url)));
  13. check(type, Match.Where(t => ['audio'].includes(t)));
  14. let converted = Converted.findOne({ id: video.id });
  15. if (!converted) {
  16. let inserted_id = Converted.insert({
  17. id: video.id, url: video.url,
  18. title: video.title, createdAt: new Date()
  19. });
  20. converted = Converted.findOne({_id: inserted_id });
  21. }
  22. if (!converted.audio) {
  23. Converted.update({ _id: converted._id }, { $set: { audio: '', error_audio: '', audio_progress: 0 } });
  24. convertAudio(converted.url, converted.title.replace(/ /g, '_'), converted._id);
  25. }
  26. if (!converted.video) {
  27. Converted.update({ _id: converted._id }, { $set: { video: '', error_video: '', video_progress: 0 } });
  28. convertVideo(converted.url, converted.title.replace(/ /g, '_'), converted._id);
  29. }
  30. },
  31. });