2017-10-28 01:31:57 +00:00
|
|
|
import asyncio
|
2017-10-15 19:32:35 +00:00
|
|
|
import re
|
|
|
|
|
2017-09-24 15:05:28 +00:00
|
|
|
from bs4 import BeautifulSoup
|
|
|
|
from lxml import html
|
2017-10-13 02:30:40 +00:00
|
|
|
|
2017-09-24 15:05:28 +00:00
|
|
|
from misc import exceptions as exc
|
2017-10-13 02:30:40 +00:00
|
|
|
from utils import utils as u
|
|
|
|
|
2017-09-24 15:05:28 +00:00
|
|
|
|
2017-10-16 06:06:33 +00:00
|
|
|
async def get_post(url):
|
2017-10-28 01:31:57 +00:00
|
|
|
await asyncio.sleep(u.RATE_LIMIT)
|
|
|
|
|
2017-10-20 20:23:27 +00:00
|
|
|
content = await u.fetch('http://iqdb.harry.lu', params={'url': url})
|
|
|
|
|
2017-10-15 01:54:29 +00:00
|
|
|
try:
|
2017-10-20 20:23:27 +00:00
|
|
|
value = BeautifulSoup(content, 'html.parser').find_all('a')[1].get('href')
|
|
|
|
if value != '#':
|
2017-11-20 04:25:30 +00:00
|
|
|
ident = re.search('show/([0-9]+)', value).group(1)
|
|
|
|
post = await u.fetch('http://e621.net/post/show.json', params={'id': ident}, json=True)
|
|
|
|
return post
|
2017-10-20 20:23:27 +00:00
|
|
|
else:
|
|
|
|
raise IndexError
|
2017-10-21 20:39:11 +00:00
|
|
|
|
2017-10-20 20:23:27 +00:00
|
|
|
except IndexError:
|
|
|
|
try:
|
|
|
|
raise exc.MatchError(re.search('\/([^\/]+)$', url).group(1))
|
2017-10-13 02:30:40 +00:00
|
|
|
|
2017-10-20 20:23:27 +00:00
|
|
|
except AttributeError:
|
|
|
|
raise exc.MissingArgument
|
2017-10-16 06:06:33 +00:00
|
|
|
|
|
|
|
|
|
|
|
async def get_image(url):
|
2017-10-20 20:23:27 +00:00
|
|
|
content = await u.fetch(url)
|
2017-10-16 06:06:33 +00:00
|
|
|
|
2017-10-20 20:23:27 +00:00
|
|
|
value = html.fromstring(content).xpath(
|
|
|
|
'string(/html/body/div[@id="content"]/div[@id="post-view"]/div[@class="content"]/div[2]/img/@src)')
|
2017-10-16 06:06:33 +00:00
|
|
|
|
2017-10-20 20:23:27 +00:00
|
|
|
return value
|