Skip to content

Commit

Permalink
added gui files, made many improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
unalignedcoder authored Oct 13, 2023
1 parent 4426aef commit c869462
Show file tree
Hide file tree
Showing 5 changed files with 200 additions and 12 deletions.
77 changes: 77 additions & 0 deletions picrel-gui.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
import PySimpleGUI as sg
import subprocess
import threading

def run_picrel_script(url, use_names, reload_queue, use_titles, monitor_thread):
# Define the command to execute the picrel script
cmd = ['python', 'picrel.py', url]

if use_names:
cmd.append('-n')
if reload_queue:
cmd.append('-r')
if use_titles:
cmd.append('-t')
if monitor_thread:
cmd.append('-m')

# Execute the picrel script
process = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, universal_newlines=True)

while True:
line = process.stdout.readline()
if not line:
break
print(line.strip())

process.wait()

def run_script():
# Disable the 'Run' button while the script is running
window['-RUN-'].update(disabled=True)

# Clear the output before running the script
window['-OUTPUT-'].update('')

# Get the selected values from the GUI
url = values['-THREAD_URL-']
use_names = values['-USE_NAMES-']
reload_queue = values['-RELOAD-']
use_titles = values['-TITLE-']
monitor_thread = values['-MONITOR-']

# Create a separate thread for running the picrel script
thread = threading.Thread(target=run_picrel_script, args=(url, use_names, reload_queue, use_titles, monitor_thread))
thread.start()

def exit_script():
# Close the window
window.close()

# Set the color theme for the GUI
sg.theme('GreenMono')
sg.set_options(font=('Arial', 14))

# Create the GUI layout
layout = [
[sg.Text('Thread URL or Queue file'), sg.Input(key='-THREAD_URL-')],
[sg.Checkbox('Use Thread Names', key='-USE_NAMES-')],
[sg.Checkbox('Reload queue file', key='-RELOAD-')],
[sg.Checkbox('Use Media Titles', key='-TITLE-')],
[sg.Checkbox('Keep monitoring the thread', key='-MONITOR-')],
[sg.Button('Run', key='-RUN-', size=(10, 1)), sg.Button('Exit', key='-EXIT-', size=(10, 1))],
[sg.Output(size=(80, 10), key='-OUTPUT-')]
]

# Create the window
window = sg.Window('4chan-picrel / Media downloader', layout)

# Event loop
while True:
event, values = window.read()
if event == sg.WINDOW_CLOSED or event == '-EXIT-':
exit_script()
break
elif event == '-RUN-':
run_script()

22 changes: 14 additions & 8 deletions picrel.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

log = logging.getLogger('picrel')
workpath = os.path.dirname(os.path.realpath(__file__))
downldir = '' #user can have the 'download' folder here

args = None

Expand Down Expand Up @@ -40,7 +41,7 @@ def main():
thread = args.thread[0].strip()

if thread[:4].lower() == 'http':
download_thread(thread, args)
download_thread(thread, args, [])
else:
download_from_file(thread)

Expand Down Expand Up @@ -76,10 +77,10 @@ def download_thread(thread_link, args, downloaded_files):
if len(thread_link.split('/')) > 6:
thread_tmp = thread_link.split('/')[6].split('#')[0]

if args.use_names or os.path.exists(os.path.join(workpath, 'downloads', board, thread_tmp)):
if args.use_names or os.path.exists(os.path.join(workpath, downldir, board, thread_tmp)):
thread = thread_tmp

thread_folder = os.path.join(workpath, 'downloads', board, thread)
thread_folder = os.path.join(workpath, downldir, board, thread)
downloaded_files = load_downloaded_files(thread_folder) # Load the downloaded files from the JSON file
new_files_downloaded = False # Flag to track if new files were downloaded

Expand All @@ -92,7 +93,7 @@ def download_thread(thread_link, args, downloaded_files):
regex_result_len = len(regex_result)
regex_result_cnt = 1

directory = os.path.join(workpath, 'downloads', board, thread)
directory = os.path.join(workpath, downldir, board, thread)
if not os.path.exists(directory):
os.makedirs(directory)

Expand Down Expand Up @@ -158,17 +159,22 @@ def download_from_file(filename):
running_links = []
while True:
processes = []
#queue_file = os.path.join(os.path.abspath(workpath), filename)
queue_file = workpath + filename
#log.info('your queue path:' + queue_file)

if os.path.isabs(filename):
queue_file = filename
else:
queue_file = os.path.join(workpath, filename)

log.info('your queue path:' + queue_file)

for link in [_f for _f in [line.strip() for line in open(queue_file) if line[:4] == 'http'] if _f]:
if link not in running_links:
running_links.append(link)
log.info('Added ' + link)

board = link.split('/')[3]
thread = link.split('/')[5].split('#')[0]
thread_folder = os.path.join(workpath, 'downloads', board, thread)
thread_folder = os.path.join(workpath, downldir, board, thread)

downloaded_files = load_downloaded_files(thread_folder)
process = Process(target=call_download_thread, args=(link, args, downloaded_files))
Expand Down
88 changes: 88 additions & 0 deletions queue-creator-gui.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
import PySimpleGUI as sg
import subprocess
import threading
import sys

def run_queue_creator(board, query, queuefile, naming, thread_url=None, api_url=None, directory=False):
# Define the command to execute the queue-creator script
cmd = ['python', 'queue-creator.py', '-b', board, '-q', query, '-f', queuefile, '-n', naming]

if thread_url:
cmd.extend(['-u', thread_url])

if api_url:
cmd.extend(['-a', api_url])

if directory:
cmd.append('-d')

# Execute the queue-creator script
process = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)

while True:
line = process.stdout.readline()
if not line:
break
print(line.strip())

process.wait()

if process.returncode == 0:
print('Queue file created:', queuefile)
else:
print('Error:', process.stderr.read())

def create_queue_gui(board, query, queuefile, naming, thread_url=None, api_url=None, directory=False):
# Create a separate thread for running the queue-creator script
thread = threading.Thread(target=run_queue_creator, args=(board, query, queuefile, naming, thread_url, api_url, directory))
thread.start()

# Set the theme and font for the window
sg.theme('GreenMono')
sg.set_options(font=('Arial', 14))

# Define the layout
layout = [
[sg.Text('Board name:'), sg.Input(key='-BOARD-')],
[sg.Text('Search terms:'), sg.Input(key='-QUERY-')],
[sg.Text('Name of the Queue File:'), sg.Input(key='-QUEUEFILE-'), sg.FileSaveAs()],
[sg.Text('Search name:'), sg.Input(key='-NAMING-')],
#[sg.Text('Thread URL (optional):'), sg.Input(key='-THREADURL-')],
#[sg.Text('API URL (optional):'), sg.Input(key='-APIURL-')],
[sg.Checkbox('Save queue file in Board Directory', key='-DIRECTORY-')],
[sg.Button('Create Queue File'), sg.Button('Exit')],
[sg.Output(size=(60, 10), key='-OUTPUT-')]
]

# Create the window
window = sg.Window('4chan-picrel / Queue File Creator', layout)

# Event loop
while True:
event, values = window.read()
if event == sg.WINDOW_CLOSED or event == 'Exit':
break
elif event == 'Create Queue':
board = values['-BOARD-']
query = values['-QUERY-']
queuefile = values['-QUEUEFILE-']
naming = values['-NAMING-']
#thread_url = values['-THREADURL-']
#api_url = values['-APIURL-']
directory = values['-DIRECTORY-']

if not queuefile:
print('Please specify a queue file.')
elif not board:
print('Please specify a board.')
elif not query:
print('Please specify a query.')
elif not naming:
print('Please specify a naming convention.')
else:
#print('Creating queue file...')
#create_queue_gui(board, query, queuefile, naming, thread_url, api_url, directory)
create_queue_gui(board, query, queuefile, naming, directory)

# Close the window
window.close()
16 changes: 14 additions & 2 deletions queue-creator.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,14 @@
import json
import re
import os
import logging

# logging.basicConfig(level=logging.INFO)
# log = logging.getLogger('queue-creator')

API_URL_TEMPLATE = 'https://a.4cdn.org/{board}/catalog.json'
THREAD_URL_TEMPLATE = 'https://boards.4chan.org/{board}/thread/{id}/{name}'
downldir = '' #user can have the 'download' folder here

def get_threads(board, url_template=None):
template = url_template or API_URL_TEMPLATE
Expand All @@ -29,16 +34,23 @@ def main():
parser.add_argument('-u', '--thread-url', help='base url of the chans boards (default: https://boards.4chan.org/{board}/thread/{id}/{name})')
parser.add_argument('-a', '--api-url', help='base url of the chans api (default: https://a.4cdn.org/{board}/catalog.json)')
parser.add_argument('-d', '--directory', action='store_true', help='use or create the {board}/{name} directory, and place the queue file there')
#parser.add_argument('-t', '--time', action='store_true', help='add date to timestamp')
args = parser.parse_args()

# if args.time:
# logging.basicConfig(level=logging.INFO, format='[%(asctime)s] %(message)s', datefmt='%Y-%m-%d %I:%M:%S %p')
# else:
# logging.basicConfig(level=logging.INFO)

url_template = args.thread_url or THREAD_URL_TEMPLATE
name = args.naming.lower().replace(' ', '-')
query = re.compile(args.query)

if args.directory:
directory_path = os.path.join(args.board, name)
directory_path = os.path.join(os.getcwd(), downldir, args.board, name)
os.makedirs(directory_path, exist_ok=True)
queue_file_path = os.path.join(directory_path, args.queuefile)

else:
queue_file_path = args.queuefile

Expand All @@ -55,7 +67,7 @@ def main():
file.write('%s\n' % thread_url)
if args.verbose:
print(thread_url)

print('\nSuccess.\n' + '"' + args.queuefile + '" was saved here:\n' + queue_file_path + '\n')

if __name__ == '__main__':
try:
Expand Down
9 changes: 7 additions & 2 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,7 @@
beautifulsoup4==4.12.2
Django==4.2.1
asgiref==3.7.2
beautifulsoup4==4.12.2
Django==4.2.5
PySimpleGUI==4.60.5
soupsieve==2.5
sqlparse==0.4.4
tzdata==2023.3

0 comments on commit c869462

Please sign in to comment.