Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added python 3 and added proxy and exhentai.org/api.php with some additions to cookies (hath_perks, sk, star) #12

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 60 additions & 20 deletions ehentai_metadata/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/env python2
#!/usr/bin/env python3
# vim:fileencoding=UTF-8:ts=4:sw=4:sta:et:sts=4:ai
from __future__ import (unicode_literals, division, absolute_import,
print_function)
Expand All @@ -13,7 +13,7 @@

import re
import json
from urllib import urlencode
from urllib.parse import urlencode

def to_metadata(log,gmetadata,ExHentai_Status): # {{{
title = gmetadata['title']
Expand Down Expand Up @@ -108,24 +108,41 @@ class Ehentai(Source):
Option('ipb_member_id','string',None,_('ipb_member_id'),
_('If Use Exhentai is True, please input your cookies.')),
Option('ipb_pass_hash','string',None,_('ipb_pass_hash'),
_('If Use Exhentai is True, please input your cookies.'))
_('If Use Exhentai is True, please input your cookies.')),
Option('igneous','string',None,_('igneous'),
_('If Use Exhentai is True, please input your cookies.')),
Option('hath_perks','string',None,_('hath_perks'),
_('If Use Exhentai is True, please input your cookies.')),
Option('sk','string',None,_('sk'),
_('If Use Exhentai is True, please input your cookies.')),
Option('star','string',None,_('star'),
_('If Use Exhentai is True, please input your cookies.')),
Option('Use_Proxy','bool',False,_('Use Proxy'),
_('If Use Proxy is True, the plugin will search metadata by proxy.')),
Option('link','string',None,_('link'), # username:[email protected]:8888
_('If Use Proxy is True, please input your proxy. example: username:[email protected]:8888')),
)

config_help_message = ('<p>'+_('To Download Metadata from exhentai.org you must sign up'
' a free account and get the cookies of .exhentai.org.'
' If you don\'t have an account, you can <a href="%s">sign up</a>.')) % 'https://forums.e-hentai.org/index.php'

def __init__(self, *args, **kwargs): # {{{
Source.__init__(self, *args, **kwargs)
self.config_exhentai()
self.config_proxy()
# }}}

def config_exhentai(self): # {{{

ExHentai_Status = self.prefs['Use_Exhentai']
ExHentai_Cookies = [{'name':'ipb_member_id', 'value':self.prefs['ipb_member_id'], 'domain':'.exhentai.org', 'path':'/'},
{'name':'ipb_pass_hash', 'value':self.prefs['ipb_pass_hash'], 'domain':'.exhentai.org', 'path':'/'}]

{'name':'ipb_pass_hash', 'value':self.prefs['ipb_pass_hash'], 'domain':'.exhentai.org', 'path':'/'},
{'name':'igneous', 'value':self.prefs['igneous'], 'domain':'.exhentai.org', 'path':'/'},
{'name':'hath_perks', 'value':self.prefs['hath_perks'], 'domain':'.exhentai.org', 'path':'/'},
{'name':'sk', 'value':self.prefs['sk'], 'domain':'.exhentai.org', 'path':'/'},
{'name':'star', 'value':self.prefs['star'], 'domain':'.exhentai.org', 'path':'/'},]

if ExHentai_Status is True:
for cookie in ExHentai_Cookies:
if cookie['value'] is None:
Expand All @@ -136,9 +153,17 @@ def config_exhentai(self): # {{{
self.ExHentai_Cookies = ExHentai_Cookies
return
# }}}


def config_proxy(self): # {{{

Proxy_Status = self.prefs['Use_Proxy']
Proxy = {'https': self.prefs['link'], 'http': self.prefs['link']}
self.Proxy_Status = Proxy_Status
self.Proxy = Proxy
# }}}

def create_query(self,log,title=None, authors=None,identifiers={},is_exhentai=False): # {{{

EHentai_SEARCH_URL = 'https://e-hentai.org/?'
ExHentai_SEARCH_URL = 'https://exhentai.org/?'

Expand All @@ -154,8 +179,7 @@ def build_term(type,parts):
if author_token:
q = q + (' ' if q != '' else '') + build_term('author', author_token)
q = q.strip()
if isinstance(q, unicode):
q = q.encode('utf-8')

if not q:
return None
q_dict = {'f_doujinshi':1, 'f_manga':1, 'f_artistcg':1, 'f_gamecg':1, 'f_western':1, 'f_non-h':1,
Expand All @@ -182,13 +206,27 @@ def get_gallery_info(self,log,raw): # {{{
# }}}

def get_all_details(self,gidlist,log,abort,result_queue,timeout): # {{{

EHentai_API_url = 'https://api.e-hentai.org/api.php'
ExHentai_API_url = 'https://exhentai.org/api.php'

is_exhentai = self.ExHentai_Status
use_proxy = self.Proxy_Status
proxy = self.Proxy
url = EHentai_API_url
br = self.browser
if is_exhentai is True:
url = ExHentai_API_url
if use_proxy is True:
def proxy_bypass(hostname):
log(hostname + ' by proxy')
return True
br.set_proxies(proxy,proxy_bypass)
data = {"method": "gdata","gidlist": gidlist,"namespace": 1}
data = json.dumps(data)
try:
raw = br.open_novisit(EHentai_API_url,data=data,timeout=timeout).read()
_raw = br.open_novisit(url,data=data,timeout=timeout)
raw = _raw.read()
except Exception as e:
log.exception('Failed to make api request.',e)
return
Expand Down Expand Up @@ -222,9 +260,7 @@ def get_book_url(self, identifiers): # {{{
url = self.EHentai_url % (gid,token)
return ('ehentai', db, url)
# }}}

def download_cover(self, log, result_queue, abort,title=None, authors=None, identifiers={}, timeout=30, get_best_cover=False): # {{{

cached_url = self.get_cached_cover_url(identifiers)
if cached_url is None:
return
Expand All @@ -239,7 +275,6 @@ def download_cover(self, log, result_queue, abort,title=None, authors=None, iden
except:
log.exception('Failed to download cover from:', cached_url)
# }}}

def get_cached_cover_url(self, identifiers): # {{{

url = None
Expand All @@ -250,21 +285,27 @@ def get_cached_cover_url(self, identifiers): # {{{
url = self.cached_identifier_to_cover_url(db)
return url
# }}}

def identify(self, log, result_queue, abort, title=None, authors=None,identifiers={}, timeout=30): # {{{

is_exhentai = self.ExHentai_Status
use_proxy = self.Proxy_Status
proxy = self.Proxy
query = self.create_query(log,title=title, authors=authors,identifiers=identifiers,is_exhentai=is_exhentai)
if not query:
log.error('Insufficient metadata to construct query')
return
br = self.browser
if use_proxy is True:
def proxy_bypass(hostname):
log(hostname + ' by proxy')
return True
br.set_proxies(proxy,proxy_bypass)
if is_exhentai is True:
for cookie in self.ExHentai_Cookies:
br.set_cookie(name=cookie['name'], value=cookie['value'], domain=cookie['domain'], path=cookie['path'])
try:
_raw = br.open_novisit(query,timeout=timeout)
raw = _raw.read()
raw = str(_raw.read())
except Exception as e:
log.exception('Failed to make identify query: %r'%query)
return as_unicode(e)
Expand All @@ -289,7 +330,6 @@ def identify(self, log, result_queue, abort, title=None, authors=None,identifier
# To run these test use: calibre-customize -b ehentai_metadata && calibre-debug -e ehentai_metadata/__init__.py
from calibre.ebooks.metadata.sources.test import (test_identify_plugin,
title_test, authors_test)

test_identify_plugin(Ehentai.name,
[
(
Expand Down