from __future__ import annotations
from typing import Type, TypeVar
import praw
import requests

__version__ = '0.1.0'

T_movie_poster_porn_scraper = TypeVar(
    'T_movie_poster_porn_scraper', bound="movie_poster_porn_scraper"
)


class movie_poster_porn_scraper(object):
    def __init__(self, instance: praw.Reddit) -> None:
        super().__init__()
        self.reddit_instance = instance

    @classmethod
    def create_instance(
        cls: Type(T_movie_poster_porn_scraper),
        client_id: str,
        client_secret: str,
        user_agent: str,
    ) -> T_movie_poster_porn_scraper:

        reddit_instance = praw.Reddit(
            client_id=client_id,
            client_secret=client_secret,
            user_agent=user_agent,
        )

        return cls(reddit_instance)

    def get_hot_posters(
        self: T_movie_poster_porn_scraper,
    ) -> T_movie_poster_porn_scraper:

        self._poster_urls = {}
        for post in self.reddit_instance.subreddit('MoviePosterPorn').hot(
            limit=10
        ):
            print(post.title)
            print(post.url)
            # print(dir(post))
            # self._poster_urls.append(post.url)
            self._poster_urls[post.title] = post.url
        print(self._poster_urls)
        return self

    def get_posters(self: T_movie_poster_porn_scraper):
        for title, url in self._poster_urls.items():
            r = requests.get(url)
            with open('posters/' + title + '.jpg', 'wb') as p:
                p.write(r.content)
        return self


inst = movie_poster_porn_scraper.create_instance(
    client_id='yb7NnBPh4riSnw',
    client_secret='-3Z0XUXD2XCiksfX26jORG107fA',
    user_agent='Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:70.0)'
    'Gecko/20100101 Firefox/70.0',
)

print(inst.reddit_instance.read_only)

inst.get_hot_posters().get_posters()
