def getPost(self, post_id): url = 'https://'+ self.url + '/post/show/' + str(post_id) response = requests.get(url) try: response.raise_for_status() except HTTPError as err: raise NazurinError(err) response = response.text soup = BeautifulSoup(response, 'html.parser') tag = soup.find(id="post-view").find(recursive=False) if tag.name == 'script': content = str.strip(tag.string) elif tag.name == 'div' and ('status-notice' in tag['class']): raise NazurinError(tag.get_text(strip=True)) else: logger.error(tag) raise NazurinError('Unknown error') info = content[19:-2] try: info = json.loads(info) post = info['posts'][0] tags = info['tags'] except json.decoder.JSONDecodeError as err: logger.error(err) return post, tags
def getArtwork(self, artwork_id): """Fetch an artwork.""" response = self.call(Pixiv.api.illust_detail, artwork_id) if 'illust' in response.keys(): illust = response.illust else: error = response.error msg = error.user_message or error.message raise NazurinError(msg) if illust.restrict != 0: raise NazurinError("Artwork is private") return illust
def getPost(self, post_id=None, md5=None): """Fetch an post.""" try: if post_id: post = self.api.post_show(post_id) else: post = self.api.post_list(md5=md5) except PybooruHTTPError as err: if 'Not Found' in err._msg: raise NazurinError('Post not found') if 'file_url' not in post.keys(): raise NazurinError('You may need a gold account to view this post\nSource: ' + post['source']) return post
def view_illust(self, artwork_id): illust = self.getArtwork(artwork_id) if illust.type == 'ugoira': raise NazurinError('Ugoira view is not supported.') details = self.buildCaption(illust) imgs = self.getImages(illust) return imgs, details
def insert(self, key, data): if key: data['_id'] = key try: return self._collection.insert_one(data).acknowledged except DuplicateKeyError: raise NazurinError('Already exists in database.')
def requireAuth(self): if Pixiv.api.access_token and time.time() - Pixiv.updated_time < 3600: # Logged in, access_token not expired return if Pixiv.api.refresh_token: # Logged in, access_token expired self.refreshToken() return # Haven't logged in tokens = Pixiv.document.get() if tokens: Pixiv.api.access_token = tokens['access_token'] Pixiv.api.refresh_token = tokens['refresh_token'] Pixiv.updated_time = tokens['updated_time'] if time.time() - Pixiv.updated_time >= 3600: # Token expired self.refreshToken() else: logger.info('Pixiv logged in through cached tokens') else: # Initialize database if not REFRESH_TOKEN: raise NazurinError('Pixiv refresh token is required') Pixiv.api.refresh_token = REFRESH_TOKEN Pixiv.api.auth() Pixiv.updated_time = time.time() Pixiv.collection.insert( DOCUMENT, { 'access_token': Pixiv.api.access_token, 'refresh_token': Pixiv.api.refresh_token, 'updated_time': Pixiv.updated_time }) logger.info('Pixiv tokens cached')
def getPost(self, post_id): """Fetch an post.""" api = 'https://gelbooru.com/index.php?page=dapi&s=post&q=index&json=1&id=' + str(post_id) response = requests.get(api) if not response.text: raise NazurinError('Post not found') post = response.json()[0] return post
def bookmark(self, artwork_id): response = self.call(Pixiv.api.illust_bookmark_add, artwork_id) if 'error' in response.keys(): logger.error(response) raise NazurinError(response['error']['user_message']) else: logger.info('Bookmarked artwork ' + str(artwork_id)) return True
def getTweet(self, status_id): """Get a tweet from API.""" # Old: 'https://syndication.twitter.com/tweets.json?ids='+ status_id +'&lang=en' api = 'https://cdn.syndication.twimg.com/tweet?id='+ str(status_id) +'&lang=en' response = requests.get(api) if response.status_code == 404: raise NazurinError('Tweet not found or unavailable.') tweet = response.json() return tweet
def getImages(self, tweet): """Get all images in a tweet.""" if 'photos' not in tweet.keys(): raise NazurinError('No photo found.') photos = tweet['photos'] imgs = list() for photo in photos: filename, url = self.parseUrl(photo['url']) imgs.append(Image('twitter - ' + tweet['id_str'] + ' - ' + filename, url)) return imgs
def auth(self): if GoogleDrive.drive: return gauth = GoogleAuth() scope = ['https://www.googleapis.com/auth/drive'] gauth.auth_method = 'service' if GD_CREDENTIALS: if GD_CREDENTIALS.startswith('{'): credentials = json.loads(GD_CREDENTIALS) gauth.credentials = ServiceAccountCredentials.from_json_keyfile_dict( credentials, scope) else: gauth.credentials = ServiceAccountCredentials.from_json_keyfile_name( GD_CREDENTIALS, scope) else: raise NazurinError( 'Credentials not found for Google Drive storage.') GoogleDrive.drive = GDrive(gauth)
def getPost(self, post_id): response = requests.get('https://www.zerochan.net/' + str(post_id)) try: response.raise_for_status() except HTTPError as err: raise NazurinError(err) # Override post_id if there's a redirection if response.history: post_id = response.url.split('/')[3] response = response.text soup = BeautifulSoup(response, 'html.parser') info = soup.find("script", {"type":"application/ld+json"}).contents info = json.loads(''.join(info).replace('\\\'', '\'')) name = info['name'].split(' #')[0] created_at = int(datetime.strptime(info['datePublished'], '%c').replace(tzinfo=timezone.utc).timestamp()) size = int(info['contentSize'][:-2]) * 1024 tags = dict() for tag in soup.find('ul', id='tags').find_all('li'): tag_name, tag_type = tag.contents tag_name = unquote(tag_name['href'][1:]).replace('+', '_') tag_type = tag_type[1:] tags[tag_name] = tag_type post = { 'id': int(post_id), 'name': name, 'created_at': created_at, 'image_width': info['width'][:-3], 'image_height': info['height'][:-3], 'tags': tags, 'file_ext': info['encodingFormat'], 'file_size': size, 'file_url': info['contentUrl'], 'preview_file_url': info['thumbnail'], 'uploader': info['author'] } return post