import requests
from datetime import datetime
from bs4 import BeautifulSoup
import undetected_chromedriver as uc

class ScieceBrief:
    def __init__(self) -> None:
        
        self.start_date = datetime.now() # datetime.strptime('2023-06-06', '%Y-%m-%d')
        
        self.initial_year = 2016
        self.current_year = datetime.now().year
        
        pass
    
    def fetch(self, url) -> str:
        headers = {
            'accept':
            'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8',
            'user-agent': 'application/json, text/javascript, */*; q=0.01',
        }
        r = requests.get(url, headers=headers)
        r.raise_for_status()
        r.encoding = r.apparent_encoding
        return r.text
    
    def get_archive_list(self, year: int = None) -> list:
        """ 根据年份获取文章列表 """        
        # 年份为指定时为今年
        if year is None:
            year = self.current_year
            
        print(f'「SCIENCE BRIEF」正在获取 {year} 年文章列表...')
            
        # 判断是否小于初始年份
        if year < self.initial_year:
            print('「SCIENCE BRIEF」历史文章已全部获取。无法获取更多...')
            return []
        
        year_archive_html = self.fetch(
            f'https://www.science.org/pb/widgets/loi/content?widgetId=13edd6e9-129f-49a3-9346-5b522e6ed914&pbContext=;page:string:List of Issues;journal:journal:scirobotics;ctype:string:Journal Content;requestedJournal:journal:scirobotics;website:website:aaas-site;wgroup:string:Publication Websites;pageGroup:string:Publication Pages&id=y{year}'
        )
        print('「SCIENCE BRIEF」获取成功，正在解析...')
        
        # 解析 HTML
        soup = BeautifulSoup(year_archive_html, 'html.parser')
        # 所有过去期刊的 div
        past_issues = soup.find_all('div', class_='col-12 col-sm-3 col-lg-2 mb-4 mb-sm-3')
        
        # 文章列表
        archives = []
        for issue in past_issues:
            # 检查 <img> 标签是否存在
            img_tag = issue.find('img')
            if img_tag:
                # 提取 coverImage
                cover_image = img_tag['src']
            else:
                cover_image = None
            
            # 提取 archiveUrl
            anchor_tag = issue.find('a')
            if anchor_tag:
                archive_url = anchor_tag['href']
                title = anchor_tag['title']
            else:
                archive_url = None
                title = ''
            
            # 提取 volume 和 issue
            volume = issue.find('span', class_='past-issue__content__item--volume').text.strip()
            issue_num = issue.find('span', class_='past-issue__content__item--issue').text.strip()
            
            archives.append({
                'archiveUrl': f"https://www.science.org{archive_url}",
                'title': title,
                'volume': volume,
                'issue': issue_num,
                'coverImage': f"https://www.science.org{cover_image}"
            })
            
        print(f'「SCIENCE BRIEF」{year} 年文章列表获取成功，共计 {len(archives)} 期')
        return archives
    
    def get_acrticle_info(self, url: str):
        """ 获取期刊文章列表信息 """
        print('「SCIENCE BRIEF」正在获取期刊文章列表...')
        archive_html = self.fetch(url)
        print('「SCIENCE BRIEF」获取成功，正在解析...')
        # 解析 HTML
        soup = BeautifulSoup(archive_html, 'html.parser')
        
        # 找到toc__body元素
        toc_body = soup.find('div', class_='toc__body')

        # 遍历每个部分
        acrticle_infos = {}
        for section in toc_body.find_all('section', class_='toc__section'):
            # 提取分区标题
            section_title = section.find('h4').text.strip()

            # 找到部分中的所有文章卡片
            cards = section.find_all('div', class_='card')
            
            acrticle_infos[section_title] = []

            # 遍历每个文章卡片，提取信息
            for card in cards:
                # 提取标题
                title = card.find('h3', class_='article-title').text.strip()
                
                # 提取链接
                link = card.find('a', class_='sans-serif')['href']
                
                # 生成
                id = link[1:].replace('/', '_').replace('.', '-')

                # 提取日期
                date = card.find('time').text.strip()

                # 提取访问权限
                access = False if card.find('i', class_='icon-access-full') else True

                # 提取摘要
                summary = card.find('div', class_='card-body').text.strip()

                # 提取摘要
                abstract = card.find('div', class_='collapse')
                abstract_text = abstract.text.strip() if abstract else None

                acrticle_infos[section_title].append({
                    'id': id,
                    'title': title,
                    'link': f"https://www.science.org{link}",
                    'date': date,
                    'access': access,
                    'summary': summary,
                    'abstract': abstract_text
                })
                
        print(f'「SCIENCE BRIEF」期刊文章列表获取成功！共计 {len(acrticle_infos)} 个分区')
        return acrticle_infos
    
    def get_acrticle(self, url: str):
        """ 获取期刊文章内容 """
        print('「SCIENCE BRIEF」正在获取期刊文章...')

        print('「SCIENCE BRIEF」获取成功，正在解析...')
        # # 解析 HTML
        # soup = BeautifulSoup(acrticle_html, 'html.parser')
        
        # # 找到meta-panel元素
        # meta_panel = soup.find('div', class_='meta-panel')
        
        # print(meta_panel)

        # # 提取文章类型
        # article_type = meta_panel.find('div', class_='meta-panel__type').find('span').text.strip()

        # # 提取访问权限
        # access = meta_panel.find('div', class_='meta-panel__access').find('i')['data-original-title'] == "Free access"

        # # 打印提取的信息
        # print("Article Type:", article_type)
        # print("Access:", access)
        
    
sciecebrief_spider = ScieceBrief()
    
if __name__ == '__main__':
    # archives = sciecebrief_spider.get_archive_list()     
    # archive_info = sciecebrief_spider.get_acrticle_info('https://www.science.org/toc/scirobotics/8/78')
    
    archive = sciecebrief_spider.get_acrticle('https://www.science.org/doi/10.1126/scirobotics.adi2720')
    
    # import json
    # print(json.dumps(archive, indent=4))这个一天能爬多少条数据