commit
65c5810cc9
@ -0,0 +1,45 @@
|
||||
[reddit.creds]
|
||||
client_id = { optional = false, nmin = 12, nmax = 30, explanation = "the ID of your Reddit app of SCRIPT type", example = "fFAGRNJru1FTz70BzhT3Zg", regex = "^[-a-zA-Z0-9._~+/]+=*$", input_error = "The client ID can only contain printable characters.", oob_error = "The ID should be over 12 and under 30 characters, double check your input." }
|
||||
client_secret = { optional = false, nmin = 20, nmax = 40, explanation = "the SECRET of your Reddit app of SCRIPT type", example = "fFAGRNJru1FTz70BzhT3Zg", regex = "^[-a-zA-Z0-9._~+/]+=*$", input_error = "The client ID can only contain printable characters.", oob_error = "The secret should be over 20 and under 40 characters, double check your input." }
|
||||
username = { optional = false, nmin = 3, nmax = 20, explanation = "the username of your reddit account", example = "JasonLovesDoggo", regex = "^[-_0-9a-zA-Z]+$", oob_error = "A username HAS to be between 3 and 20 characters" }
|
||||
password = { optional = false, nmin = 8, explanation = "the password of your reddit account", example = "fFAGRNJru1FTz70BzhT3Zg", oob_error = "Password too short" }
|
||||
2fa = { optional = true, type = "bool", options = [true,
|
||||
false,
|
||||
], default = false, explanation = "Whether you have Reddit 2FA enabled, Valid options are True and False", example = true }
|
||||
|
||||
|
||||
[reddit.thread]
|
||||
random = { optional = true, options = [true,
|
||||
false,
|
||||
], default = false, type = "bool", explanation = "If set to no, it will ask you a thread link to extract the thread, if yes it will randomize it. Default: 'False'", example = "True" }
|
||||
subreddit = { optional = false, regex = "[_0-9a-zA-Z]+$", nmin = 3, explanation = "what subreddit to pull posts from, the name of the sub, not the URL", example = "AskReddit", oob_error = "A subreddit name HAS to be between 3 and 20 characters" }
|
||||
post_id = { optional = true, default = "", regex = "^((?!://|://)[+a-zA-Z])*$", explanation = "Used if you want to use a specific post.", example = "urdtfx" }
|
||||
max_comment_length = { default = 500, optional = false, nmin = 10, nmax = 10000, type = "int", explanation = "max number of characters a comment can have. default is 500", example = 500, oob_error = "the max comment length should be between 10 and 10000" }
|
||||
post_lang = { default = "", optional = true, explanation = "The language you would like to translate to.", example = "es-cr" }
|
||||
min_comments = { default = 20, optional = false, nmin = 15, type = "int", explanation = "The minimum number of comments a post should have to be included. default is 20", example = 29, oob_error = "the minimum number of comments should be between 15 and 999999" }
|
||||
[settings]
|
||||
allow_nsfw = { optional = false, type = "bool", default = false, example = false, options = [true,
|
||||
false,
|
||||
], explanation = "Whether to allow NSFW content, True or False" }
|
||||
theme = { optional = false, default = "dark", example = "light", options = ["dark",
|
||||
"light",
|
||||
], explanation = "sets the Reddit theme, either LIGHT or DARK" }
|
||||
times_to_run = { optional = false, default = 1, example = 2, explanation = "used if you want to run multiple times. set to an int e.g. 4 or 29 or 1", type = "int", nmin = 1, oob_error = "It's very hard to run something less than once." }
|
||||
opacity = { optional = false, default = 0.9, example = 0.8, explanation = "Sets the opacity of the comments when overlayed over the background", type = "float", nmin = 0, nmax = 1, oob_error = "The opacity HAS to be between 0 and 1", input_error = "The opacity HAS to be a decimal number between 0 and 1" }
|
||||
storymode = { optional = true, type = "bool", default = false, example = false, options = [true,
|
||||
false,
|
||||
], explanation = "not yet implemented" }
|
||||
|
||||
[settings.background]
|
||||
background_choice = { optional = true, default = "minecraft", example = "minecraft", options = ["minecraft", "gta", "rocket-league", "motor-gta", ""], explanation = "Sets the background for the video" }
|
||||
#background_audio = { optional = true, type = "bool", default = false, example = false, options = [true,
|
||||
# false,
|
||||
#], explaination="Sets a audio to play in the background (put a background.mp3 file in the assets/backgrounds directory for it to be used.)" }
|
||||
#background_audio_volume = { optional = true, type = "float", default = 0.3, example = 0.1, explanation="Sets the volume of the background audio. only used if the background_audio is also set to true" }
|
||||
|
||||
|
||||
[settings.tts]
|
||||
choice = { optional = false, default = "", options = ["streamlabspolly", "tiktok", "googletranslate", "awspolly", ], example = "streamlabspolly", explanation = "The backend used for TTS generation. This can be left blank and you will be prompted to choose at runtime." }
|
||||
aws_polly_voice = { optional = false, default = "Matthew", example = "Matthew", explanation = "The voice used for AWS Polly" }
|
||||
streamlabs_polly_voice = { optional = false, default = "Matthew", example = "Matthew", explanation = "The voice used for Streamlabs Polly" }
|
||||
tiktok_voice = { optional = false, default = "en_us_006", example = "en_us_006", explanation = "The voice used for TikTok TTS" }
|
@ -1,86 +0,0 @@
|
||||
|
||||
REDDIT_CLIENT_ID="" #fFAGRNJru1FTz70BzhT3Zg
|
||||
#EXPLANATION the ID of your Reddit app of SCRIPT type
|
||||
#RANGE 12:30
|
||||
#MATCH_REGEX [-a-zA-Z0-9._~+/]+=*$
|
||||
#OOB_ERROR The ID should be over 12 and under 30 characters, double check your input.
|
||||
|
||||
REDDIT_CLIENT_SECRET="" #fFAGRNJru1FTz70BzhT3Zg
|
||||
#EXPLANATION the SECRET of your Reddit app of SCRIPT type
|
||||
#RANGE 20:40
|
||||
#MATCH_REGEX [-a-zA-Z0-9._~+/]+=*$
|
||||
#OOB_ERROR The secret should be over 20 and under 40 characters, double check your input.
|
||||
|
||||
REDDIT_USERNAME="" #asdfghjkl
|
||||
#EXPLANATION the username of your reddit account
|
||||
#RANGE 3:20
|
||||
#MATCH_REGEX [_0-9a-zA-Z]+$
|
||||
#OOB_ERROR A username HAS to be between 3 and 20 characters
|
||||
|
||||
REDDIT_PASSWORD="" #fFAGRNJru1FTz70BzhT3Zg
|
||||
#EXPLANATION the password of your reddit account
|
||||
#RANGE 8:None
|
||||
#OOB_ERROR Password too short
|
||||
|
||||
#OPTIONAL
|
||||
RANDOM_THREAD="no"
|
||||
# If set to no, it will ask you a thread link to extract the thread, if yes it will randomize it. Default: "no"
|
||||
|
||||
REDDIT_2FA="" #no
|
||||
#MATCH_REGEX ^(yes|no)
|
||||
#EXPLANATION Whether you have Reddit 2FA enabled, Valid options are "yes" and "no"
|
||||
|
||||
SUBREDDIT="AskReddit"
|
||||
#EXPLANATION what subreddit to pull posts from, the name of the sub, not the URL
|
||||
#RANGE 3:20
|
||||
#MATCH_REGEX [_0-9a-zA-Z]+$
|
||||
#OOB_ERROR A subreddit name HAS to be between 3 and 20 characters
|
||||
|
||||
ALLOW_NSFW="False"
|
||||
#EXPLANATION Whether to allow NSFW content, True or False
|
||||
#MATCH_REGEX ^(True|False)$
|
||||
|
||||
POST_ID=""
|
||||
#MATCH_REGEX ^((?!://|://).)*$
|
||||
#EXPLANATION Used if you want to use a specific post. example of one is urdtfx
|
||||
|
||||
THEME="LIGHT" #dark
|
||||
#EXPLANATION sets the Reddit theme, either LIGHT or DARK
|
||||
#MATCH_REGEX ^(dark|light|DARK|LIGHT)$
|
||||
|
||||
TIMES_TO_RUN="" #2
|
||||
#EXPLANATION used if you want to run multiple times. set to an int e.g. 4 or 29 and leave blank for 1
|
||||
|
||||
MAX_COMMENT_LENGTH="500" #500
|
||||
#EXPLANATION max number of characters a comment can have. default is 500
|
||||
#RANGE 0:10000
|
||||
#MATCH_TYPE int
|
||||
#OOB_ERROR the max comment length should be between 0 and 10000
|
||||
|
||||
OPACITY="1" #.8
|
||||
#EXPLANATION Sets the opacity of the comments when overlayed over the background
|
||||
#RANGE 0:1
|
||||
#MATCH_TYPE float
|
||||
#OOB_ERROR The opacity HAS to be between 0 and 1
|
||||
|
||||
# If you want to translate the comments to another language, set the language code here.
|
||||
# If empty, no translation will be done.
|
||||
POSTLANG=""
|
||||
#EXPLANATION Activates the translation feature, set the language code for translate or leave blank
|
||||
|
||||
TTSCHOICE="Polly"
|
||||
#EXPLANATION the backend used for TTS. Without anything specified, the user will be prompted to choose one.
|
||||
# IMPORTANT NOTE: if you use translate, you need to set this to googletranslate or tiktok and use custom voice in your language
|
||||
|
||||
STREAMLABS_VOICE="Joanna"
|
||||
#EXPLANATION Sets the voice for the Streamlabs Polly TTS Engine. Check the file for more information on different voices.
|
||||
|
||||
AWS_VOICE="Joanna"
|
||||
#EXPLANATION Sets the voice for the AWS Polly TTS Engine. Check the file for more information on different voices.
|
||||
|
||||
TIKTOK_VOICE="en_us_006"
|
||||
#EXPLANATION Sets the voice for the TikTok TTS Engine. Check the file for more information on different voices.
|
||||
|
||||
#OPTIONAL
|
||||
STORYMODE="False"
|
||||
# IN-PROGRESS - not yet implemented
|
@ -0,0 +1,32 @@
|
||||
# GitHub Action that uses Black to reformat the Python code in an incoming pull request.
|
||||
# If all Python code in the pull request is compliant with Black then this Action does nothing.
|
||||
# Othewrwise, Black is run and its changes are committed back to the incoming pull request.
|
||||
# https://github.com/cclauss/autoblack
|
||||
|
||||
name: autoblack
|
||||
on:
|
||||
push:
|
||||
branches: ["master"]
|
||||
jobs:
|
||||
build:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v1
|
||||
- name: Set up Python 3.7
|
||||
uses: actions/setup-python@v1
|
||||
with:
|
||||
python-version: 3.9
|
||||
- name: Install Black
|
||||
run: pip install black
|
||||
- name: Run black --check .
|
||||
run: black --check .
|
||||
- name: If needed, commit black changes to the pull request
|
||||
if: failure()
|
||||
run: |
|
||||
black . --line-length 101
|
||||
git config --global user.name 'autoblack'
|
||||
git config --global user.email 'jasoncameron.all@gmail.com'
|
||||
git remote set-url origin https://x-access-token:${{ secrets.GITHUB_TOKEN }}@github.com/$GITHUB_REPOSITORY
|
||||
git checkout $GITHUB_HEAD_REF
|
||||
git commit -am "fixup: Format Python code with Black"
|
||||
git push
|
@ -0,0 +1,12 @@
|
||||
name: Lint
|
||||
|
||||
on: [pull_request]
|
||||
|
||||
jobs:
|
||||
lint:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v2
|
||||
- uses: psf/black@stable
|
||||
with:
|
||||
options: "--line-length 101"
|
@ -0,0 +1,281 @@
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="utf-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1">
|
||||
<meta http-equiv="cache-control" content="no-cache"/>
|
||||
<title>RedditVideoMakerBot</title>
|
||||
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4.1.3/dist/css/bootstrap.min.css" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous">
|
||||
<link href="https://getbootstrap.com/docs/5.2/dist/css/bootstrap.min.css" rel="stylesheet">
|
||||
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.8.3/font/bootstrap-icons.css">
|
||||
|
||||
<style>
|
||||
.bd-placeholder-img {
|
||||
font-size: 1.125rem;
|
||||
text-anchor: middle;
|
||||
-webkit-user-select: none;
|
||||
-moz-user-select: none;
|
||||
user-select: none;
|
||||
}
|
||||
|
||||
@media (min-width: 768px) {
|
||||
.bd-placeholder-img-lg {
|
||||
font-size: 3.5rem;
|
||||
}
|
||||
}
|
||||
|
||||
.b-example-divider {
|
||||
height: 3rem;
|
||||
background-color: rgba(0, 0, 0, .1);
|
||||
border: solid rgba(0, 0, 0, .15);
|
||||
border-width: 1px 0;
|
||||
box-shadow: inset 0 .5em 1.5em rgba(0, 0, 0, .1), inset 0 .125em .5em rgba(0, 0, 0, .15);
|
||||
}
|
||||
|
||||
.b-example-vr {
|
||||
flex-shrink: 0;
|
||||
width: 1.5rem;
|
||||
height: 100vh;
|
||||
}
|
||||
|
||||
.bi {
|
||||
vertical-align: -.125em;
|
||||
fill: currentColor;
|
||||
}
|
||||
|
||||
.nav-scroller {
|
||||
position: relative;
|
||||
z-index: 2;
|
||||
height: 2.75rem;
|
||||
overflow-y: hidden;
|
||||
}
|
||||
|
||||
.nav-scroller .nav {
|
||||
display: flex;
|
||||
flex-wrap: nowrap;
|
||||
padding-bottom: 1rem;
|
||||
margin-top: -1px;
|
||||
overflow-x: auto;
|
||||
text-align: center;
|
||||
white-space: nowrap;
|
||||
-webkit-overflow-scrolling: touch;
|
||||
}
|
||||
|
||||
#tooltip {
|
||||
background-color: #333;
|
||||
color: white;
|
||||
padding: 5px 10px;
|
||||
border-radius: 4px;
|
||||
font-size: 13px;
|
||||
}
|
||||
</style>
|
||||
</head>
|
||||
|
||||
<body>
|
||||
|
||||
<header>
|
||||
<div class="navbar navbar-dark bg-dark shadow-sm">
|
||||
<div class="container">
|
||||
<a href="#" class="navbar-brand d-flex align-items-center">
|
||||
<svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" fill="none" stroke="currentColor" stroke-linecap="round" stroke-linejoin="round" stroke-width="2" aria-hidden="true" class="me-2" viewBox="0 0 24 24"><path d="M23 19a2 2 0 0 1-2 2H3a2 2 0 0 1-2-2V8a2 2 0 0 1 2-2h4l2-3h6l2 3h4a2 2 0 0 1 2 2z"/><circle cx="12" cy="13" r="4"/></svg>
|
||||
<strong>RedditVideoMakerBot</strong>
|
||||
</a>
|
||||
</div>
|
||||
</div>
|
||||
</header>
|
||||
|
||||
<main>
|
||||
<div class="album py-2 bg-light">
|
||||
<div class="container">
|
||||
|
||||
<div class="row mt-2">
|
||||
<div class="col-12 col-md-3 mb-3">
|
||||
<input type="text" class="form-control" placeholder="Search videos" aria-label="Search videos" onkeyup="searchFilter()">
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="grid row row-cols-1 row-cols-sm-2 row-cols-md-3 g-3" id="videos">
|
||||
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</main>
|
||||
|
||||
<footer class="text-muted py-5">
|
||||
<div class="container">
|
||||
<p class="float-end mb-1">
|
||||
<a href="#">Back to top</a>
|
||||
</p>
|
||||
<p class="mb-1"><a href="https://getbootstrap.com/docs/5.2/examples/album/" target="_blank">Album</a> Example Theme by © Bootstrap. <a href="https://github.com/elebumm/RedditVideoMakerBot/blob/master/README.md#developers-and-maintainers" target="_blank">Developers and Maintainers</a></p>
|
||||
<p class="mb-0">If your data is not refreshing, try to hard reload(Ctrl + F5) and visit your local <a href="../video_creation/data/videos.json" target="_blank">videos.json</a> file.</p>
|
||||
</div>
|
||||
</footer>
|
||||
|
||||
<script src="https://code.jquery.com/jquery-3.1.1.js" integrity="sha256-16cdPddA6VdVInumRGo6IbivbERE8p7CQR3HzTBuELA=" crossorigin="anonymous"></script>
|
||||
<script src="https://cdn.jsdelivr.net/npm/popper.js@1.14.3/dist/umd/popper.min.js" integrity="sha384-ZMP7rVo3mIykV+2+9J3UJ46jBk0WLaUAdn689aCwoqbBJiSnjAK/l8WvCWPIPm49" crossorigin="anonymous"></script>
|
||||
<script src="https://cdn.jsdelivr.net/npm/bootstrap@4.1.3/dist/js/bootstrap.min.js" integrity="sha384-ChfqqxuZUCnJSK3+MXmPNIyE6ZbWh2IMqE241rYiqJxyMiZ6OW/JmZQ5stwEULTy" crossorigin="anonymous"></script>
|
||||
<script src="https://cdnjs.cloudflare.com/ajax/libs/clipboard.js/2.0.10/clipboard.min.js"></script>
|
||||
<script src="https://unpkg.com/isotope-layout@3/dist/isotope.pkgd.js"></script>
|
||||
|
||||
<script>
|
||||
const intervals = [
|
||||
{ label: 'year', seconds: 31536000 },
|
||||
{ label: 'month', seconds: 2592000 },
|
||||
{ label: 'day', seconds: 86400 },
|
||||
{ label: 'hour', seconds: 3600 },
|
||||
{ label: 'minute', seconds: 60 },
|
||||
{ label: 'second', seconds: 1 }
|
||||
];
|
||||
|
||||
function timeSince(date) {
|
||||
const seconds = Math.floor((Date.now() / 1000 - date));
|
||||
const interval = intervals.find(i => i.seconds < seconds);
|
||||
const count = Math.floor(seconds / interval.seconds);
|
||||
return `${count} ${interval.label}${count !== 1 ? 's' : ''} ago`;
|
||||
}
|
||||
|
||||
$(document).ready(function () {
|
||||
$.getJSON("../video_creation/data/videos.json",
|
||||
function (data) {
|
||||
data.sort((b, a) => a['time'] - b['time'])
|
||||
var video = '';
|
||||
$.each(data, function (key, value) {
|
||||
|
||||
video += '<div class="col">';
|
||||
video += '<div class="card shadow-sm">';
|
||||
//keeping original themed image card for future thumbnail usage video += '<svg class="bd-placeholder-img card-img-top" width="100%" height="225" xmlns="http://www.w3.org/2000/svg" role="img" aria-label="Placeholder: Thumbnail" preserveAspectRatio="xMidYMid slice" focusable="false"><title>Placeholder</title><rect width="100%" height="100%" fill="#55595c"/><text x="50%" y="50%" fill="#eceeef" dy=".3em">r/'+value.subreddit+'</text></svg>';
|
||||
|
||||
video += '<div class="card-body">';
|
||||
video += '<p class="card-text">r/'+value.subreddit+' • '+checkTitle(value.reddit_title, value.filename)+'</p>';
|
||||
video += '<div class="d-flex justify-content-between align-items-center">';
|
||||
video += '<div class="btn-group">';
|
||||
video += '<a href="https://www.reddit.com/r/'+value.subreddit+'/comments/'+value.id+'/" class="btn btn-sm btn-outline-secondary" target="_blank">View</a>';
|
||||
video += '<a href="results/'+value.subreddit+'/'+value.filename+'" class="btn btn-sm btn-outline-secondary" download>Download</a>';
|
||||
video += '</div>';
|
||||
video += '<div class="btn-group">';
|
||||
video += '<button type="button" data-toggle="tooltip" id="copy" data-original-title="Copy to clipboard" class="btn btn-sm btn-outline-secondary" data-clipboard-text="'+getCopyData(value.subreddit, value.reddit_title, value.filename, value.background_credit)+'"><i class="bi bi-card-text"></i></button>';
|
||||
video += '<button type="button" data-toggle="tooltip" id="copy" data-original-title="Copy to clipboard" class="btn btn-sm btn-outline-secondary" data-clipboard-text="'+checkTitle(value.reddit_title, value.filename)+' #Shorts #reddit"><i class="bi bi-youtube"></i></button>';
|
||||
video += '<button type="button" data-toggle="tooltip" id="copy" data-original-title="Copy to clipboard" class="btn btn-sm btn-outline-secondary" data-clipboard-text="'+checkTitle(value.reddit_title, value.filename)+' #reddit"><i class="bi bi-instagram"></i></button>';
|
||||
video += '</div>';
|
||||
video += '<small class="text-muted">'+timeSince(value.time)+'</small>';
|
||||
video += '</div>';
|
||||
video += '</div>';
|
||||
video += '</div>';
|
||||
video += '</div>';
|
||||
|
||||
});
|
||||
|
||||
$('#videos').append(video);
|
||||
});
|
||||
});
|
||||
|
||||
$(document).ready(function(){
|
||||
$('[data-toggle="tooltip"]').tooltip();
|
||||
$('[data-toggle="tooltip"]').on('click', function(){
|
||||
$(this).tooltip('hide');
|
||||
});
|
||||
});
|
||||
|
||||
$('#copy').tooltip({
|
||||
trigger: 'click',
|
||||
placement: 'bottom'
|
||||
});
|
||||
|
||||
function setTooltip(btn, message) {
|
||||
$(btn).tooltip('hide')
|
||||
.attr('data-original-title', message)
|
||||
.tooltip('show');
|
||||
}
|
||||
|
||||
function hoverTooltip(btn, message) {
|
||||
$(btn).tooltip('hide')
|
||||
.attr('data-original-title', message)
|
||||
.tooltip('show');
|
||||
}
|
||||
|
||||
function hideTooltip(btn) {
|
||||
setTimeout(function() {
|
||||
$(btn).tooltip('hide');
|
||||
}, 1000);
|
||||
}
|
||||
|
||||
function disposeTooltip(btn) {
|
||||
setTimeout(function() {
|
||||
$(btn).tooltip('dispose');
|
||||
}, 1500);
|
||||
}
|
||||
|
||||
var clipboard = new ClipboardJS('#copy');
|
||||
|
||||
clipboard.on('success', function(e) {
|
||||
e.clearSelection();
|
||||
console.info('Action:', e.action);
|
||||
console.info('Text:', e.text);
|
||||
console.info('Trigger:', e.trigger);
|
||||
setTooltip(e.trigger, 'Copied!');
|
||||
hideTooltip(e.trigger);
|
||||
disposeTooltip(e.trigger);
|
||||
});
|
||||
|
||||
clipboard.on('error', function(e) {
|
||||
console.error('Action:', e.action);
|
||||
console.error('Trigger:', e.trigger);
|
||||
setTooltip(e.trigger, fallbackMessage(e.action));
|
||||
hideTooltip(e.trigger);
|
||||
});
|
||||
|
||||
function getCopyData(subreddit, reddit_title, filename, background_credit){
|
||||
|
||||
if (subreddit == undefined) {
|
||||
subredditCopy = "";
|
||||
} else {
|
||||
subredditCopy = "r/" + subreddit + "\n\n";
|
||||
}
|
||||
|
||||
const file = filename.slice(0, -4);
|
||||
if (reddit_title == file) {
|
||||
titleCopy = reddit_title;
|
||||
} else {
|
||||
titleCopy = file;
|
||||
}
|
||||
|
||||
var copyData = "";
|
||||
copyData += subredditCopy;
|
||||
copyData += titleCopy;
|
||||
copyData += "\n\nBackground credit: " + background_credit;
|
||||
return copyData;
|
||||
}
|
||||
|
||||
function getLink(subreddit, id, reddit_title) {
|
||||
if (subreddit == undefined) {
|
||||
return reddit_title;
|
||||
} else {
|
||||
return "<a target='_blank' href='https://www.reddit.com/r/" + subreddit + "/comments/" + id + "/'>"+ reddit_title +"</a>";
|
||||
}
|
||||
}
|
||||
|
||||
function checkTitle(reddit_title, filename) {
|
||||
const file = filename.slice(0, -4);
|
||||
if (reddit_title == file) {
|
||||
return reddit_title;
|
||||
} else {
|
||||
return file;
|
||||
}
|
||||
}
|
||||
|
||||
var searchFilter = () => {
|
||||
const input = document.querySelector(".form-control");
|
||||
const cards = document.getElementsByClassName("col");
|
||||
console.log(cards[1])
|
||||
let filter = input.value
|
||||
for (let i = 0; i < cards.length; i++) {
|
||||
let title = cards[i].querySelector(".card-text");
|
||||
if (title.innerText.toLowerCase().indexOf(filter.toLowerCase()) > -1) {
|
||||
cards[i].classList.remove("d-none")
|
||||
} else {
|
||||
cards[i].classList.add("d-none")
|
||||
}
|
||||
}
|
||||
}
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
@ -0,0 +1,223 @@
|
||||
#!/bin/bash
|
||||
|
||||
# If the install fails, then print an error and exit.
|
||||
function install_fail() {
|
||||
echo "Installation failed"
|
||||
exit 1
|
||||
}
|
||||
|
||||
# This is the help fuction. It helps users withe the options
|
||||
function Help(){
|
||||
echo "Usage: install.sh [option]"
|
||||
echo "Options:"
|
||||
echo " -h: Show this help message and exit"
|
||||
echo " -d: Install only dependencies"
|
||||
echo " -p: Install only python dependencies (including playwright)"
|
||||
echo " -b: Install just the bot"
|
||||
echo " -l: Install the bot and the python dependencies"
|
||||
}
|
||||
|
||||
# Options
|
||||
while getopts ":hydpbl" option; do
|
||||
case $option in
|
||||
# -h, prints help message
|
||||
h)
|
||||
Help exit 0;;
|
||||
# -y, assumes yes
|
||||
y)
|
||||
ASSUME_YES=1;;
|
||||
# -d install only dependencies
|
||||
d)
|
||||
DEPS_ONLY=1;;
|
||||
# -p install only python dependencies
|
||||
p)
|
||||
PYTHON_ONLY=1;;
|
||||
b)
|
||||
JUST_BOT=1;;
|
||||
l)
|
||||
BOT_AND_PYTHON=1;;
|
||||
# if a bad argument is given, then throw an error
|
||||
\?)
|
||||
echo "Invalid option: -$OPTARG" >&2 Help exit 1;;
|
||||
:)
|
||||
echo "Option -$OPTARG requires an argument." >&2 Help exit 1;;
|
||||
esac
|
||||
done
|
||||
|
||||
# Install dependencies for MacOS
|
||||
function install_macos(){
|
||||
# Check if homebrew is installed
|
||||
if [ ! command -v brew &> /dev/null ]; then
|
||||
echo "Installing Homebrew"
|
||||
# if it's is not installed, then install it in a NONINTERACTIVE way
|
||||
NONINTERACTIVE=1 /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/uninstall.sh)"
|
||||
# Check for what arcitecture, so you can place path.
|
||||
if [[ "uname -m" == "x86_64" ]]; then
|
||||
echo "export PATH=/usr/local/bin:$PATH" >> ~/.bash_profile && source ~/.bash_profile
|
||||
fi
|
||||
# If not
|
||||
else
|
||||
# Print that it's already installed
|
||||
echo "Homebrew is already installed"
|
||||
fi
|
||||
# Install the required packages
|
||||
echo "Installing required Packages"
|
||||
brew install python@3.10 tcl-tk python-tk
|
||||
}
|
||||
|
||||
# Function to install for arch (and other forks like manjaro)
|
||||
function install_arch(){
|
||||
echo "Installing required packages"
|
||||
sudo pacman -S --needed python3 tk git && python3 -m ensurepip || install_fail
|
||||
}
|
||||
|
||||
# Function to install for debian (and ubuntu)
|
||||
function install_deb(){
|
||||
echo "Installing required packages"
|
||||
sudo apt install python3 python3-dev python3-tk python3-pip git || install_fail
|
||||
}
|
||||
|
||||
# Function to install for fedora (and other forks)
|
||||
function install_fedora(){
|
||||
echo "Installing required packages"
|
||||
sudo dnf install python3 python3-tkinter python3-pip git python3-devel || install_fail
|
||||
}
|
||||
|
||||
# Function to install for centos (and other forks based on it)
|
||||
function install_centos(){
|
||||
echo "Installing required packages"
|
||||
sudo yum install -y python3 || install_fail
|
||||
sudo yum install -y python3-tkinter epel-release python3-pip git || install_fail
|
||||
}
|
||||
|
||||
function get_the_bot(){
|
||||
echo "Downloading the bot"
|
||||
git clone https://github.com/elebumm/RedditVideoMakerBot.git
|
||||
}
|
||||
|
||||
#install python dependencies
|
||||
function install_python_dep(){
|
||||
# tell the user that the script is going to install the python dependencies
|
||||
echo "Installing python dependencies"
|
||||
# cd into the directory
|
||||
cd RedditVideoMakerBot
|
||||
# install the dependencies
|
||||
pip3 install -r requirements.txt
|
||||
# cd out
|
||||
cd ..
|
||||
}
|
||||
|
||||
# install playwright function
|
||||
function install_playwright(){
|
||||
# tell the user that the script is going to install playwright
|
||||
echo "Installing playwright"
|
||||
# cd into the directory where the script is downloaded
|
||||
cd RedditVideoMakerBot
|
||||
# run the install script
|
||||
python3 -m playwright install
|
||||
python3 -m playwright install-deps
|
||||
# give a note
|
||||
printf "Note, if these gave any errors, playwright may not be officially supported on your OS, check this issues page for support\nhttps://github.com/microsoft/playwright/issues"
|
||||
if [ -x "$(command -v pacman)" ]; then
|
||||
printf "It seems you are on and Arch based distro.\nTry installing these from the AUR for playwright to run:\nenchant1.6\nicu66\nlibwebp052\n"
|
||||
fi
|
||||
cd ..
|
||||
}
|
||||
|
||||
# Install depndencies
|
||||
function install_deps(){
|
||||
# if the platform is mac, install macos
|
||||
if [ "$(uname)" == "Darwin" ]; then
|
||||
install_macos || install_fail
|
||||
# if pacman is found
|
||||
elif [ -x "$(command -v pacman)" ]; then
|
||||
# install for arch
|
||||
install_arch || install_fail
|
||||
# if apt-get is found
|
||||
elif [ -x "$(command -v apt-get)" ]; then
|
||||
# install fro debian
|
||||
install_deb || install_fail
|
||||
# if dnf is found
|
||||
elif [ -x "$(command -v dnf)" ]; then
|
||||
# install for fedora
|
||||
install_fedora || install_fail
|
||||
# if yum is found
|
||||
elif [ -x "$(command -v yum)" ]; then
|
||||
# install for centos
|
||||
install_centos || install_fail
|
||||
# else
|
||||
else
|
||||
# print an error message and exit
|
||||
printf "Your OS is not supported\n Please install python3, pip3 and git manually\n After that, run the script again with the -pb option to install python and playwright dependencies\n If you want to add support for your OS, please open a pull request on github\n
|
||||
https://github.com/elebumm/RedditVideoMakerBot"
|
||||
exit 1
|
||||
fi
|
||||
}
|
||||
|
||||
# Main function
|
||||
function install_main(){
|
||||
# Print that are installing
|
||||
echo "Installing..."
|
||||
# if -y (assume yes) continue
|
||||
if [[ ASSUME_YES -eq 1 ]]; then
|
||||
echo "Assuming yes"
|
||||
# else, ask if they want to continue
|
||||
else
|
||||
echo "Continue? (y/n)"
|
||||
read answer
|
||||
# if the answer is not yes, then exit
|
||||
if [ "$answer" != "y" ]; then
|
||||
echo "Aborting"
|
||||
exit 1
|
||||
fi
|
||||
fi
|
||||
# if the -d (only dependencies) options is selected install just the dependencies
|
||||
if [[ DEPS_ONLY -eq 1 ]]; then
|
||||
echo "Installing only dependencies"
|
||||
install_deps
|
||||
elif [[ PYTHON_ONLY -eq 1 ]]; then
|
||||
# if the -p (only python dependencies) options is selected install just the python dependencies and playwright
|
||||
echo "Installing only python dependencies"
|
||||
install_python_dep
|
||||
install_playwright
|
||||
# if the -b (only the bot) options is selected install just the bot
|
||||
elif [[ JUST_BOT -eq 1 ]]; then
|
||||
echo "Installing only the bot"
|
||||
get_the_bot
|
||||
# if the -l (bot and python) options is selected install just the bot and python dependencies
|
||||
elif [[ BOT_AND_PYTHON -eq 1 ]]; then
|
||||
echo "Installing only the bot and python dependencies"
|
||||
get_the_bot
|
||||
install_python_dep
|
||||
# else, install everything
|
||||
else
|
||||
echo "Installing all"
|
||||
install_deps
|
||||
get_the_bot
|
||||
install_python_dep
|
||||
install_playwright
|
||||
fi
|
||||
|
||||
DIR="./RedditVideoMakerBot"
|
||||
if [ -d "$DIR" ]; then
|
||||
printf "\nThe bot is already installed, want to run it?"
|
||||
# if -y (assume yes) continue
|
||||
if [[ ASSUME_YES -eq 1 ]]; then
|
||||
echo "Assuming yes"
|
||||
# else, ask if they want to continue
|
||||
else
|
||||
echo "Continue? (y/n)"
|
||||
read answer
|
||||
# if the answer is not yes, then exit
|
||||
if [ "$answer" != "y" ]; then
|
||||
echo "Aborting"
|
||||
exit 1
|
||||
fi
|
||||
fi
|
||||
cd RedditVideoMakerBot
|
||||
python3 main.py
|
||||
fi
|
||||
}
|
||||
|
||||
# Run the main function
|
||||
install_main
|
@ -1,12 +1,11 @@
|
||||
boto3==1.24.23
|
||||
botocore==1.27.23
|
||||
boto3==1.24.24
|
||||
botocore==1.27.24
|
||||
gTTS==2.2.4
|
||||
moviepy==1.0.3
|
||||
mutagen==1.45.1
|
||||
playwright==1.23.0
|
||||
praw==7.6.0
|
||||
python-dotenv==0.20.0
|
||||
pytube==12.1.0
|
||||
requests==2.28.1
|
||||
rich==12.4.4
|
||||
rich==12.5.1
|
||||
toml==0.10.2
|
||||
translators==5.3.1
|
||||
|
@ -1,193 +0,0 @@
|
||||
#!/usr/bin/env python
|
||||
import os
|
||||
from rich.console import Console
|
||||
from rich.table import Table
|
||||
from rich import box
|
||||
import re
|
||||
import dotenv
|
||||
from utils.console import handle_input
|
||||
|
||||
console = Console()
|
||||
|
||||
|
||||
def check_env() -> bool:
|
||||
"""Checks to see what's been put in .env
|
||||
|
||||
Returns:
|
||||
bool: Whether or not everything was put in properly
|
||||
"""
|
||||
if not os.path.exists(".env.template"):
|
||||
console.print("[red]Couldn't find .env.template. Unable to check variables.")
|
||||
return True
|
||||
if not os.path.exists(".env"):
|
||||
console.print("[red]Couldn't find the .env file, creating one now.")
|
||||
with open(".env", "x", encoding="utf-8") as file:
|
||||
file.write("")
|
||||
success = True
|
||||
with open(".env.template", "r", encoding="utf-8") as template:
|
||||
# req_envs = [env.split("=")[0] for env in template.readlines() if "=" in env]
|
||||
matching = {}
|
||||
explanations = {}
|
||||
bounds = {}
|
||||
types = {}
|
||||
oob_errors = {}
|
||||
examples = {}
|
||||
req_envs = []
|
||||
var_optional = False
|
||||
for line in template.readlines():
|
||||
if line.startswith("#") is not True and "=" in line and var_optional is not True:
|
||||
req_envs.append(line.split("=")[0])
|
||||
if "#" in line:
|
||||
examples[line.split("=")[0]] = "#".join(line.split("#")[1:]).strip()
|
||||
elif "#OPTIONAL" in line:
|
||||
var_optional = True
|
||||
elif line.startswith("#MATCH_REGEX "):
|
||||
matching[req_envs[-1]] = line.removeprefix("#MATCH_REGEX ")[:-1]
|
||||
var_optional = False
|
||||
elif line.startswith("#OOB_ERROR "):
|
||||
oob_errors[req_envs[-1]] = line.removeprefix("#OOB_ERROR ")[:-1]
|
||||
var_optional = False
|
||||
elif line.startswith("#RANGE "):
|
||||
bounds[req_envs[-1]] = tuple(
|
||||
map(
|
||||
lambda x: float(x) if x != "None" else None,
|
||||
line.removeprefix("#RANGE ")[:-1].split(":"),
|
||||
)
|
||||
)
|
||||
var_optional = False
|
||||
elif line.startswith("#MATCH_TYPE "):
|
||||
types[req_envs[-1]] = eval(line.removeprefix("#MATCH_TYPE ")[:-1].split()[0])
|
||||
var_optional = False
|
||||
elif line.startswith("#EXPLANATION "):
|
||||
explanations[req_envs[-1]] = line.removeprefix("#EXPLANATION ")[:-1]
|
||||
var_optional = False
|
||||
else:
|
||||
var_optional = False
|
||||
missing = set()
|
||||
incorrect = set()
|
||||
dotenv.load_dotenv()
|
||||
for env in req_envs:
|
||||
value = os.getenv(env)
|
||||
if value is None:
|
||||
missing.add(env)
|
||||
continue
|
||||
if env in matching.keys():
|
||||
re.match(matching[env], value) is None and incorrect.add(env)
|
||||
if env in bounds.keys() and env not in types.keys():
|
||||
len(value) >= bounds[env][0] or (
|
||||
len(bounds[env]) > 1 and bounds[env][1] >= len(value)
|
||||
) or incorrect.add(env)
|
||||
continue
|
||||
if env in types.keys():
|
||||
try:
|
||||
temp = types[env](value)
|
||||
if env in bounds.keys():
|
||||
(bounds[env][0] <= temp or incorrect.add(env)) and len(bounds[env]) > 1 and (
|
||||
bounds[env][1] >= temp or incorrect.add(env)
|
||||
)
|
||||
except ValueError:
|
||||
incorrect.add(env)
|
||||
|
||||
if len(missing):
|
||||
table = Table(
|
||||
title="Missing variables",
|
||||
highlight=True,
|
||||
show_lines=True,
|
||||
box=box.ROUNDED,
|
||||
border_style="#414868",
|
||||
header_style="#C0CAF5 bold",
|
||||
title_justify="left",
|
||||
title_style="#C0CAF5 bold",
|
||||
)
|
||||
table.add_column("Variable", justify="left", style="#7AA2F7 bold", no_wrap=True)
|
||||
table.add_column("Explanation", justify="left", style="#BB9AF7", no_wrap=False)
|
||||
table.add_column("Example", justify="center", style="#F7768E", no_wrap=True)
|
||||
table.add_column("Min", justify="right", style="#F7768E", no_wrap=True)
|
||||
table.add_column("Max", justify="left", style="#F7768E", no_wrap=True)
|
||||
for env in missing:
|
||||
table.add_row(
|
||||
env,
|
||||
explanations[env] if env in explanations.keys() else "No explanation given",
|
||||
examples[env] if env in examples.keys() else "",
|
||||
str(bounds[env][0]) if env in bounds.keys() and bounds[env][1] is not None else "",
|
||||
str(bounds[env][1])
|
||||
if env in bounds.keys() and len(bounds[env]) > 1 and bounds[env][1] is not None
|
||||
else "",
|
||||
)
|
||||
console.print(table)
|
||||
success = False
|
||||
if len(incorrect):
|
||||
table = Table(
|
||||
title="Incorrect variables",
|
||||
highlight=True,
|
||||
show_lines=True,
|
||||
box=box.ROUNDED,
|
||||
border_style="#414868",
|
||||
header_style="#C0CAF5 bold",
|
||||
title_justify="left",
|
||||
title_style="#C0CAF5 bold",
|
||||
)
|
||||
table.add_column("Variable", justify="left", style="#7AA2F7 bold", no_wrap=True)
|
||||
table.add_column("Current value", justify="left", style="#F7768E", no_wrap=False)
|
||||
table.add_column("Explanation", justify="left", style="#BB9AF7", no_wrap=False)
|
||||
table.add_column("Example", justify="center", style="#F7768E", no_wrap=True)
|
||||
table.add_column("Min", justify="right", style="#F7768E", no_wrap=True)
|
||||
table.add_column("Max", justify="left", style="#F7768E", no_wrap=True)
|
||||
for env in incorrect:
|
||||
table.add_row(
|
||||
env,
|
||||
os.getenv(env),
|
||||
explanations[env] if env in explanations.keys() else "No explanation given",
|
||||
str(types[env].__name__) if env in types.keys() else "str",
|
||||
str(bounds[env][0]) if env in bounds.keys() else "None",
|
||||
str(bounds[env][1]) if env in bounds.keys() and len(bounds[env]) > 1 else "None",
|
||||
)
|
||||
missing.add(env)
|
||||
console.print(table)
|
||||
success = False
|
||||
if success is True:
|
||||
return True
|
||||
console.print(
|
||||
"[green]Do you want to automatically overwrite incorrect variables and add the missing variables? (y/n)"
|
||||
)
|
||||
if not input().casefold().startswith("y"):
|
||||
console.print("[red]Aborting: Unresolved missing variables")
|
||||
return False
|
||||
if len(incorrect):
|
||||
with open(".env", "r+", encoding="utf-8") as env_file:
|
||||
lines = []
|
||||
for line in env_file.readlines():
|
||||
line.split("=")[0].strip() not in incorrect and lines.append(line)
|
||||
env_file.seek(0)
|
||||
env_file.write("\n".join(lines))
|
||||
env_file.truncate()
|
||||
console.print("[green]Successfully removed incorrectly set variables from .env")
|
||||
with open(".env", "a", encoding="utf-8") as env_file:
|
||||
for env in missing:
|
||||
env_file.write(
|
||||
env
|
||||
+ "="
|
||||
+ ('"' if env not in types.keys() else "")
|
||||
+ str(
|
||||
handle_input(
|
||||
"[#F7768E bold]" + env + "[#C0CAF5 bold]=",
|
||||
types[env] if env in types.keys() else False,
|
||||
matching[env] if env in matching.keys() else ".*",
|
||||
explanations[env]
|
||||
if env in explanations.keys()
|
||||
else "Incorrect input. Try again.",
|
||||
bounds[env][0] if env in bounds.keys() else None,
|
||||
bounds[env][1] if env in bounds.keys() and len(bounds[env]) > 1 else None,
|
||||
oob_errors[env] if env in oob_errors.keys() else "Input too long/short.",
|
||||
extra_info="[#C0CAF5 bold]⮶ "
|
||||
+ (explanations[env] if env in explanations.keys() else "No info available"),
|
||||
)
|
||||
)
|
||||
+ ('"' if env not in types.keys() else "")
|
||||
+ "\n"
|
||||
)
|
||||
return True
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
check_env()
|
@ -1,46 +0,0 @@
|
||||
# write a class that takes .env file and parses it into a dictionary
|
||||
from dotenv import dotenv_values
|
||||
|
||||
DEFAULTS = {
|
||||
"SUBREDDIT": "AskReddit",
|
||||
"ALLOW_NSFW": "False",
|
||||
"POST_ID": "",
|
||||
"THEME": "DARK",
|
||||
"REDDIT_2FA": "no",
|
||||
"TIMES_TO_RUN": "",
|
||||
"MAX_COMMENT_LENGTH": "500",
|
||||
"OPACITY": "1",
|
||||
"VOICE": "en_us_001",
|
||||
"STORYMODE": "False",
|
||||
}
|
||||
|
||||
|
||||
class Config:
|
||||
def __init__(self):
|
||||
self.raw = dotenv_values("../.env")
|
||||
self.load_attrs()
|
||||
|
||||
def __getattr__(self, attr): # code completion for attributes fix.
|
||||
return getattr(self, attr)
|
||||
|
||||
def load_attrs(self):
|
||||
for key, value in self.raw.items():
|
||||
self.add_attr(key, value)
|
||||
|
||||
def add_attr(self, key, value):
|
||||
if value is None or value == "":
|
||||
setattr(self, key, DEFAULTS[key])
|
||||
else:
|
||||
setattr(self, key, str(value))
|
||||
|
||||
|
||||
config = Config()
|
||||
|
||||
print(config.SUBREDDIT)
|
||||
# def temp():
|
||||
# root = ''
|
||||
# if isinstance(root, praw.models.Submission):
|
||||
# root_type = 'submission'
|
||||
# elif isinstance(root, praw.models.Comment):
|
||||
# root_type = 'comment'
|
||||
#
|
@ -1,51 +0,0 @@
|
||||
# Okay, have to admit. This code is from StackOverflow. It's so efficient, that it's probably the best way to do it.
|
||||
# Although, it is edited to use less threads.
|
||||
|
||||
|
||||
from itertools import cycle
|
||||
from shutil import get_terminal_size
|
||||
from threading import Thread
|
||||
from time import sleep
|
||||
|
||||
|
||||
class Loader:
|
||||
def __init__(self, desc="Loading...", end="Done!", timeout=0.1):
|
||||
"""
|
||||
A loader-like context manager
|
||||
|
||||
Args:
|
||||
desc (str, optional): The loader's description. Defaults to "Loading...".
|
||||
end (str, optional): Final print. Defaults to "Done!".
|
||||
timeout (float, optional): Sleep time between prints. Defaults to 0.1.
|
||||
"""
|
||||
self.desc = desc
|
||||
self.end = end
|
||||
self.timeout = timeout
|
||||
|
||||
self._thread = Thread(target=self._animate, daemon=True)
|
||||
self.steps = ["⢿", "⣻", "⣽", "⣾", "⣷", "⣯", "⣟", "⡿"]
|
||||
self.done = False
|
||||
|
||||
def start(self):
|
||||
self._thread.start()
|
||||
return self
|
||||
|
||||
def _animate(self):
|
||||
for c in cycle(self.steps):
|
||||
if self.done:
|
||||
break
|
||||
print(f"\r{self.desc} {c}", flush=True, end="")
|
||||
sleep(self.timeout)
|
||||
|
||||
def __enter__(self):
|
||||
self.start()
|
||||
|
||||
def stop(self):
|
||||
self.done = True
|
||||
cols = get_terminal_size((80, 20)).columns
|
||||
print("\r" + " " * cols, end="", flush=True)
|
||||
print(f"\r{self.end}", flush=True)
|
||||
|
||||
def __exit__(self, exc_type, exc_value, tb):
|
||||
# handle exceptions with those variables ^
|
||||
self.stop()
|
@ -0,0 +1,170 @@
|
||||
#!/usr/bin/env python
|
||||
import toml
|
||||
from rich.console import Console
|
||||
import re
|
||||
|
||||
from typing import Tuple, Dict
|
||||
|
||||
from utils.console import handle_input
|
||||
|
||||
|
||||
console = Console()
|
||||
config = dict # autocomplete
|
||||
|
||||
|
||||
def crawl(obj: dict, func=lambda x, y: print(x, y, end="\n"), path=None):
|
||||
if path is None: # path Default argument value is mutable
|
||||
path = []
|
||||
for key in obj.keys():
|
||||
if type(obj[key]) is dict:
|
||||
crawl(obj[key], func, path + [key])
|
||||
continue
|
||||
func(path + [key], obj[key])
|
||||
|
||||
|
||||
def check(value, checks, name):
|
||||
def get_check_value(key, default_result):
|
||||
return checks[key] if key in checks else default_result
|
||||
|
||||
incorrect = False
|
||||
if value == {}:
|
||||
incorrect = True
|
||||
if not incorrect and "type" in checks:
|
||||
try:
|
||||
value = eval(checks["type"])(value)
|
||||
except:
|
||||
incorrect = True
|
||||
|
||||
if (
|
||||
not incorrect and "options" in checks and value not in checks["options"]
|
||||
): # FAILSTATE Value is not one of the options
|
||||
incorrect = True
|
||||
if (
|
||||
not incorrect
|
||||
and "regex" in checks
|
||||
and (
|
||||
(isinstance(value, str) and re.match(checks["regex"], value) is None)
|
||||
or not isinstance(value, str)
|
||||
)
|
||||
): # FAILSTATE Value doesn't match regex, or has regex but is not a string.
|
||||
incorrect = True
|
||||
|
||||
if (
|
||||
not incorrect
|
||||
and not hasattr(value, "__iter__")
|
||||
and (
|
||||
("nmin" in checks and checks["nmin"] is not None and value < checks["nmin"])
|
||||
or ("nmax" in checks and checks["nmax"] is not None and value > checks["nmax"])
|
||||
)
|
||||
):
|
||||
incorrect = True
|
||||
if (
|
||||
not incorrect
|
||||
and hasattr(value, "__iter__")
|
||||
and (
|
||||
("nmin" in checks and checks["nmin"] is not None and len(value) < checks["nmin"])
|
||||
or ("nmax" in checks and checks["nmax"] is not None and len(value) > checks["nmax"])
|
||||
)
|
||||
):
|
||||
incorrect = True
|
||||
|
||||
if incorrect:
|
||||
value = handle_input(
|
||||
message=(
|
||||
(("[blue]Example: " + str(checks["example"]) + "\n") if "example" in checks else "")
|
||||
+ "[red]"
|
||||
+ ("Non-optional ", "Optional ")["optional" in checks and checks["optional"] is True]
|
||||
)
|
||||
+ "[#C0CAF5 bold]"
|
||||
+ str(name)
|
||||
+ "[#F7768E bold]=",
|
||||
extra_info=get_check_value("explanation", ""),
|
||||
check_type=eval(get_check_value("type", "False")),
|
||||
default=get_check_value("default", NotImplemented),
|
||||
match=get_check_value("regex", ""),
|
||||
err_message=get_check_value("input_error", "Incorrect input"),
|
||||
nmin=get_check_value("nmin", None),
|
||||
nmax=get_check_value("nmax", None),
|
||||
oob_error=get_check_value(
|
||||
"oob_error", "Input out of bounds(Value too high/low/long/short)"
|
||||
),
|
||||
options=get_check_value("options", None),
|
||||
optional=get_check_value("optional", False),
|
||||
)
|
||||
return value
|
||||
|
||||
|
||||
def crawl_and_check(obj: dict, path: list, checks: dict = {}, name=""):
|
||||
if len(path) == 0:
|
||||
return check(obj, checks, name)
|
||||
if path[0] not in obj.keys():
|
||||
obj[path[0]] = {}
|
||||
obj[path[0]] = crawl_and_check(obj[path[0]], path[1:], checks, path[0])
|
||||
return obj
|
||||
|
||||
|
||||
def check_vars(path, checks):
|
||||
global config
|
||||
crawl_and_check(config, path, checks)
|
||||
|
||||
|
||||
def check_toml(template_file, config_file) -> Tuple[bool, Dict]:
|
||||
global config
|
||||
config = None
|
||||
try:
|
||||
template = toml.load(template_file)
|
||||
except Exception as error:
|
||||
console.print(f"[red bold]Encountered error when trying to to load {template_file}: {error}")
|
||||
return False
|
||||
try:
|
||||
config = toml.load(config_file)
|
||||
except toml.TomlDecodeError:
|
||||
console.print(
|
||||
f"""[blue]Couldn't read {config_file}.
|
||||
Overwrite it?(y/n)"""
|
||||
)
|
||||
if not input().startswith("y"):
|
||||
print("Unable to read config, and not allowed to overwrite it. Giving up.")
|
||||
return False
|
||||
else:
|
||||
try:
|
||||
with open(config_file, "w") as f:
|
||||
f.write("")
|
||||
except:
|
||||
console.print(
|
||||
f"[red bold]Failed to overwrite {config_file}. Giving up.\nSuggestion: check {config_file} permissions for the user."
|
||||
)
|
||||
return False
|
||||
except FileNotFoundError:
|
||||
console.print(
|
||||
f"""[blue]Couldn't find {config_file}
|
||||
Creating it now."""
|
||||
)
|
||||
try:
|
||||
with open(config_file, "x") as f:
|
||||
f.write("")
|
||||
config = {}
|
||||
except:
|
||||
console.print(
|
||||
f"[red bold]Failed to write to {config_file}. Giving up.\nSuggestion: check the folder's permissions for the user."
|
||||
)
|
||||
return False
|
||||
|
||||
console.print(
|
||||
"""\
|
||||
[blue bold]###############################
|
||||
# #
|
||||
# Checking TOML configuration #
|
||||
# #
|
||||
###############################
|
||||
If you see any prompts, that means that you have unset/incorrectly set variables, please input the correct values.\
|
||||
"""
|
||||
)
|
||||
crawl(template, check_vars)
|
||||
with open(config_file, "w") as f:
|
||||
toml.dump(config, f)
|
||||
return config
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
check_toml(".config.template.toml", "config.toml")
|
Loading…
Reference in new issue