-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathyoutube_automate.py
executable file
·92 lines (75 loc) · 3.51 KB
/
youtube_automate.py
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
import feedparser
import requests
from os.path import exists
import json
import subprocess
import ftplib
import sys
import shutil
import paramiko
config_file = __import__('config')
youtube = __import__('youtube')
show_name = sys.argv[1]
show_config = config_file.podcasts[show_name]
print("Checking for new entries")
cache_path = "./cache_" + show_config["general"]["name"] +".json"
file_exists = exists(cache_path)
playlist_url = show_config["general"]["playlist_url"]
Feed = feedparser.parse(playlist_url)
entries = Feed.entries
new_entry = None
# For dev purposes only
# with open("feed.xml", 'r') as f:
# Feed = feedparser.parse(str(f.read()))
# entries = Feed.entries
differences = False
links = []
for entry in entries:
links.append(entry.link)
if file_exists is False:
print('Cache does not exists')
json_text = json.dumps(links)
with open(cache_path, 'w') as f:
f.write(json_text)
file_exists = True
if file_exists is True:
with open(cache_path, 'r') as f:
json_list = json.loads(str(f.read()))
json_list = json_list
if json_list[0] == links[0]:
print("No changes since last try")
else:
new_entry = links[0]
print("New links : " + str(entry))
if new_entry is not None:
#subprocess.run("python3 botoz3000.py yt-mp3 matinale " + new_entry, shell=True)
youtube.video_to_show(show_config, new_entry)
path = show_config["item"]["guid"] + "_" + show_config["item"]["ep_id"]
mp3 = show_config["general"]["name"] + "/" + path + "/" + show_config["item"]["guid"] + ".mp3"
previous_xml = show_config["general"]["name"] + "/" + path + "/" + show_config["general"]["xml_file_name"] + "-previous.xml"
new_xml = show_config["general"]["name"] + "/" + path + "/" + show_config["general"]["xml_file_name"] + ".xml"
if show_config["general"]["connection_type"] == "FTP":
server = ftplib.FTP()
server.connect(show_config["general"]["ftp_url"])
server.login(show_config["general"]["ftp_login"],show_config["general"]["ftp_password"])
server.storbinary("STOR " + "/" + show_config["general"]["ftp_folder"] + "/" + show_config["item"]["guid"] + ".mp3", open(mp3, 'rb'))
server.storlines("STOR " + "/" + show_config["general"]["ftp_folder"] + "/" + show_config["general"]["xml_file_name"] + "-previous.xml", open(previous_xml, 'rb'))
server.storlines("STOR " + "/" + show_config["general"]["ftp_folder"] + "/" + show_config["general"]["xml_file_name"] + ".xml", open(new_xml, 'rb'))
server.close()
if show_config["general"]["connection_type"] == "SFTP":
host,port = show_config["general"]["sftp_url"],22
transport = paramiko.Transport((host,port))
username,password = show_config["general"]["sftp_login"],show_config["general"]["sftp_password"]
transport.connect(None,username,password)
sftp = paramiko.SFTPClient.from_transport(transport)
sftp.put(mp3, show_config["general"]["sftp_folder"] + "/" + show_config["item"]["guid"] + ".mp3")
sftp.put(previous_xml, show_config["general"]["sftp_folder"] + "/" + show_config["general"]["xml_file_name"] + "-previous.xml")
sftp.put(new_xml, show_config["general"]["sftp_folder"] + "/" + show_config["general"]["xml_file_name"] + ".xml")
sftp.close()
json_text = json.dumps(links)
with open(cache_path, 'w') as f:
f.write(json_text)
try:
shutil.rmtree("./" + show_name)
except OSError as e:
print("Error: %s : %s" % (show_name, e.strerror))