
    cfw                     r    d dl Z d dlZd dlZd dlZd dlmZ d dlmZm	Z	 ddl
mZ dZdZedz   Zd	 Zd
 Zd ZdS )    N)Bar)urlparseparse_qs   )corei4  zhttps://purr.neocities.orgz/mastodon-archive-import/c                   	 | j                             d          \  }}|dz   |z   dz   }t          j        |dd| j                  }|dz   |z   g 		fd}t          ||           	|d<   t          dt          d	           d
 }dt          	          dk    r-| j	        s&t          dt          	          dz             fd}t          t          t          |	|           r                                 t          d           dS )z4
    Find and serve all archive files for Meow.
    @z.user.z.jsonT)requiredquietcombinec                     t          |           j        }|v r|S |z   }t          j                            |          r                    |           |S | S )a!  
        Checks if we have the file, in which case returns the relative path, so
        that Meow knows to look it up in the local storage. Otherwise, returns
        the URL, so Meow will try to load it remotely. Adds relative paths to
        media_files to serve them to Meow.
        )r   pathosisfileappend)urlr   	file_name	media_dirmedia_filess      c/home/feoh/.local/pipx/venvs/mastodon-archive/lib/python3.11/site-packages/mastodon_archive/meow.pyuse_local_file_if_existsz&meow.<locals>.use_local_file_if_exists)   sa     }}!;K$	7>>)$$ 	t$$$KJ    fileszPlease, open Meow atzto continue!c                      d S N )argss    r   <lambda>zmeow.<locals>.<lambda>C   s    D r   Nr   zExporting filesr   )maxc                  ,                                     S r   )next)r   bars    r   r   zmeow.<locals>.<lambda>I   s    CHHJJ r   zExport finished!)usersplitr   loadr   transform_media_urlsprintmeow_open_pathlenr   r   serveserver_portmeow_originfinish)
r   usernamedomainstatus_filedatar   file_cbr"   r   r   s
          @@@r   meowr3      sH    --Xv8#h.8K9[4tT\RRRD!H,IK     * 7888DM	
 ..AAA  G
C
;!z 	/'s;/?/?!/CDDDC....G	+{D)['JJJ
 

	
r   c                     dD ]L}| |         D ]A}|d         }|d         |d         d         }|D ]}|d         r ||d                   |d<   BMdD ]"} || d         |                   | d         |<   #dS )zN
    Calls func on each media file URL and sets the latter to the result.
    )statuses
favourites	bookmarksmedia_attachmentsreblogNr   )avatarheaderaccountr   )r1   func
collectionstatusattachments
attachmentpictures          r   r&   r&   Q   s     > @ @
:& 	@ 	@F !45Kh+$X./BC) @ @
e$ @(,Z->(?(?Ju%@		@ ( B B#'4Y(@#A#AY  B Br   c                 :  	 d		fd} G 	fddt           j        j                  }dt          j        _        t          j        d| f|          5 } |            r|                                  |            d d d            d S # 1 swxY w Y   d S )NFc                  
      S r   r   )completes   r   not_completedzserve.<locals>.not_completedc   s    |r   c                   *    e Zd Z fdZd ZdS )serve.<locals>.Handlerc                    t          t          | j                  j                  }| j        dk    r|                     d           |                     d	           |                     dd           |                                  | j                            t          t          j                  d                                   d S d|v r|d         d         v r|                     d           |                     d	           |                                  |d         d         z   }t          |d	          5 }| j                            |                                           d d d            n# 1 swxY w Y                 d S d
|v r-|                     d           |                                  dd S |                     d           d S )N/   zAccess-Control-Allow-OriginzContent-typezapplication/jsonzutf-8filer   rbrE   Ti  )r   r   r   querysend_responsesend_headerend_headerswfilewritebytesjsondumpsopenread
send_error)
selfrN   r   rL   rE   r1   r2   r   r   origins
       r   do_GETzserve.<locals>.Handler.do_GETh   s   Xdi00677EyC""3'''  !>GGG  1CDDD  """
  tz$'7'7!A!ABBB					5U6]1%5%D%D""3'''  !>GGG  """%fa(88	)T** 2dJ$$TYY[[1112 2 2 2 2 2 2 2 2 2 2 2 2 2 2 					u$$""3'''  """$$$$$s   -E;;E?E?c                     d S r   r   )rZ   formatr   s      r   log_messagez"serve.<locals>.Handler.log_message   s    Fr   N)__name__
__module____qualname__r\   r_   )rE   r1   r2   r   r   r[   s   r   HandlerrH   g   sV        	% 	% 	% 	% 	% 	% 	% 	% 	% 	%>	 	 	 	 	r   rc   Tz	127.0.0.1)httpserverBaseHTTPRequestHandlersocketserver	TCPServerallow_reuse_addresshandle_request)
portr[   r1   r   r   r2   rF   rc   httpdrE   s
    `````   @r   r*   r*   a   s/   H    ! ! ! ! ! ! ! ! ! ! ! !$+4 ! ! !F 26L.		d 3W	=	= #moo 	#  """ moo 	## # # # # # # # # # # # # # # # # #s   )BBB)r   rU   http.serverrd   rg   progress.barr   urllib.parser   r    r   r+   r,   r(   r3   r&   r*   r   r   r   <module>rq      s   " 
			                + + + + + + + +      *::2 2 2hB B B ,# ,# ,# ,# ,#r   