Last active
September 2, 2022 12:10
-
-
Save shirooo39/6f8fe563fa68e4c7e89691df531d9dda to your computer and use it in GitHub Desktop.
need help converting this python script to javascript
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import pathlib | |
import requests | |
import sys | |
def main(): | |
source = 'https://s4.anilist.co/file/anilistcdn/media/anime/banner/109287-Du3UxrOnIS4w.jpg' | |
fallback_source = 'D:/test.png' | |
try: | |
# check if the given source is a url and image can be pulled from url, but don't actually download the file | |
requests.get(source) | |
except requests.exceptions.RequestException: | |
# if nothing can't be pulled from the given url, check if the given source is a local file path | |
if not (pathlib.Path(source).exists and pathlib.Path(source).is_file()): | |
# if nothing can't be find locally, use a fallback source, which is a local file that must present | |
source = fallback_source | |
# if fallback source can't be find, print out an html <img> element with empty source | |
if not (pathlib.Path(source).exists and pathlib.Path(source).is_file()): | |
sys.exit(f'<img src="" alt="alternate">') | |
sys.exit(f'<img src="{source}" alt="alternate">') | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
converted using an online converter but I don't think this is correct...