Last active
August 29, 2015 14:13
-
-
Save jefftriplett/ecc9cfca41b36284a0e8 to your computer and use it in GitHub Desktop.
Fetch a url and translate it to markdown in one command. Formally moved to: https://github.com/jefftriplett/url2markdown-cli
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
""" | |
Copyright (c) 2015, Jeff Triplett | |
All rights reserved. | |
Redistribution and use in source and binary forms, with or without | |
modification, are permitted provided that the following conditions are met: | |
* Redistributions of source code must retain the above copyright notice, this | |
list of conditions and the following disclaimer. | |
* Redistributions in binary form must reproduce the above copyright notice, | |
this list of conditions and the following disclaimer in the documentation | |
and/or other materials provided with the distribution. | |
* Neither the name of url2markdown-cli.py nor the names of its contributors | |
may be used to endorse or promote products derived from this software | |
without specific prior written permission. | |
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | |
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | |
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE | |
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR | |
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER | |
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, | |
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | |
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
------------------------------------------------------------------------ | |
Thanks to @kennethreitz for his url2markdown project which this compliments: | |
(https://github.com/kennethreitz/url2markdown) | |
To install library dependencies: | |
pip install click requests requests-cache | |
To use: | |
python url2markdown-cli.py --with-cache https://www.djangoproject.com/ | |
To use your own custom url2markdown server instance: | |
export URL2MARKDOWN_URL='http://url2markdown.herokuapp.com/?url={url}' | |
""" | |
import click | |
import os | |
import requests | |
import requests_cache | |
URL2MARKDOWN_URL = os.environ.get('URL2MARKDOWN_URL', | |
'http://url2markdown.herokuapp.com/?url={url}') | |
def url2markdown(url): | |
req = requests.get(URL2MARKDOWN_URL.format(url=url)) | |
return req.text | |
@click.command() | |
@click.argument('url') | |
@click.option('--with-cache', is_flag=True) | |
def main(url, with_cache): | |
if with_cache: | |
requests_cache.install_cache() | |
click.echo(url2markdown(url)) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment