
    GdiG                        d Z ddlmZmZmZ ddlmZmZ ddlZddlZ ee	      Z
dZe
j                  ddg      d	        Ze
j                  d
dg      d        Ze
j                  ddg      d        Ze	dk(  re
j!                  dd       yy)z(Simple Flask API to serve Funda listings    )Flaskjsonifyrequest)datetimedateNz/root/funda.leads/listings.jsonz/funda-todayGET)methodsc                  h   	 t        t        d      5 } t        j                  |       }ddd       t        j                         }g }D ]f  }|j                  d      s	 |d   j                  d	      d
   }t        j                  |d      j                         }||k(  r|j                  |       h t        t!        |      t#        |      |d      S # 1 sw Y   xY w# t        $ r t        ddi      dfcY S t        j                  $ r t        ddi      dfcY S w xY w# t        t        f$ r Y w xY w)zGet today's Funda listingsrNerrorNo listings file found  Invalid listings file  publish_dateTr   %Y-%m-%d)countr   listings)openLISTINGS_FILEjsonloadFileNotFoundErrorr   JSONDecodeErrorr   todaygetsplitr   strptimeappend
ValueErrorKeyErrorlenstr)fr   r   filteredlistingpub_strpub_dates          /root/funda.leads/api.pyget_today_listingsr+      s=   @-% 	$yy|H	$ JJLEH 	{{>*	n-33C8;G((*=BBDH5 (	 XE
  )	$ 	$ A!9:;S@@ @!89:C??@ H% 		s@   C CC ADCC D8!DDD10D1z/fundac                     	 t        t        d      5 } t        j                  |       }ddd       t        j                  j                  d      }t        j                  j                  d	      }t        j                  j                  d
      }|s|s|rg }D ]  }|j                  d      s	 |d   j                  d      d   }t        j                  |d      j                         }|r*t        j                  |d      j                         }	||	k7  r}|r*t        j                  |d      j                         }
||
k  r|r*t        j                  |d      j                         }||kD  r|j                  |        |}t        t#              |d      S # 1 sw Y   rxY w# t        $ r t        ddi      dfcY S t        j                  $ r t        ddi      dfcY S w xY w# t        t         f$ r Y aw xY w)a  
    Get Funda listings.
    
    Query params:
        date: Filter by publish date (YYYY-MM-DD format)
        from: Filter listings from this date onwards (YYYY-MM-DD)
        to: Filter listings up to this date (YYYY-MM-DD)
    
    Examples:
        /funda - All listings
        /funda?date=2026-01-12 - Only listings from Jan 12, 2026
        /funda?from=2026-01-10&to=2026-01-12 - Listings between dates
    r   Nr   r   r   r   r   r   fromtor   r   r   r   )r   r   )r   r   r   r   r   r   r   r   argsr   r   r   r   r   r    r!   r"   r#   )r%   r   date_filter	from_dateto_dater&   r'   r(   r)   filter_datefrom_dtto_dts               r*   get_listingsr6   )   s   @-% 	$yy|H	$ ,,""6*K  (Illt$Gi7 	G;;~.!.177<Q?#,,WjAFFH "*"3"3K"L"Q"Q"SK;.  &//	:FKKMG')  $--gzBGGIE%' (7	> X  _	$ 	$ A!9:;S@@ @!89:C??@L ) sR   F% FF% -A&G&+G& +G&,G&F"F% %G#?!G#"G#&G98G9z/funda/healthc                      t        ddi      S )zHealth check endpointstatusok)r        r*   healthr<   m   s     Hd#$$r;   __main__z0.0.0.0i  )hostport)__doc__flaskr   r   r   r   r   r   os__name__appr   router+   r6   r<   runr:   r;   r*   <module>rG      s    . ) ) #  	Ho1>E7+ ,: 8eW%A &AF ?UG,% -% zGGG& r;   