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
|
from urllib import request import datetime from rfeed import * import re import os from glob import glob import feedparser import pprint from os import rename import youtube_dl
def save_to_file(file_name, contents): fh = open(file_name, 'w') fh.write(contents) fh.close()
def audio_download(youtube_url, file_root, only_audio=False): ydl_opts = { 'format': 'best', 'outtmpl': file_root+'/%(id)s.%(ext)s', } ydl_opts_a = { 'format': 'bestaudio/best', 'outtmpl': file_root+'/%(id)s.%(ext)s', 'postprocessors': [{ 'key': 'FFmpegExtractAudio', 'preferredcodec': 'mp3', 'preferredquality': '192', }],
} if only_audio: ydl_opts = ydl_opts_a with youtube_dl.YoutubeDL(ydl_opts) as ydl: ydl.download([youtube_url])
class YoutubeDowner: def __init__(self, yurl, burl, file_root, feed_title, only_audio=False): self.yurl = yurl self.burl = burl self.file_root = file_root self.feed_title = feed_title self.feed_des = feed_title self.only_audio = only_audio self.type_name = "mp4" if only_audio: self.type_name = "mp3"
rss = feedparser.parse(yurl) self.entries = rss['entries'][:3] if not os.path.exists(file_root): os.makedirs(file_root) self.generate_xml()
self.down()
def generate_xml(self): item_list = [] for entry in self.entries: print(entry['title'],entry['published'],entry['link']) date_s = entry['published'].split('T')[0].split('-') dates = [int(x) for x in date_s] new_link = self.burl + "/%s.%s" % (entry['yt_videoid'],self.type_name) print('%s: <a href="%s">%s</a> <pre>%s</pre>' % (self.type_name,new_link,self.type_name,entry['summary'])) _one = Item( title = entry['title'], link = new_link, description = '%s: <a href="%s">%s</a> <pre>%s</pre>' % (self.type_name,new_link,self.type_name,entry['summary']), author = "Youtube", guid = Guid(new_link), pubDate = datetime.datetime(dates[0], dates[1], dates[2], 6, 0)) item_list+=[_one]
feed = Feed( title = self.feed_title, link = "https://www.xxxxx.biz/atom/updated.xml", description = self.feed_des, language = "en-US", lastBuildDate = datetime.datetime.now(), items = item_list)
save_to_file('%s.xml' % self.file_root, feed.rss()) def down(self): for entry in self.entries: if entry['yt_videoid']+'.'+self.type_name not in os.listdir(self.file_root): print("downing %s, %s"%(entry['title'], entry['link'])) audio_download(entry['link'], self.file_root, self.only_audio) _now = sorted(glob(self.file_root + "/*"),key=os.path.getctime) print(_now) if len(_now)>=6: for _d in _now[:-5]: os.popen("rm -rf %s"%_d)
|