Last active
October 20, 2020 15:55
-
-
Save nkmathew/e95cbdf4ed12b885499b1acd129f2420 to your computer and use it in GitHub Desktop.
LightShot Image Extractor: scrapes the jpg screenshot link from a LightShot page
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
#!/usr/bin/env python3 | |
# coding: utf-8 | |
""" LightShot Image Extractor | |
Date: 17th December 2016 | |
Author: nkmathew <[email protected]> | |
Script for scraping the jpg link from a LightShot screenshot page to be used for | |
hotlinking | |
""" | |
import urllib.request | |
import sys | |
import argparse | |
from bs4 import BeautifulSoup | |
__version__ = '0.1.0' | |
def create_args_parser(): | |
""" Returns command line parser """ | |
parser = argparse.ArgumentParser( | |
description="Extracts the jpg link from a LightShot page(http://prnt.sc/)", | |
prog='LightShot Image Extractor') | |
parser.add_argument( | |
'-v', '--version', action='version', | |
help='Prints script version', | |
version='LightShot Image Extractor v%s' % __version__) | |
parser.add_argument(dest='page_url', help='LightShot page url', | |
type=str, default='') | |
return parser | |
def parse_options(arguments=None): | |
""" Reads command-line arguments | |
""" | |
if arguments is None: | |
arguments = sys.argv[1:] | |
if isinstance(arguments, str): | |
arguments = arguments.split() | |
if isinstance(arguments, argparse.Namespace): | |
return arguments | |
parser = create_args_parser() | |
args = parser.parse_args(arguments) | |
return args | |
def lightshot_image(url): | |
""" Extracts the jpg url from a LightShot page | |
>>> lightshot_image('http://prntscr.com/dki21q') | |
http://image.prntscr.com/image/1aaf571d0adf4aa098eb565bbb196af6.png | |
""" | |
agent = {'User-Agent': 'Mozilla/5.0 (Windows NT 6.3; Win64; x64)'} | |
request = urllib.request.Request(url, headers=agent) | |
html = urllib.request.urlopen(request).read() | |
soup = BeautifulSoup(html, 'html.parser') | |
meta_tags = soup.find_all('meta') | |
image_src = '' | |
for meta in meta_tags: | |
try: | |
if 'twitter:image:src' in meta['name']: | |
image_src = meta['content'] | |
except KeyError: | |
pass | |
return image_src | |
def main(options=None): | |
""" Entry point """ | |
opts = parse_options(options) | |
print(lightshot_image(opts.page_url)) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Would be nice if someone made a PHP version of this