Skip to content

Commit

Permalink
Rewritten poor code on byuser.py and init.py
Browse files Browse the repository at this point in the history
  • Loading branch information
nanometer5088 committed Feb 20, 2023
1 parent 35a0748 commit b3adab4
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 112 deletions.
49 changes: 17 additions & 32 deletions src/byuser.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,55 +4,40 @@

import requests
from log import logtofile as log
from src.streaming import getVideoInfo

from src.streaming import mpv, getVideoInfo

def info(username):
rss_url = f"https://tik.hostux.net/@{username}/rss"
response = requests.get(rss_url)

response = requests.get(f"https://tik.hostux.net/@{username}/rss")
if response.status_code == 404:
print(
"Something went wrong while getting the information. Make sure the username was correctly inserted and try again."
)
log(
f"https://tik.hostux.net/@{username}/rss returned a 404 error. The username is likely incorrect"
)
print("Something went wrong while getting the information. Make sure the username was correctly inserted and try again.")
log(f"{rss_url} returned a 404 error. The username is likely incorrect.")
sys.exit()
if str(response.content) == "b''":
log(
f"https://tik.hostux.net/@{username}/rss returned no information. The account likely does not exist"
)

if not response.content:
print("The specified account does not exist.")
log(f"{rss_url} returned no information. The account likely does not exist.")
sys.exit()

# if response.description == None:
# log("This account does not have a bio.\n")
# else:
# log(f"Bio: {str(response.description)}\n") ## TIKTOK BIO

return atoma.parse_rss_bytes(response.content)


def getLinks(username):
feed = info(username)
linklist = []

for i in range(len(feed.items)):
linklist.append("https://www.tiktok.com" + feed.items[i].link)
return linklist

links = ["https://www.tiktok.com" + item.link for item in feed.items]
return links

def streamuser(username):
links = getLinks(username)

if len(links) == 0:
log(
"The link list is empty. The specified account is likely private or has no published videos"
)
if not links:
error_msg = "The link list is empty. The specified account is likely private or has no published videos"
log(error_msg)
print("This account is private or has no published videos.")
from src.streaming import mpv, getVideoInfo
return

for i in range(len(links)):
url = getVideoInfo(links[i])
for link in links:
url = getVideoInfo(link)
mpv(url)
log(f"Video {links[i]} was played.")
log(f"Video {link} was played.")
146 changes: 66 additions & 80 deletions src/init.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,100 +12,86 @@
import os
import platform
import sys
import requests

from log import logtofile as log


def clear_screen():
os.system("cls" if os.name == "nt" else "clear")


def init():
os.system("cls || clear")
input(
"Welcome to CLI TikTok, an open-source TikTok archiver and viewer!\nPress ENTER to proceed"
)
clear_screen()
print("Welcome to CLI TikTok, an open-source TikTok archiver and viewer!")
input("Press ENTER to proceed")

log("Started dependency test")
try:
import atoma
import requests
import yt_dlp
import distro

os.system("cls || clear")
log("Dependency test sucessful!\n")
except ModuleNotFoundError:
log("Dependency test failed - Missing library")
os.system("cls || clear")
input(
"""
The program detected dependencies are not installed
Press ENTER to install the necessary libraries
(You will need to open the program again afterwards)
"""
)
log("User accepted automatic installation, running it.")
required_libraries = ["atoma", "requests", "yt_dlp", "distro"]
missing_libraries = [library for library in required_libraries if not _library_exists(library)]
if missing_libraries:
log("Dependency test failed - Missing libraries: " + ", ".join(missing_libraries))
clear_screen()
input("The program detected dependencies are not installed.\nPress ENTER to install the necessary libraries.\n(You will need to open the program again afterwards)")
log("User accepted automatic installation, running it.\n")
os.system("pip install -r requirements.txt --user")
os.system("cls || clear")
clear_screen()
return -1
else:
log("Dependency test successful.\n")

log("Started operating system and python detection")
if platform.system() == "Windows":
log(f"Operating System: Windows {platform.win32_ver()[0]}")
elif platform.system() == "Darwin":
log(f"Operating System: Mac OS {platform.mac_ver()}") # Untested, might break on Mac OS
elif platform.system() == "Linux":
import distro
log(f"Operating System: {distro.name()} {distro.version()} - {os.uname().release}")
elif (
platform.system() != "Windows"
or platform.system() != "Darwin"
or platform.system() != "Linux"
):
log(f"Operating System: {platform.system()} - {platform.machine()}")

log(
f"Python {sys.version_info.major}.{sys.version_info.minor}.{sys.version_info.micro} - {sys.version_info.releaselevel}"
)

log("Operating System and Python detection finished!\n")
log("Operating System: " + _get_os_info())
log("Python " + _get_python_version())
log("Operating System and Python detection finished\n")

log("Started update / networking test")
try:
import requests

log("Started update / networking test")
data = requests.get(
"https://raw.githubusercontent.com/nanometer5088/CLI-TikTok/main/VERSION"
)
version = open("VERSION", "r", encoding="utf=8")
userversion = version.readline().rstrip()
if userversion < (data.text):
log(
f"New version detected! User version is {userversion}, but {data.text} was found on Github."
)
os.system("cls || clear")
log("User was prompted to update")
input(
"""
There's a new version available!
Updates bring performance and feature improvements!
Download the new version here:
https://github.com/nanometer5088/CLI-TikTok/archive/refs/heads/main.zip
Press ENTER to proceed
"""
)
os.system("cls || clear")
data = requests.get("https://raw.githubusercontent.com/nanometer5088/CLI-TikTok/main/VERSION")
userversion = _read_user_version()
if userversion < data.text:
log(f"New version detected! User version is {userversion}, but {data.text} was found on Github.")
clear_screen()
input("There's a new version available!\nUpdates bring performance and feature improvements!\nDownload the new version here:\nhttps://github.com/nanometer5088/CLI-TikTok/archive/refs/heads/main.zip\nPress ENTER to proceed")
clear_screen()
else:
log("The user has internet acess and the software is up-to-date.")
version.close()
log("The user has internet access and the software is up-to-date.\n")
clear_screen()
except requests.exceptions.ConnectionError:
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")
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()


def _library_exists(library):
try:
__import__(library)
return True
except ImportError:
return False


def _get_os_info():
system = platform.system()
if system == "Windows":
return f"Windows {platform.win32_ver()[0]}"
elif system == "Darwin":
return f"Mac OS {platform.mac_ver()}"
elif system == "Linux":
import distro
return f"{distro.name()} {distro.version()} - {os.uname().release}"
else:
return f"{system} - {platform.machine()}"


def _get_python_version():
return f"{sys.version_info.major}.{sys.version_info.minor}.{sys.version_info.micro} - {sys.version_info.releaselevel}"


def _read_user_version():
with open("VERSION", "r", encoding="utf-8") as f:
return f.readline().rstrip()

0 comments on commit b3adab4

Please sign in to comment.