Improved code around version check

It won't assume you have no internet connection if you can't connect to Github, it will do further testing
Regardless, it'll be logged.
This commit is contained in:
nanometer5088
2023-03-24 17:55:24 -03:00
parent a73a134b6b
commit 5a578721f5
2 changed files with 149 additions and 136 deletions

263
main.py
View File

@@ -25,154 +25,161 @@ from src.functions import listas
from src.streaming import playback, playbackrandom
from src.trending import streamtrending
# Needlessly big code to simply prompt the user which action they want to do
log("Main menu started")
try:
question = int(
input(
"""Do you want to download or watch tiktoks?
(1) Download
(2) Watch
"""
)
)
os.system("cls || clear")
## Download
if question == 1:
downloadquestion = int(
def main():
# Needlessly big code to simply prompt the user which action they want to do
log("Main menu started")
try:
question = int(
input(
"""Do you want to download your liked videos or a creator?
(1) Liked Videos
(2) Creator
"""
"""Do you want to download or watch tiktoks?
(1) Download
(2) Watch
"""
)
)
os.system("cls || clear")
## Download liked videos
if downloadquestion == 1:
log("The user chose to download liked videos\n")
urls = listas()[0]
downloadtiktoks(urls)
sys.exit()
## Download creator
if downloadquestion == 2:
log("The user chose to download videos from a creator")
print(
"Due to specific limitations of the current data method, downloading by creator will only get the latest 24 videos."
)
print(
"This limitation is being actively researched, any contributions will be welcome."
)
username = str(input("Enter the tiktok username here: "))
log(f"The creator chosen was: @{username}\n")
links = getLinks(username)
downloadtiktoks(links)
sys.exit()
## Stream
if question == 2:
watchquestion = int(
input(
"""Do you want to watch your liked videos, a creator or trending videos?
(1) Liked Videos
(2) Creator
(3) Trending
"""
)
)
os.system("cls || clear")
## Stream liked videos
if watchquestion == 1:
randomquestion = int(
## Download
if question == 1:
downloadquestion = int(
input(
"""Do you want to watch the tiktoks in randomized order?
(1) Yes
(2) No
"""
"""Do you want to download your liked videos or a creator?
(1) Liked Videos
(2) Creator
"""
)
)
os.system("cls || clear")
## Stream liked videos randomized
if randomquestion == 1:
log("The user chose to stream liked videos in shuffled mode\n")
## Download liked videos
if downloadquestion == 1:
log("The user chose to download liked videos\n")
urls = listas()[0]
datas = listas()[1]
playbackrandom(urls, datas)
downloadtiktoks(urls)
sys.exit()
## Stream liked videos in descending order
if randomquestion == 2:
log("The user chose to stream liked videos in regular mode\n")
urls = listas()[0]
datas = listas()[1]
playback(urls, datas)
## Download creator
if downloadquestion == 2:
log("The user chose to download videos from a creator")
print(
"Due to specific limitations of the current data method, downloading by creator will only get the latest 24 videos."
)
print(
"This limitation is being actively researched, any contributions will be welcome."
)
username = str(input("Enter the tiktok username here: "))
log(f"The creator chosen was: @{username}\n")
links = getLinks(username)
downloadtiktoks(links)
sys.exit()
## Stream creator
if watchquestion == 2:
log("The user chose to stream videos from a creator")
print(
"Due to specific limitations of the current data method, watching by creator will only get the latest 24 videos."
## Stream
if question == 2:
watchquestion = int(
input(
"""Do you want to watch your liked videos, a creator or trending videos?
(1) Liked Videos
(2) Creator
(3) Trending
"""
)
)
print(
"This limitation is being actively researched, any contributions will be welcome."
)
username = str(input("Enter the tiktok username here: "))
log(f"The creator chosen was: @{username}\n")
streamuser(username)
sys.exit()
os.system("cls || clear")
## Stream trending videos
if watchquestion == 3:
log("The user chose to stream trending videos\n")
print(
"Due to specific limitations of the current data method, watching by creator will only get the latest 24 videos."
)
print(
"This limitation is being actively researched, any contributions will be welcome."
)
streamtrending()
sys.exit()
## Stream liked videos
if watchquestion == 1:
# Error handling for invalid number (3, 4, 6, 133)
log("The user entered an invalid numeric choice, and the software exited")
print("The option you chose isn't valid.")
randomquestion = int(
input(
"""Do you want to watch the tiktoks in randomized order?
(1) Yes
(2) No
"""
)
)
os.system("cls || clear")
# Error handling for invalid input (ENTER, 't', '5ga')
except ValueError:
log("The user entered an invalid non-numeric choice, and the software exited")
print("The option you chose isn't valid.")
## Stream liked videos randomized
if randomquestion == 1:
log("The user chose to stream liked videos in shuffled mode\n")
urls = listas()[0]
datas = listas()[1]
playbackrandom(urls, datas)
sys.exit()
# Error handling for missing user_data.json file
except FileNotFoundError:
log(
"The user does not have a user_data.json file, but chose an option that depends on it, so the software exited"
)
print(
"The 'user_data.json' file was not found. Make sure it is in the program folder and try again."
)
## Stream liked videos in descending order
if randomquestion == 2:
log("The user chose to stream liked videos in regular mode\n")
urls = listas()[0]
datas = listas()[1]
playback(urls, datas)
sys.exit()
# Error handling for MPV media player or MPV not found in PATH
except subprocess.CalledProcessError:
log(
"Tried to run MPV media player, but it was not found in the PATH, so the software exited"
)
os.system("cls || clear")
print(
"MPV media player was not found on your system path. Make sure it's installed and try again."
)
## Stream creator
if watchquestion == 2:
log("The user chose to stream videos from a creator")
print(
"Due to specific limitations of the current data method, watching by creator will only get the latest 24 videos."
)
print(
"This limitation is being actively researched, any contributions will be welcome."
)
username = str(input("Enter the tiktok username here: "))
log(f"The creator chosen was: @{username}\n")
streamuser(username)
sys.exit()
# Error handling for exiting the code with CTRL + C
except KeyboardInterrupt:
log("The user used CTRL + C to force-stop the software.")
print("\n\tKeyboardInterrupt was detected - Goodbye!")
## Stream trending videos
if watchquestion == 3:
log("The user chose to stream trending videos\n")
print(
"Due to specific limitations of the current data method, watching by creator will only get the latest 24 videos."
)
print(
"This limitation is being actively researched, any contributions will be welcome."
)
streamtrending()
sys.exit()
# Error handling for invalid number (3, 4, 6, 133)
log("The user entered an invalid numeric choice, and the software exited")
print("The option you chose isn't valid.")
# Error handling for invalid input (ENTER, 't', '5ga')
except ValueError:
log("The user entered an invalid non-numeric choice, and the software exited")
print("The option you chose isn't valid.")
# Error handling for missing user_data.json file
except FileNotFoundError:
log(
"The user does not have a user_data.json file, but chose an option that depends on it, so the software exited"
)
print(
"The 'user_data.json' file was not found. Make sure it is in the program folder and try again."
)
# Error handling for MPV media player or MPV not found in PATH
except subprocess.CalledProcessError:
log(
"Tried to run MPV media player, but it was not found in the PATH, so the software exited"
)
os.system("cls || clear")
print(
"MPV media player was not found on your system path. Make sure it's installed and try again."
)
# Error handling for exiting the code with CTRL + C
except KeyboardInterrupt:
log("The user used CTRL + C to force-stop the software.")
print("\n\tKeyboardInterrupt was detected - Goodbye!")
main()
# Warning, this section is experimental and will only run if you use any launch arguments
# GUI Code:

View File

@@ -48,9 +48,9 @@ def init():
log("Started update / networking test")
try:
import ast
data = requests.get("https://raw.githubusercontent.com/nanometer5088/CLI-TikTok/main/src/constants.py")
link = requests.get("https://raw.githubusercontent.com/nanometer5088/CLI-TikTok/main/src/constants.py")
userversion = _read_user_version()
data = str(ast.literal_eval(data.text.split("APP = ")[1])["version"])
data = str(ast.literal_eval(link.text.split("APP = ")[1])["version"])
if userversion < data:
log(f"New version detected! User version is {userversion}, but {data} was found on Github.")
clear_screen()
@@ -61,13 +61,19 @@ def init():
clear_screen()
except requests.exceptions.ConnectionError:
clear_screen()
log("A connection error was detected when trying to connect to https://raw.githubusercontent.com/ to check for updates.")
print("CLI-TikTok detected your device isn't connected to the internet.")
print("This software requires a reliable and uncensored internet connection to properly work.")
print("Please try again with an internet connection.")
log("The software exited, and the user was notified of the connection problem.")
sys.exit()
try:
for site in ["https://www.eff.org", "https://freedom.press", "https://www.torproject.org", "https://www.privacyguides.org"]:
requests.get(site)
log("The user seems to be connected to the internet, but Github is not accessible. Dazed and confused, but trying to continue.")
clear_screen()
except requests.exceptions.ConnectionError:
clear_screen()
print("CLI-TikTok detected your device isn't connected to the internet.")
print("This software requires a reliable and uncensored internet connection to properly work.")
print("Please try again with an internet connection.")
log("The software exited, and the user was notified of the connection problem.")
sys.exit()
def _library_exists(library):