Skip to content

Commit

Permalink
Merge pull request #1 from WolfwithSword/cli_params
Browse files Browse the repository at this point in the history
Add CLI params
  • Loading branch information
WolfwithSword committed Aug 23, 2024
2 parents 45935e8 + c607f8b commit 6be47c8
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 7 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ lib/
*.zip
.tcn-cache/
*.db
test.config.ini
output/

# Byte-compiled / optimized / DLL files
__pycache__/
Expand Down
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,20 @@ user_expiry_s: `3600` number of seconds to keep user API results from twitch bef

vodlist_expiry_s: `600` number of seconds to keep list of user's vods with tagged users from twitch API before expiring. Can be long, but if a new public vod goes up, it won't be picked up until this expires

### CLI Parameters

- **-c \<filepath>** | **--conf_file \<filepath>**
- Accepts a filepath for a config file to use instead of the default `config.ini` found next to the program
- **-o \<filepath>** | **--output_file \<filepath**
- A filepath to output the generated html to. Must end with '.html'.
- Can be in a different directory and will automatically copy the lib folder to it if not present.

*Examples*

`./twitchcollabnetwork.exe -c configs/streamer1.config.ini -o output/streamer1/output.html`

`./twitchcollabnetwork.exe -c configs/streamer1.config.ini -o output/streamer1.html`

# FAQ

- It Crashed!
Expand Down
62 changes: 55 additions & 7 deletions main.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import os
import argparse
import sys
import shutil

import asyncio
import time
import os

from datetime import datetime
import logging
Expand All @@ -14,12 +18,39 @@
from helpers.twitch_utils import TwitchUtils
from helpers.utils import chunkify, time_since

#########
# Setup #
#########

logging.basicConfig(level=logging.INFO)
logger = logging.getLogger("tcn-main")

config = TCNConfig()
cwd = os.getcwd()
config_path = os.path.join(cwd, 'config.ini')
OUTPUT_FILE = "output.html"

argv = sys.argv
conf_parser = argparse.ArgumentParser(
description=__doc__, # -h/--help
formatter_class=argparse.RawDescriptionHelpFormatter,
add_help=False
)
conf_parser.add_argument("-c", "--conf_file", help="Specify config file", metavar="FILE")
conf_parser.add_argument('-o', '--output_file', help="Specify the output file", metavar="FILE")
args, remaining_argv = conf_parser.parse_known_args()

if args.conf_file:
if os.path.isfile(args.conf_file):
logger.info(f"Using config file: {args.conf_file}")
config_path = args.conf_file
else:
logger.warning(f"Could not use config path `{args.conf_file}`. Using default file and values")

if not os.path.isfile(config_path):
logger.error("No valid config file was found. Please setup a valid config file")
quit()

config = TCNConfig()
config.setup(path=config_path)

if not config.primary_channelnames:
Expand All @@ -31,15 +62,22 @@
logger.error("Please input your twitch client_id and client_secret")
quit()

BLACKLISTED = config.blacklisted_channelnames
if args.output_file:
if not str(args.output_file).endswith(".html"):
logger.error(f"Custom output file `{args.output_file}` is invalid. Must be an HTML file")
quit()
OUTPUT_FILE = args.output_file
logger.info(f"Output will go to: {OUTPUT_FILE}")

################
# End of Setup #
################

# Each user lookup is always two api requests. First is for user check, second is for video archives check.
# So N=500 users, means 1000 API requests
# So N=500 users, means 1000 API requests. Mitigated if using disk cache.


async def twitch_run():

start_time = time.time()
twitch = await Twitch(app_id=CLIENT_ID, app_secret=CLIENT_SECRET)
cache_dir = os.path.join(cwd, '.tcn-cache/')
Expand Down Expand Up @@ -110,17 +148,27 @@ async def twitch_run():
net = Network(notebook=False, height="1500px", width="100%",
bgcolor="#222222",
font_color="white",
heading=f"Twitch Collab Network: {','.join(config.primary_channelnames)}<br>{datetime.today().strftime('%Y-%m-%d')}<br>Depth: {depth}, Connections: {len(users)}",
heading=f"Twitch Collab Network: {','.join(config.primary_channelnames)}"
f"<br>{datetime.today().strftime('%Y-%m-%d')}"
f"<br>Depth: {depth}, Connections: {len(users)}",
select_menu=False, filter_menu=True, neighborhood_highlight=True)
net.from_nx(G)
options = '{"nodes": {"borderWidth": 5}}'
# net.show_buttons(filter_=True) # If uncommenting, do not set_options below
net.set_options(options)

net.set_template_dir(os.path.join(cwd, 'templates'), 'template.html')
net.write_html(name="output.html", notebook=False, local=True, open_browser=False)

output_dir = os.path.dirname(OUTPUT_FILE)
if not os.path.exists(output_dir):
os.makedirs(output_dir)
net.write_html(name=OUTPUT_FILE, notebook=False, local=True, open_browser=False)

if not os.path.exists(os.path.join(output_dir, 'lib')) and os.path.exists('lib'):
shutil.copytree("lib", os.path.join(output_dir, "lib"))

logger.info("Completed in {:.2f} seconds".format(time_since(start_time=start_time)))
logger.info(f"Output: {os.path.abspath(OUTPUT_FILE)}")

if twitch_utils.cache:
twitch_utils.cache.expire()
Expand Down

0 comments on commit 6be47c8

Please sign in to comment.