
    Q_i                     ^    d Z ddlZddlmZ ddlmZ ddlmZmZ ddlm	Z	m
Z
  G d d	e      Zy)
z!Google Trends RSS feed collector.    N)List)datetime   )BaseCollector	TrendItem)MAX_ITEMS_PER_SOURCETRENDS_HOURSc                   H     e Zd ZdZ fdZdedefdZdedee   fdZ	 xZ
S )GoogleTrendsCollectorz5Collects trending topics from Google Trends RSS feed.c                 $    t         |   d       y )Ngoogle_trends)super__init__)self	__class__s    B/home/sietch6/trending-topics-pipeline/collectors/google_trends.pyr   zGoogleTrendsCollector.__init__   s    )    regionreturnc                     d| dt          S )z$Build the RSS feed URL for a region.z+https://trends.google.com/trending/rss?geo=z&hours=)r	   )r   r   s     r   _get_feed_urlz#GoogleTrendsCollector._get_feed_url   s    <VHGL>ZZr   c                    | j                  |      }t        j                  |      }|j                  r$|j                  st        d|j                         g }t        |j                  dt         d      D ]  \  }}t        |dd      }g }t        |d      r_|j                  }	t        |	t              r |	D 
cg c]  }
|
j                  dd       }}
n#t        |	t              r|	j                  dd      g}|j!                  t#        |j$                  d	||t'        j(                         j+                         |j                  d
      |||j                  dd      d              |S c c}
w )z
        Collect trending topics from Google Trends RSS.

        Args:
            region: Two-letter region code (US, CA)

        Returns:
            List of TrendItem objects
        zFailed to parse feed: Nr   )startht_approx_traffic ht_news_itemht_news_item_titler   link	published)traffic
news_itemsr   )
topic_namesourcerankr   	timestampurlmetadata)r   
feedparserparsebozoentries
ValueErrorbozo_exception	enumerater   getattrhasattrr   
isinstancelistgetdictappendr   titler   utcnow	isoformat)r   r   r&   feeditemsr$   entryr    r!   	news_datans              r   collectzGoogleTrendsCollector.collect   sS      ($99T\\5d6I6I5JKLL$T\\2G3G%HPQR 	KD%e%8"=G Jun-!..	i.KT!Ua!%%(<b"A!UJ!U	40"+--0Db"I!JJLL${{*!&oo/99;		&)#*&0%*YY{B%?	: ) "Vs   8E))__name__
__module____qualname____doc__r   strr   r   r   r>   __classcell__)r   s   @r   r   r      s8    ?*[C [C [.c .d9o .r   r   )rB   r(   typingr   r   baser   r   configr   r	   r    r   r   <module>rI      s#    '    * 58M 8r   