#!/usr/pkg/bin/python3.13
# vim:fileencoding=utf-8
from __future__ import absolute_import, division, print_function, unicode_literals

import re
import shutil

try:
    from urllib.parse import urlencode
except ImportError:
    from urllib import urlencode
from calibre.ptempfile import PersistentTemporaryDirectory, PersistentTemporaryFile
from calibre.web.feeds.news import BasicNewsRecipe


class AdvancedUserRecipe1515196393(BasicNewsRecipe):
    title = "The Galaxy's Edge"
    __author__ = 'andyh2000'
    delay = 2
    oldest_article = 7
    max_articles_per_feed = 100
    auto_cleanup   = True
    language = 'en'
    encoding = 'utf8'
    no_stylesheets = True

    extra_css = '.photo-caption { font-size: smaller }'

    def parse_index(self):
        soup = self.index_to_soup('http://www.galaxysedge.com/')
        cover_image = soup.find('div', attrs={'class':'ci-img'})
        cover_image = cover_image.find('img')
        self.cover_url = cover_image['src']
        issue_title = soup.find('h1')
        self.title = "Galaxy's Edge: " + self.tag_to_string(issue_title).lower().title()
        toc = soup.find('div', attrs={'class':'nav-tabs'})
        current_section = "Articles"
        current_articles = []
        feeds = []
        br = self.get_browser()
        self.ctdir = PersistentTemporaryDirectory()
        for x in toc.findAll(['li'], attrs={"class": re.compile(".*get_content.*")}):
            edwo = x.find('a')
            title = self.tag_to_string(edwo)
            self.log('\t\tFound article:', title)
            post_id = x["data-post-id"]
            cat_id = x["data-cat-id"]
            parent_id = x["data-parent-id"]
            self.log('\t\tdata-parent-id', parent_id)
            self.log('\t\tdata-cat-id', cat_id)
            self.log('\t\tdata-post-id', post_id)
            data = urlencode({'action':'get_content', 'cat_id':cat_id, 'parent_id':parent_id, 'post_id':post_id})
            r=br.open('http://www.galaxysedge.com/wp-content/themes/galaxyedge/get_content.php', data)
            content_file = PersistentTemporaryFile(suffix='.html', dir=self.ctdir)
            content_file.write(r.read())
            content_file.close()
            current_articles.append({'title': title, 'url':'file://' + content_file.name, 'description':'', 'date':''})
        if current_articles and current_section:
            feeds.append((current_section, current_articles))

        return feeds

    def cleanup(self):
        self.log("Deleting temp files...")
        shutil.rmtree(self.ctdir)
