diff --git a/.gitignore b/.gitignore index 9556ed4..f2e1f52 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,4 @@ venv/* imgin/__pycache__/* testdata/* .vscode/* +*.egg-info \ No newline at end of file diff --git a/imgin/get.py b/imgin/get.py index d90a908..50abaf9 100644 --- a/imgin/get.py +++ b/imgin/get.py @@ -1,10 +1,11 @@ import sys -from os import remove +from os import remove, write from threading import Thread +#from gevent import sleep +from time import sleep import requests import bs4 -from gevent import sleep from .config import IMAGE_CACHE, SINGLE_IMAGE_DELETE_AFTER_SECS @@ -51,6 +52,8 @@ def get(url: str, write_dir: str, delete=True): for count, el in enumerate(soup.select('.post-image meta[itemprop="contentUrl"]'), start=1): try: found_url = "https:" + el['content'] + if '?1' in found_url: + continue except KeyError: error("Could not obtain url for detected image") continue @@ -66,7 +69,7 @@ def get(url: str, write_dir: str, delete=True): if delete: Thread(target=delete_file, args=[f"{write_dir}{found_url[-11:]}"]).start() # Write the found urls to a file with the name of the album so the viewer endpoint can get them - found_list_file = IMAGE_CACHE + orig_url.replace('/', '_') + found_list_file = write_dir + orig_url.replace('/', '_') with open(found_list_file, 'w') as f: f.write(','.join(found_urls)) Thread(target=delete_file, args=[found_list_file]).start() diff --git a/run_tests.sh b/run_tests.sh index ba03150..d63fc69 100755 --- a/run_tests.sh +++ b/run_tests.sh @@ -1,13 +1,14 @@ #!/bin/bash ran=0 -SECONDS=0 ; +SECONDS=0 ; close () { exit 10; } for f in tests/*.py; do - python3 "$f" || close # if needed + python3 "$f" || close & # if needed let "ran++" done echo "ran $ran test files successfully in $SECONDS seconds" rm -f *.dat +rm -rf /tmp/imgin* \ No newline at end of file diff --git a/tests/test.py b/tests/test.py deleted file mode 100644 index 0d6f79c..0000000 --- a/tests/test.py +++ /dev/null @@ -1,7 +0,0 @@ -import unittest -class TestBasic(unittest.TestCase): - - def test_basic(self): - self.assertTrue(True) - -unittest.main() \ No newline at end of file diff --git a/tests/test_album.py b/tests/test_album.py new file mode 100644 index 0000000..172c2b1 --- /dev/null +++ b/tests/test_album.py @@ -0,0 +1,26 @@ +import unittest +import os +from glob import glob + +from imgin import get + +CACHE_DIR = '/tmp/imgin-imgur-images-album/' + +class TestAlbum(unittest.TestCase): + + def test_album_a(self): + code = 'ethCwFv' + get(f"https://imgur.com/a/{code}", CACHE_DIR) + files = glob(CACHE_DIR + '*') + for i in files: + if i.endswith('m_a_ethCwFv'): + continue + print(f'got tests/test_images/album/{i[-11:]} checking if it is an image we should have') + self.assertTrue(os.path.exists(f'tests/test_images/album/{i[-11:]}')) + +try: + os.mkdir(CACHE_DIR) +except FileExistsError: + pass + +unittest.main() \ No newline at end of file diff --git a/tests/test_single_image.py b/tests/test_single_image.py new file mode 100644 index 0000000..0fdb50f --- /dev/null +++ b/tests/test_single_image.py @@ -0,0 +1,22 @@ +import unittest +import os + +from imgin import get + +CACHE_DIR = '/tmp/imgin-imgur-images-single/' + +class TestSingleImage(unittest.TestCase): + + def test_single_image(self): + img = "7TiLluI.jpg" + get(f"https://imgur.com/{img}", CACHE_DIR) + with open(f"tests/test_images/{img}", "rb") as expected: + with open(CACHE_DIR + img, "rb") as actual: + self.assertEqual(actual.read(), expected.read()) + +try: + os.mkdir(CACHE_DIR) +except FileExistsError: + pass + +unittest.main() \ No newline at end of file