-
Notifications
You must be signed in to change notification settings - Fork 0
/
setup.py
executable file
·120 lines (101 loc) · 4.87 KB
/
setup.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
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
#!/usr/bin/env python3
# this script helps the user set values for the main script.
import urllib.request, urllib.error, urllib.parse
import base64
import simplejson
import subprocess
import os
import glob
import sys
import getpass
import configparser
from datetime import datetime, timedelta
# 'which' & 'is_exe' code snipet via:
# http://stackoverflow.com/questions/377017/test-if-executable-exists-in-python/377028#377028
def is_exe(fpath):
return os.path.isfile(fpath) and os.access(fpath, os.X_OK)
def which(program):
fpath, fname = os.path.split(program)
if fpath:
if is_exe(program):
return program
else:
for path in os.environ["PATH"].split(os.pathsep):
path = path.strip('"')
exe_file = os.path.join(path, program)
if is_exe(exe_file):
return exe_file
return None
print("This script helps set up important values and preferances for the TWL_Downloader.")
print(" --")
print("Please make sure you have installed the latest copy of youtube-dl before running this setup script.")
print("youtube-dl is available via: http://rg3.github.io/youtube-dl/", end=' ')
if sys.platform == 'darwin':
print("or via Homebrew at: http://brew.sh (reccomended for Mac users).")
else:
print('')
print('')
foundPathToYouTubeDL = which('youtube-dl')
if foundPathToYouTubeDL:
print("Found a copy of youtube-dl at '%s'. Press enter below to accept this default value." % foundPathToYouTubeDL)
pathToYouTubeDL = input("Path to youtube-dl [%s]: " % foundPathToYouTubeDL)
if not pathToYouTubeDL:
pathToYouTubeDL = foundPathToYouTubeDL
else:
print("You'll also need to know the path to its binary (e.g. '/usr/local/bin/youtube-dl').")
pathToYouTubeDL = input("Path to youtube-dl: ")
if not is_exe(pathToYouTubeDL):
sys.exit("ERROR: The path you provided '%s' doesn't appear to be a valid executable.\nPlease check that the path resolves and that the permission are set to allow execution." % pathToYouTubeDL)
print("Great, things look good with that path.")
print('')
print("For security, your email address and password will not be permanently stored.\nHowever, these values are need to look up your API key.")
username = input("ToWatchList.com Email Address: ")
password = getpass.getpass("ToWatchList.com Password: ")
TWL_API_URL = "https://towatchlist.com/api/getkey"
# create the API request
request = urllib.request.Request(TWL_API_URL)
# add authorization
base64string = base64.encodestring('%s:%s' % (username, password)).replace('\n', '')
request.add_header("Authorization", "Basic %s" % base64string)
# perform the request
result = urllib.request.urlopen(request)
# array of the results
myKey = simplejson.loads(result.read())
if myKey['result'] != '1':
sys.exit("ERROR: Bad username/password combination.")
apiKey = myKey['key']
print("Super: we've got your current API key.")
print('')
print("Finally, the script needs to know where you want your video downloads to be saved.")
print("The default behavior puts the files in the current working directory (CWD or PWD) when executed.")
print("However, you can change this default by specifying another path below (press enter with no value to accept the defaults).")
doneFlag = False
while not doneFlag:
downloadLocation = input("Download Path [null for default]: ")
# expand tilde for user paths
downloadLocation = os.path.expanduser(downloadLocation)
if downloadLocation:
if os.path.isdir(downloadLocation):
doneFlag = True
print("Okay, twl_downloader.py will download videos to '%s'." % downloadLocation)
else:
print("The location specified ('%s') doesn't look like a valid directory. Please try again." % downloadLocation)
else:
downloadLocation = 'False'
doneFlag = True
print("Okay, twl_downloader.py will use the current path when downloading videos.")
savepath = os.path.expanduser('~/.twl_downloader_settings.cfg')
# Set up config file
config = configparser.RawConfigParser()
config.add_section('twl_downloader_settings')
config.set('twl_downloader_settings', 'apiKey', apiKey)
config.set('twl_downloader_settings', 'pathToYouTubeDL', pathToYouTubeDL)
config.set('twl_downloader_settings', 'downloadLocation', downloadLocation)
# date of -48 hours
config.set('twl_downloader_settings', 'refreshTime', datetime.utcnow()-timedelta(days=2))
# Writing our config file
with open(savepath, 'wb') as configfile:
config.write(configfile)
print('')
print("Thanks for setting up TWL_Downloader for ToWatchList!\nYour settings have been saved under '%s'.\nIf you need to change any values may edit them there or run this setup program again.\n" % savepath)
print("Finally, please be aware that TWL_Downloader uses an officially unsupported API which may change anytime.\nSee https://github.com/ToWatchList/TWL_Downloader and http://towatchlist.com/api for details.")