コード例 #1
0
def sen_gif(message):
    bool_ban = SendTrackMessage(message, 'ugoira')
    if bool_ban:
        bot.reply_to(message, '你/频道/群组 被BAN了, 请私聊bot确认是否为个人被ban')
        return 0

    pixiv_id = -1
    try:
        pixiv_id = message.text.split(' ')[1]
        int(pixiv_id)
        api = AppPixivAPI()
        api.login("your username", "pswe")
        file, title, tags, artist, _type = PixivDownloadUgoiraZip(
            pixiv_id, api)

        if _type != 'ugoira':
            bot.send_message(message.chat.id, '该图片不是ugoira,请使用id命令')
        else:
            caption = 'title : ' + title + '\n' + 'url : pixiv.net/i/' + pixiv_id + '\n' + 'tags : '
            for tag in tags:
                if len(caption) + len(tag) < 193:
                    caption = caption + tag + '   '
            bot.send_document(message.chat.id, file, caption)

    except:
        bot.send_message(message.chat.id, '満身創痍|使用范例: /ugoira 75162826')
コード例 #2
0
ファイル: api.py プロジェクト: twenty5151/koneko-old
 def _login(self):
     """
     Logins to pixiv in the background, using credentials from config file.
     """
     api = AppPixivAPI()
     api.login(self._credentials['Username'], self._credentials['Password'])
     self.api_queue.put(api)
コード例 #3
0
ファイル: extract.py プロジェクト: CrackedP0t/ErrantBot
def pixiv(page_url, options):
    from pixivpy3 import AppPixivAPI

    parsed = urlparse(page_url)

    id = int(parsed.path.split("/")[-1])

    api = AppPixivAPI()

    secrets = h.get_secrets()["pixiv"]
    api.login(secrets["username"], secrets["password"])

    data = api.illust_detail(id)["illust"]

    if len(data["meta_pages"]) == 0:
        image_url = data["meta_single_page"]["original_image_url"]
    elif options["album"]:
        image_url = [
            image["image_urls"]["original"] for image in data["meta_pages"]
        ]
    else:
        image_url = data["meta_pages"][
            options["index"]]["image_urls"]["original"]

    return Work(
        data["title"],
        (
            data["user"]["account" if options["username"] else "name"],
            data["user"]["name" if options["username"] else "account"],
        ),
        None,
        data["x_restrict"] > 0,
        image_url,
        page_url,
    )
コード例 #4
0
def send_picture_to_channel(message):
    if int(message.chat.id) not in white_list:
        try:
            temp_text = message.chat.title + '使用id命令' + '    ' + str(
                message.chat.id) + '    ' + message.chat.username
        except:
            temp_text = 'A Null is captured'
        bot.send_message('your chat_id', temp_text)

    bool_ban = False
    if int(message.chat.id) in black_list:
        bool_ban = True
    if bool_ban:
        bot.reply_to(message, '你/频道/群组 被BAN了, 请私聊bot确认是否为个人被ban')
        return 0

    pixiv_id = -1
    try:
        pixiv_id = message.text.split(' ')[1]
        int(pixiv_id)
        api = AppPixivAPI()
        api.login("your username", "pswe")
        file, title, tags, artist = PixivDownloadOrigin(pixiv_id, api)
        if file == -1:
            bot.send_message(message.chat.id, '该图片不存在|Picture does not exist')
        else:
            caption = 'title : ' + title + '\n' + 'artist : ' + artist + '\n' + 'url : pixiv.net/i/' + pixiv_id + '\n' + 'tags : '
            for tag in tags:
                if len(caption) + len(tag) < 193:
                    caption = caption + '#' + tag + '  '
            bot.send_photo(message.chat.id, file, caption)
    except:
        bot.send_message(message.chat.id, '満身創痍|使用范例: /id 43369925')
コード例 #5
0
ファイル: bookmarksp.py プロジェクト: KernelErr/PixivURLs
def main():
    aapi = AppPixivAPI()
    aapi.login(username, password)
    illust_ids = set()
    for illust in get_imageUrls(aapi):
        illust_ids.add(illust.id)

    print('Images Found:'+str(len(illust_ids)))
コード例 #6
0
ファイル: main.py プロジェクト: kaevowen/archive
 def login_push(self):
     self.ID = self.lineEdit.text()
     self.PW = self.lineEdit_2.text()
     try:
         api = AppPixivAPI()
         api.login(self.ID, self.PW)
         print('connected')
     except pixivpy3.utils.PixivError:
         msgbox = QMessageBox
         msgbox.critical(self, "Error", "Invaild ID or Password", msgbox.Ok)
コード例 #7
0
ファイル: pixiv.py プロジェクト: zhuzenghui666/PickTrue
class Pixiv(DummySite):
    def __init__(self, url, username, password, proxy=None):
        proxies = get_proxy(proxy)
        requests_kwargs = {
            "timeout": (3, 10),
        }
        requests_kwargs.update(proxies)
        self.api = AppPixivAPI(**requests_kwargs)
        self._fetcher = PixivFetcher(**proxies)
        self.api.login(username, password)
        self._user_id = int(url.split("/")[-1])
        self._dir_name = None
        self._total_illustrations = 0
        self._fetch_user_detail()

    @property
    def fetcher(self):
        return self._fetcher

    @property
    def dir_name(self):
        assert self._dir_name is not None
        return self._dir_name

    def _fetch_user_detail(self):
        assert self._user_id is not None
        profile = self.api.user_detail(self._user_id)
        user = profile['user']
        self._dir_name = "-".join([
            user['name'],
            user['account'],
            str(user['id']),
        ])
        self._dir_name = normalize_filename(self._dir_name)
        self._total_illustrations = profile['profile']['total_illusts']
        return self.dir_name

    def _fetch_image_list(self, ):
        ret = self.api.user_illusts(self._user_id)
        while True:
            for illustration in ret.illusts:
                yield from parse_image_urls(illustration)
            if ret.next_url is None:
                break
            ret = self.api.user_illusts(**self.api.parse_qs(ret.next_url))

    def _fetch_single_image_url(self, illustration_id):
        json_result = self.api.illust_detail(illustration_id)
        illustration_info = json_result.illust
        return illustration_info.image_urls['large']

    @property
    def tasks(self):
        yield from self._fetch_image_list()
コード例 #8
0
ファイル: utils.py プロジェクト: yiyang-hsu/pixiv-tgbot
def get_image_info(url: str):
    if ('pixiv' in url):
        api = AppPixivAPI()
        api.login(PIXIV_USERNAME, PIXIV_PASSWORD)
        if '?' in url:
            pixiv_id = int(
                dict(i.split('=')
                     for i in url.split('?')[-1].split('&'))['illust_id'])
        else:
            pixiv_id = int(url.split('/')[-1])
        return pixiv_download(pixiv_id, api)
    else:
        return {}
コード例 #9
0
ファイル: pixivpy.py プロジェクト: PencilCore/Satania-QQBot
def login():
    username = request.json['username']
    password = request.json['password']

    try:
        api = AppPixivAPI()
        api.login(username, password)

        pixivclients[username] = api

        return jsonify({'code': 0})
    except Exception as err:
        print(err)
        return jsonify({'code': -1})
コード例 #10
0
def send_related(message):
    bool_ban = SendTrackMessage(message, 'related')
    if bool_ban:
        bot.reply_to(message, '你/频道/群组 被BAN了, 请私聊bot确认是否为个人被ban')
        return 0

    try:
        split_list = message.text.split(' ')
        if len(split_list) > 3 or len(split_list) < 2:
            bot.send_message(message.chat.id, '満身創痍|使用范例: /related 43369925 5')
            return -1
        elif len(split_list) == 3:
            origin_id = split_list[1]
            num = split_list[2]
        elif len(split_list) == 2:
            origin_id = split_list[1]
            num = 5
        int(origin_id)
        num = int(num)

        if num <= 0:
            bot.send_message(message.chat.id, '満身創痍|使用范例: /related 43369925 5')
        else:
            api = AppPixivAPI()
            api.login("your username", "pswe")
            id_list = PixivRelated(origin_id, num, api)
            if id_list == []:
                bot.send_message(
                    message.chat.id,
                    '该图片不存在或没有相关图片|Picture does not exist or has no related illustrations'
                )
            elif len(id_list) < 6:
                for i in id_list:
                    file, title, tags = PixivDownload(i, api)
                    caption = 'url : pixiv.net/i/' + str(
                        i) + '\n' + 'title : ' + title + '\n' + 'tags : '
                    for tag in tags:
                        if len(caption) + len(tag) < 193:
                            caption = caption + tag + '    '
                    bot.send_photo(message.chat.id, file, caption)
            else:
                file_list = []
                for i in id_list:
                    temp_file, temp1, temp2 = PixivDownload(i, api)
                    file_list.append(InputMediaPhoto(temp_file))
                bot.send_media_group(message.chat.id, file_list)

    except:
        bot.send_message(message.chat.id, '満身創痍|使用范例: /related 43369925 5')
コード例 #11
0
ファイル: builders.py プロジェクト: pixelzery/cols
def pixiv_render(item, base_path, debug=False):
    global pixiv_api
    if pixiv_api is None:
        pixiv_api = AppPixivAPI()
        pixiv_api.login(pixiv_username, pixiv_password)

    illust_id = get_illust_id(item.get_remote())

    detail = pixiv_api.illust_detail(illust_id)
    path = (str(detail['illust']['user']['name']) + '_' +
            str(detail['illust']['user']['id']))
    cpath(base_path + path)

    urls = []
    if detail['illust']['page_count'] > 1:
        for page in detail['illust']['meta_pages']:
            page_url = None
            try:
                page_url = page['image_urls']['original']
            except (NameError, KeyError):
                try:
                    page_url = list(page['image_urls'].values())[-1]
                except (NameError, KeyError):
                    pass
            if page_url is not None:
                urls.append(page_url)
    if len(urls) <= 0:
        try:
            urls.append(
                detail['illust']['meta_single_page']['original_image_url'])
        except (NameError, KeyError):
            try:
                urls.append(detail['illust']['image_urls']['large'])
            except (NameError, KeyError):
                pass

    ret = []
    for url in urls:
        name = str(detail['illust']['title']) + '_' + str(
            illust_id) + os.path.basename(url)
        ret.append(path + '/' + name)
        pixiv_api.download(url,
                           name=name,
                           path=os.path.abspath(base_path + path))
        if debug: print('.', end='', flush=True)
    return ret, detail
コード例 #12
0
def send_top(message):
    bool_ban = SendTrackMessage(message, 'ranking')
    if bool_ban:
        bot.reply_to(message, '你/频道/群组 被BAN了, 请私聊bot确认是否为个人被ban')
        return 0

    support_freq = ['day', 'week', 'month', 'day_male', 'day_female']
    try:
        split_list = message.text.split(' ')
        if len(split_list) > 3 or len(split_list) < 2:
            bot.send_message(message.chat.id, '満身創痍|使用范例: /ranking day 6')
            return -1
        elif len(split_list) == 3:
            frequence = split_list[1]
            num = split_list[2]
        elif len(split_list) == 2:
            frequence = split_list[1]
            num = 6
        num = int(num)

        if frequence not in support_freq:
            bot.send_message(message.chat.id,
                             '不支持的关键词,请输入day,week,month,day_male或day_female')
        else:
            api = AppPixivAPI()
            api.login("your username", "pswe")
            pixiv_id = PixivRanking(frequence, num, api)
            if num < 6:
                for i in pixiv_id:
                    file, title, tags = PixivDownload(i, api)
                    caption = 'url : pixiv.net/i/' + str(
                        i) + '\n' + 'title : ' + title + '\n' + 'tags : '
                    for tag in tags:
                        if len(caption) + len(tag) < 193:
                            caption = caption + tag + '    '
                    bot.send_photo(message.chat.id, file, caption)
            else:
                file_list = []
                for i in pixiv_id:
                    temp_file, temp1, temp2 = PixivDownload(i, api)
                    file_list.append(InputMediaPhoto(temp_file))
                bot.send_media_group(message.chat.id, file_list)
    except:
        bot.send_message(message.chat.id, '満身創痍|使用范例: /ranking day 6')
コード例 #13
0
def send_file(message):
    bool_ban = SendTrackMessage(message, 'file')
    if bool_ban:
        bot.reply_to(message, '你/频道/群组 被BAN了, 请私聊bot确认是否为个人被ban')
        return 0

    pixiv_id = -1
    try:
        pixiv_id = message.text.split(' ')[1]
        int(pixiv_id)
        api = AppPixivAPI()
        api.login("your username", "pswe")
        file, title, tags, artist = PixivDownloadOrigin(pixiv_id, api)
        if file == -1:
            bot.send_message(message.chat.id, '该图片不存在|Picture does not exist')
        else:
            bot.send_document(message.chat.id, file)
    except:
        bot.send_message(message.chat.id, '満身創痍|使用范例: /file 43369925')
コード例 #14
0
class Pixiv(commands.Cog):
    def __init__(self, bot):
        self.bot = bot
        conf = config.load_config()
        self.api = AppPixivAPI()
        self.api.login(conf["pixiv_username"], conf["pixiv_password"])

    @commands.command(name="Pixiv", aliases=("pixiv", "PIXIV"))
    async def pixiv(self, ctx, option):
        mode = ""
        if option == "推荐":
            mode = "day"
        elif option == "色图" or option == "涩图" or option == "setu":
            mode = "day_r18"

        result = self.api.illust_ranking(mode)
        illusts = result.illusts[:10]
        for illust in illusts:
            await ctx.send(
                f"{illust.title} - https://www.pixiv.net/artworks/{illust.id}")
        else:
            await ctx.send(f"未知选项:{option}")
コード例 #15
0
def send_picture(message):
    bool_ban = SendTrackMessage(message, 'id')
    if bool_ban:
        bot.reply_to(message, '你/频道/群组 被BAN了, 请私聊bot确认是否为个人被ban')
        return 0

    pixiv_id = -1
    try:
        pixiv_id = message.text.split(' ')[1]
        int(pixiv_id)
        api = AppPixivAPI()
        api.login("your username", "pswe")
        file, title, tags, artist = PixivDownloadOrigin(pixiv_id, api)
        if file == -1:
            bot.send_message(message.chat.id, '该图片不存在|Picture does not exist')
        else:
            caption = 'title : ' + title + '\n' + 'url : pixiv.net/i/' + pixiv_id + '\n' + 'tags : '
            for tag in tags:
                if len(caption) + len(tag) < 193:
                    caption = caption + tag + '   '
            bot.send_photo(message.chat.id, file, caption)
    except:
        bot.send_message(message.chat.id, '満身創痍|使用范例: /id 43369925')
コード例 #16
0
def send_ugoira_to_channel(message):
    if int(message.chat.id) not in white_list:
        try:
            temp_text = message.chat.title + '使用ugoira命令' + '    ' + str(
                message.chat.id) + '    ' + message.chat.username
        except:
            temp_text = 'A Null is captured'
        bot.send_message('your chat_id', temp_text)

    bool_ban = False
    if int(message.chat.id) in black_list:
        bool_ban = True
    if bool_ban:
        bot.reply_to(message, '你/频道/群组 被BAN了, 请私聊bot确认是否为个人被ban')
        return 0

    pixiv_id = -1
    try:
        pixiv_id = message.text.split(' ')[1]
        int(pixiv_id)
        api = AppPixivAPI()
        api.login("your username", "pswe")
        file, title, tags, artist, _type = PixivDownloadUgoiraZip(
            pixiv_id, api)

        if _type != 'ugoira':
            bot.send_message(message.chat.id, '该图片不是ugoira,请使用id命令')
        else:
            caption = 'title : ' + title + '\n' + 'url : pixiv.net/i/' + pixiv_id + '\n' + 'tags : '
            for tag in tags:
                if len(caption) + len(tag) < 193:
                    caption = caption + tag + '   '
            bot.send_document(message.chat.id, file, caption)

    except:
        bot.send_message(message.chat.id, '満身創痍|使用范例: /ugoira 75162826')
コード例 #17
0
from pixivpy_async import PixivClient
from pixivpy_async import AppPixivAPI
from pixivpy_async import PixivAPI
from pixivpy3 import AppPixivAPI as Sync_aapi
from pixivpy3 import PixivAPI as Sync_papi

sys.dont_write_bytecode = True

_USERNAME = "******"
_PASSWORD = "******"
_TOKEN = "uXooTT7xz9v4mflnZqJUO7po9W5ciouhKrIDnI2Dv3c"

saapi = Sync_aapi()
# saapi.login(_USERNAME, _PASSWORD)
saapi.login(refresh_token=_TOKEN)
spapi = Sync_papi()
# spapi.login(_USERNAME, _PASSWORD)
spapi.login(refresh_token=_TOKEN)


def gen_date():
    """201x-0x-xx"""
    year = random.randint(3, 9)
    month = random.randint(1, 9)
    day = random.randint(10, 29)
    return '201%s-0%s-%s' % (year, month, day)


def test_sync_illust(num):
    e = time.time()
コード例 #18
0
class PixivDownloader:
    def __init__(self,
                 client=None,
                 username=None,
                 password=None,
                 log_level=logging.WARNING):
        if not client and (bool(username) != bool(password)):
            raise AttributeError(
                'If no client is given both username and password must be given'
            )

        if client:
            self.api = client
        else:
            self.api = AppPixivAPI()

        if not client and username and password:
            self.api.login(username, password)

        self.logger = logging.getLogger('PixivDownloader')
        stdout = logging.StreamHandler()
        self.logger.addHandler(stdout)
        self.logger.setLevel(log_level)

    def login(self, username=None, password=None, refresh_token=None):
        if refresh_token:
            self.logger.info('Loging in with refresh_token')
        elif username:
            self.logger.info('Loging in with username %s', username)
        else:
            self.logger.info('Loging')

        return self.api.auth(username=username,
                             password=password,
                             refresh_token=refresh_token)

    def logout(self):
        self.logger.info('Logout')
        self.api = AppPixivAPI()

    def get_id_from_url(self, url):
        path = urlparse(url).path
        ids = re.findall('(\\d+)', path)
        if not ids:
            raise ValueError('Url does not contain post id')

        return ids[0]

    def download_by_id(self, post_id, output_dir):
        data = self.api.illust_detail(post_id)
        if data.get('error'):
            raise PixivDownloaderError(
                'Could not get post info or post doesn\'t exist.', data)

        return self.download(data.illust, output_dir)

    def download_by_url(self, url, output_dir):
        return self.download_by_id(self.get_id_from_url(url), output_dir)

    def download(self, post, output_dir):
        output_dir = Path(output_dir).expanduser().absolute()
        if not os.path.isdir(output_dir):
            os.makedirs(output_dir)
            self.logger.debug('Created dir "%s"', output_dir)

        if post.type == 'illust' and not post.meta_pages:
            downloader = self.download_illust
            type = 'Image'
        elif post.type == 'illust' and post.meta_pages:
            downloader = self.download_illust_collection
            type = 'Image Collection'
        elif post.type == 'ugoira':
            downloader = self.download_ugoira
            type = 'Video'
        elif post.type == 'manga':
            downloader = self.download_manga
            type = 'Manga'
        else:
            raise PixivDownloaderError(
                f'Post type "{post.type}" not supported')

        self.logger.info('Initialize "%s" downloader for post %s', type,
                         post.id)
        return downloader(post, output_dir)

    def download_illust(self, post, output_dir):
        image_url = post.meta_single_page.get('original_image_url',
                                              post.image_urls.large)
        if '_webp' in image_url:
            extension = 'webp'
        else:
            extension = os.path.splitext(image_url)[1].lstrip('.')
        filename = self.get_filename(post, extension)

        self.logger.info('Downloading "%s"', image_url)
        self.api.download(image_url,
                          path=output_dir,
                          name=filename,
                          replace=True)
        yield (Path(output_dir) / filename).absolute()

    def download_illust_collection(self, post, output_dir):
        output_dir = Path(output_dir)
        yield from self._downloade_meta_pages(post, output_dir)

    def download_manga(self, post, output_dir):
        output_dir = Path(output_dir) / f'{post.title}-{post.user.account}'
        if not output_dir.is_dir():
            output_dir.mkdir(parents=True, exist_ok=True)
            self.logger.debug('Created dir "%s"', output_dir)

        yield from self._downloade_meta_pages(post, output_dir)

    def _downloade_meta_pages(self, post, output_dir):
        for index, image in enumerate(post.meta_pages, 1):
            image_url = image.image_urls.get('original',
                                             image.image_urls.large)

            if '_webp' in image_url:
                extension = 'webp'
            else:
                extension = os.path.splitext(image_url)[1].lstrip('.')
            filename = self.get_filename(post,
                                         extension,
                                         suffix=f'-{index:0>2}')

            self.logger.info('Downloading "%s"', image_url)
            self.api.download(image_url,
                              path=str(output_dir),
                              name=filename,
                              replace=True)
            yield (output_dir / filename).absolute()

    def download_ugoira(self, post, output_dir):
        ugoira_data = self.api.ugoira_metadata(post.id).ugoira_metadata
        zip_url = ugoira_data.zip_urls.get('large',
                                           ugoira_data.zip_urls.medium)

        with TemporaryDirectory() as dir:
            temp_dir = Path(dir)
            filename = '{post.id}.zip'
            self.logger.info('Downloading "%s"', zip_url)
            self.api.download(zip_url, path=str(temp_dir), name=filename)

            frames_dir = temp_dir / 'frames'
            os.mkdir(frames_dir)

            self._extract_zip(temp_dir / filename, frames_dir)

            video_name = self.get_filename(post, 'mp4')
            video_file = temp_dir / video_name
            self._generate_mp4_from_frames(video_file, frames_dir,
                                           ugoira_data.frames[0].delay)

            final_path = (Path(output_dir) / video_name).absolute()
            shutil.move(video_file, final_path)
            yield final_path.absolute()

    def get_filename(
        self,
        post,
        extension,
        prefix=None,
        suffix=None,
    ):
        suffix = suffix or ''
        prefix = prefix or ''
        filename = f'{prefix}{post.id}-{post.title}{suffix}.{extension}'.replace(
            '/', '_').replace(' ', '_')
        return filename

    def _extract_zip(self, zip_file, output_dir):
        self.logger.info('Extract "%s"', zip_file)
        with ZipFile(zip_file, 'r') as zip_file:
            zip_file.extractall(output_dir)

    def _generate_mp4_from_frames(self, output_file, frames_dir, delay):
        self.logger.info('Generate video to "%s"', output_file)
        frames = sorted(
            map(lambda file: os.path.join(str(frames_dir), file),
                os.listdir(frames_dir)))
        frames = list(map(imread, frames))

        framerate = 1000 / delay

        height, width, layers = frames[0].shape
        video = VideoWriter(str(output_file), VideoWriter_fourcc(*'mp4v'),
                            framerate, (width, height))

        for frame in frames:
            video.write(frame)

        destroyAllWindows()
        video.release()
コード例 #19
0
ファイル: login_env.py プロジェクト: leon92101/pixivpy-async
def test0():
    api = AAPI()
    api.login(_USERNAME, _PASSWORD)
    print("test0 - finished")
コード例 #20
0
class PixivSpider:
    def __init__(self):
        """
        Init PixivSpider
        """
        self.api = AppPixivAPI()
        self.directory = 'download'
        if not os.path.exists('info.json'):
            self.data = {'illusts': []}
            self.count = 0
            print("Create new info.json file")
        else:
            with open('info.json', 'r') as f:
                self.data = json.load(f)
                self.count = len(self.data['illusts'])
                print("Load existing info.json file")
                print("Existed illusts count: %d" % self.count)
        self.illusts_names = Set()
        for illust in self.data['illusts']:
            self.illusts_names.add(illust['name'])

    def login(self):
        """
        Login pixiv.net
        """
        with open('login.json') as f:
            login = json.load(f)
            self.api.login(login["username"], login["password"])
            print("Login pixiv.net with user %s.", login["username"])

    def exit(self):
        """
        Stop spider and print logs
        """
        with open('info.json', 'w') as f:
            json.dump(self.data, f, indent=2)
        print("Finish! Total downloaded illusts number: %d" % self.count)

    def create_download_folder(self):
        """
        Setup image download directory
        """
        if not os.path.exists(self.directory):
            os.makedirs(self.directory)

    def download_illusts(self, illusts=None):
        """
        Download illusts
        """
        for illust in illusts:
            image_url = illust.meta_single_page.get('original_image_url',
                                                    illust.image_urls.large)
            print(u"👀  Found illust: %s (%s)" % (illust.title, image_url))
            url_basename = os.path.basename(image_url)
            extension = os.path.splitext(url_basename)[1]
            name = "%d_%s%s" % (illust.id, illust.title, extension)
            name = name.replace('/', ':')
            if name not in self.illusts_names:
                self.count += 1
                self.data['illusts'].append({
                    'id': self.count,
                    'name': name,
                    'illust_id': illust.id,
                    'illustrator_id': illust.user.id,
                    'source_url': image_url
                })
                self.illusts_names.add(name)
                name = "%d_" % self.count + name
                try:
                    self.api.download(image_url,
                                      path=self.directory,
                                      name=name)
                except PixivError:
                    print(u"😢  PixivError!!! Skip this illust")
                    continue
                print(u"✅  Download illust: %s (%s)" %
                      (illust.title, image_url))
            else:
                print(u"✨  Already download: %s: %s" %
                      (illust.title, image_url))

    def get_user_ids_from_illusts(self, illusts):
        """
        Get user ids by illusts
        """
        user_ids = []
        for illust in illusts:
            user_ids.append(illust.user.id)
        return user_ids

    def get_top_ranking_illusts(self,
                                count=DEFAULT_DOWNLOAD_TOP_RANKING_COUNT,
                                ranking_type=RankingType.DAY,
                                date=datetime.today().strftime("%Y-%m-%d"),
                                download=False):
        """
        Get top ranking illusts
        :count: the number of illusts that we want to download
        :ranking_type: ranking type
        :date: date
        :download: download flag
        """
        json_result = self.api.illust_ranking(ranking_type, date=date)
        illusts = self.get_illusts_from_all_pages(json_result,
                                                  json_result.illusts, count,
                                                  download)
        return illusts[:count]

    def get_recommended_illusts(self,
                                count=DEFAULT_DOWNLOAD_RECOMMENDED_COUNT,
                                content_type=ContentType.ILLUST,
                                download=False):
        """
        Get recommended illusts
        :count: the number of illusts that we want to download
        :content_type: content type
        :download: download flag
        """
        json_result = self.api.illust_recommended(content_type)
        illusts = self.get_illusts_from_all_pages(json_result,
                                                  json_result.illusts, count,
                                                  download)
        return illusts[:count]

    def get_illusts_by_user_ids(self,
                                user_ids,
                                count=DEFAULT_DOWNLOAD_EACH_USER_COUNT,
                                content_type=ContentType.ILLUST,
                                download=False):
        """
        Get illusts by user id
        """
        ret = {}
        for user_id in user_ids:
            json_result = self.api.user_illusts(user_id=user_id,
                                                type=content_type)
            illusts = self.get_illusts_from_all_pages(json_result,
                                                      json_result.illusts,
                                                      count, download)
            ret[user_id] = illusts[:count]
        return ret

    def get_illusts_from_all_pages(self,
                                   json_result,
                                   illusts,
                                   count,
                                   download=False):
        """
        Get illusts from all pages
        """
        while len(json_result) != 0 and len(illusts) < count:
            next_qs = self.api.parse_qs(json_result.next_url)
            if next_qs is None:
                break
            try:
                json_result = self.api.illust_ranking(**next_qs)
            except TypeError:
                break
            illusts += json_result.illusts

        if download:
            count = min(count, len(illusts))
            self.download_illusts(illusts=illusts[:count])

        return illusts
コード例 #21
0
ファイル: pixiv.py プロジェクト: shamsow/reddit-saucenao
class Pixiv:
    def __init__(self):
        self.enabled = config.getboolean('Pixiv', 'enabled', fallback=False)
        self.username = config.get('Pixiv', 'username', fallback=None)
        self._password = config.get('Pixiv', 'password', fallback=None)
        self._log = logging.getLogger(__name__)

        self._pixiv = AppPixivAPI()
        self._pixiv.set_accept_language(
            config.get('Pixiv', 'language', fallback='en-US'))

        self._re_twitter = re.compile(
            r'^https?://(www.)?twitter.com/(?P<username>.+)$')

        if self.enabled:
            self._login()

    def _login(self) -> None:
        """
        Authenticate to Pixiv
        Returns:
            None
        """
        self._log.info(
            f'[PIXIV] Authenticating to Pixiv with the username {self.username}'
        )
        try:
            self._pixiv.login(self.username, self._password)
        except Exception as error:
            self._log.exception("[PIXIV] Failed to authenticate to Pixiv",
                                exc_info=error)

    def get_illust(self, illust_id: int) -> typing.Optional[dict]:
        """
        Look up the provided illustration ID from SauceNao
        Args:
            illust_id (int):

        Returns:
            typing.Optional[dict]
        """
        if not self.enabled:
            return None

        illust = self._pixiv.illust_detail(illust_id)
        if 'error' in illust and 'invalid_grant' in illust['error']['message']:
            self._log.info(
                f'Re-Authenticating to Pixiv with the username {self.username}'
            )
            self._login()
            illust = self._pixiv.illust_detail(illust_id)

        return illust['illust'] if illust and 'illust' in illust else None

    def get_author(self, author_id: int) -> typing.Optional[dict]:
        """
        Get the author for the specified illustration
        Args:
            author_id (int):

        Returns:
            typing.Optional[dict]
        """
        if not self.enabled:
            return None

        user = self._pixiv.user_detail(author_id)
        if 'error' in user and 'invalid_grant' in user['error']['message']:
            self._log.info(
                f'Re-Authenticating to Pixiv with the username {self.username}'
            )
            self._login()
            user = self._pixiv.user_detail(author_id)

        return user

    def get_author_twitter(self, author_id: int) -> typing.Optional[str]:
        """
        Get the Pixiv artists Twitter page, if available
        Args:
            author_id (int):

        Returns:
            typing.Optional[str]
        """
        if not self.enabled:
            return None

        user = self.get_author(author_id)

        twitter_url = user['profile']['twitter_url'] if (
            user and 'profile' in user) else None
        if twitter_url:
            match = self._re_twitter.match(twitter_url)
            if match and match.group('username'):
                return f"@{match.group('username')}"
コード例 #22
0
ファイル: Perf.py プロジェクト: synodriver/pixivpy-async
import time
import random

from pixivpy_async import PixivClient
from pixivpy_async import AppPixivAPI
from pixivpy_async import PixivAPI
from pixivpy3 import AppPixivAPI as Sync_aapi
from pixivpy3 import PixivAPI as Sync_papi

sys.dont_write_bytecode = True

_USERNAME = "******"
_PASSWORD = "******"

saapi = Sync_aapi()
saapi.login(_USERNAME, _PASSWORD)
spapi = Sync_papi()
spapi.login(_USERNAME, _PASSWORD)


def gen_date():
    """201x-0x-xx"""
    year = random.randint(3, 9)
    month = random.randint(1, 9)
    day = random.randint(10, 29)
    return '201%s-0%s-%s' % (year, month, day)


def test_sync_illust(num):
    e = time.time()
    for i in range(num):
コード例 #23
0
ファイル: login_env.py プロジェクト: Mikubill/pixivpy-async
def test0():
    api = AAPI()
    # api.login(_USERNAME, _PASSWORD)
    api.login(refresh_token=_TOKEN)
    print("test0 - finished")
コード例 #24
0
import json
import os
import re

from pixivpy3 import AppPixivAPI

with open('credentials.json') as cf:
    credentials = json.load(cf)

with open('urls.txt') as uf:
    urls = [u.split('/')[-1] for u in uf if 'pixiv.net' in u]

ids = [re.findall(r'\d+', id)[0] for id in urls]

api = AppPixivAPI()
api.login(credentials['email'], credentials['password'])

for id in ids:
    json_result = api.illust_detail(id)
    api.download(json_result.illust['meta_single_page']['original_image_url'])
コード例 #25
0
ファイル: signals.py プロジェクト: utopianf/maobot
def check_log(instance, **kwargs):
    log = instance
    title = 'test'
    if log.nick != 'maobot' and log.command == 'PRIVMSG' and log.nick != 'maobot_php':
        url_pat = re.compile(r"https?://[a-zA-Z0-9\-./?@&=:~_#]+")
        url_list = re.findall(url_pat, log.message)
        for url in url_list:
            r = requests.get(url)
            content_type_encoding = r.encoding if r.encoding != 'ISO-8859-1' else None
            soup = BeautifulSoup(r.content, 'html.parser', from_encoding=content_type_encoding)
            try:
                title = soup.title.string
                Log(command='NOTICE', channel=log.channel,
                    nick='maobot', message=title, is_irc=False).save()

            except (AttributeError, TypeError, HTTPError):
                pass

            # image dl
            nicoseiga_pat = re.compile(
                'http://seiga.nicovideo.jp/seiga/[a-zA-Z]+([0-9]+)')
            pixiv_pat = re.compile(
                'https://www.pixiv.net/member_illust.php/?\?([a-zA-Z0-9\-./?@&=:~_#]+)')
            twitter_pat = re.compile(
                'https://twitter.com/[a-zA-Z0-9_]+/status/\d+')
            image_format = ["jpg", "jpeg", "gif", "png"]

            if twitter_pat.match(url):
                try:
                    images = soup.find("div", {"class": "permalink-tweet-container"}).find("div", {"class": "AdaptiveMedia-container"}).findAll("div", {"class": "AdaptiveMedia-photoContainer"})
                except AttributeError:
                    images = soup.findAll("div", {"class": "media"})
                for image in images:
                    try:
                        image_url = image.find('img')['src']
                        img = image_from_response(requests.Session().get(image_url),
                                                Image(original_url=url, related_log=log, caption=title))
                        img.save()
                        Log.objects.filter(id=log.id).update(attached_image=img.thumb)
                    except:
                        pass
            elif nicoseiga_pat.match(url):
                seiga_login = '******'
                seiga_id = nicoseiga_pat.search(url).group(1)
                seiga_source = 'http://seiga.nicovideo.jp/image/source/%s' % seiga_id
                login_post = {'mail_tel': SECRET_KEYS['nicouser'],
                              'password': SECRET_KEYS['nicopass']}

                session = requests.Session()
                session.post(seiga_login, data=login_post)
                soup = BeautifulSoup(session.get(seiga_source).text, 'lxml')
                image_url = 'http://lohas.nicoseiga.jp%s' % soup.find(
                    'div', {'class': 'illust_view_big'})['data-src']
                img = image_from_response(requests.Session().get(image_url),
                                          Image(original_url=url, related_log=log))
                img.save()
                Log.objects.filter(id=log.id).update(attached_image=img.thumb)
            elif pixiv_pat.match(url):
                from pixivpy3 import AppPixivAPI
                from urllib.parse import parse_qs
                api = AppPixivAPI()
                api.login(SECRET_KEYS['pixiuser'], SECRET_KEYS['pixipass'])
                pixiv_query = pixiv_pat.search(url).group(1)
                pixiv_dict = parse_qs(pixiv_query)
                pixiv_id = pixiv_dict['illust_id']
                pixiv_illust = api.illust_detail(pixiv_id, req_auth=True).illust
                if 'meta_pages' in pixiv_illust and len(pixiv_illust.meta_pages) != 0:
                    image_urls = []
                    if 'page' in pixiv_dict:
                        image_urls.append(pixiv_illust.meta_pages[int(pixiv_dict['page'][0])].image_urls.large)
                    else:
                        for i in pixiv_illust.meta_pages:
                            image_urls.append(i.image_urls.large)
                else:
                    image_urls = [pixiv_illust.image_urls.large]
                for image_url in image_urls:
                    response = api.requests_call('GET', image_url,
                                                 headers={'Referer': 'https://app-api.pixiv.net/'},
                                                 stream=True)
                    img = image_from_response(response,
                                              Image(original_url=url, related_log=log, caption=pixiv_illust.title))
                    img.save()
                    Log.objects.filter(id=log.id).update(attached_image=img.thumb)
            elif url.split(".")[-1] in image_format:
                img = image_from_response(requests.Session().get(url),
                                          Image(original_url=url, related_log=log))
                img.save()
                Log.objects.filter(id=log.id).update(attached_image=img.thumb)