From 69c95684947a59e08c4865e71c1455c6d9a6f780 Mon Sep 17 00:00:00 2001 From: bruno_messias Date: Tue, 22 Aug 2017 12:37:09 -0300 Subject: [PATCH] Checks state of scihub --- requeriments.txt | 1 + scihub2pdf/__init__.py | 2 ++ scihub2pdf/bin/scihub2pdf | 20 +++++++++++++++++--- scihub2pdf/scihub.py | 34 ++++++++++++++++++++++++++-------- setup.py | 5 +++-- 5 files changed, 49 insertions(+), 13 deletions(-) diff --git a/requeriments.txt b/requeriments.txt index 0b36659..9bd1a53 100755 --- a/requeriments.txt +++ b/requeriments.txt @@ -3,3 +3,4 @@ future requests lxml title2bib +Pillow diff --git a/scihub2pdf/__init__.py b/scihub2pdf/__init__.py index e69de29..36e61c6 100755 --- a/scihub2pdf/__init__.py +++ b/scihub2pdf/__init__.py @@ -0,0 +1,2 @@ + +__version__ = "0.1.0" diff --git a/scihub2pdf/bin/scihub2pdf b/scihub2pdf/bin/scihub2pdf index 250c597..5aefa44 100755 --- a/scihub2pdf/bin/scihub2pdf +++ b/scihub2pdf/bin/scihub2pdf @@ -51,24 +51,38 @@ def main(): action="store_true", help="download from title" ) + parser.add_argument( + "--uselibgen", + dest="uselibgen", + action="store_true", + help="Use libgen.io instead sci-hub. Sci-hub has annoying captcha but is stable. Libgen has no captcha but is unstable." + ) parser.add_argument( "--location", "-l", help="folder, ex: -l 'folder/'" ) parser.set_defaults(title=False) + parser.set_defaults(uselibgen=False) parser.set_defaults(location="") args = parser.parse_known_args() title_search = args[0].title + use_libgen = args[0].uselibgen inline_search = len(args[1]) > 0 location = args[0].location + + if use_libgen: + print("\n\t Using Libgen.\n") + else: + print("\n\t Using Sci-Hub.\n") + if inline_search: value = " ".join(args[1]) if title_search: - download_from_title(value, location) + download_from_title(value, location, use_libgen) else: - download_from_doi(value, location) + download_from_doi(value, location, use_libgen) else: bibtex = bibtexparser.loads(args[0].input.read()) bibs = bibtex.entries @@ -76,7 +90,7 @@ def main(): print("Input File is empty or corrupted.") sys.exit(1) - download_pdf_from_bibs(bibs, location) + download_pdf_from_bibs(bibs, location, use_libgen) if __name__ == "__main__": diff --git a/scihub2pdf/scihub.py b/scihub2pdf/scihub.py index b16d554..057b485 100755 --- a/scihub2pdf/scihub.py +++ b/scihub2pdf/scihub.py @@ -5,22 +5,40 @@ import bibtexparser from builtins import input from PIL import Image +from . import __version__ try: from StringIO import StringIO except ImportError: from io import StringIO - headers = { # "Connection": "close", "User-Agent": "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.36" } -url_captcha_scihub = "http://moscow.sci-hub.cc" -url_libgen = "http://libgen.io/scimag/ads.php" -url_scihub = "http://sci-hub.cc/" -xpath_libgen_a = "/html/body/table/tr/td[3]/a" -xpath_scihub_captcha = "//*[@id='captcha']" -xpath_scihub_iframe = "//iframe/@src" -xpath_scihub_pdf = "//*[@id='pdf']" +print("\n\t Checking state of scihub...") +url_state ="https://raw.githubusercontent.com/bibcure/scihub_state/master/state.txt" +try: + r = requests.get(url_state) + state_scihub = [i.split(">>")[1] for i in r.iter_lines()] + url_captcha_scihub = state_scihub[0] + url_libgen = state_scihub[1] + url_scihub = state_scihub[2] + xpath_libgen_a = state_scihub[3] + xpath_scihub_captcha = state_scihub[4] + xpath_scihub_iframe = state_scihub[5] + xpath_scihub_pdf = state_scihub[6] + has_update = state_scihub[7] != __version__ + if has_update: + print("\n\t\tWill be better if you upgrade scihub2pdf.") + print("\t\tFor that, just do:\n") + print("\t\t\t sudo pip install scihub2pdf --upgrade\n") +except: + url_captcha_scihub = "http://moscow.sci-hub.cc" + url_libgen = "http://libgen.io/scimag/ads.php" + url_scihub = "http://sci-hub.cc/" + xpath_libgen_a = "/html/body/table/tr/td[3]/a" + xpath_scihub_captcha = "//*[@id='captcha']" + xpath_scihub_iframe = "//iframe/@src" + xpath_scihub_pdf = "//*[@id='pdf']" def norm_url(url): diff --git a/setup.py b/setup.py index 9b8ba0e..6fd27b6 100755 --- a/setup.py +++ b/setup.py @@ -6,7 +6,7 @@ setup( name="scihub2pdf", - version="0.0.3", + version="0.1.0", packages = find_packages(exclude=["build",]), # packages = find_packages(), scripts=["scihub2pdf/bin/scihub2pdf"], @@ -14,6 +14,7 @@ install_requires = ["bibtexparser", "title2bib", "future", + "Pillow", "requests", "lxml"], include_package_data=True, @@ -21,7 +22,7 @@ description="Downloads pdfs via a DOI number, article title or a bibtex file, sci-hub", author="Bruno Messias", author_email="messias.physics@gmail.com", - download_url="https://github.com/bibcure/scihub2pdf/archive/0.0.3.tar.gz", + download_url="https://github.com/bibcure/scihub2pdf/archive/0.1.0.tar.gz", keywords=["bibtex", "sci-hub", "libgen", "doi", "science","scientific-journals"], classifiers=[