xkcd-2.3.1/0000775000076400007640000000000012374002266012036 5ustar bjrbjr00000000000000xkcd-2.3.1/LICENSE0000664000076400007640000000206512325224672013051 0ustar bjrbjr00000000000000The MIT License (MIT) Copyright (c) 2012 Ben Rosser Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. xkcd-2.3.1/xkcd.egg-info/0000775000076400007640000000000012374002262014455 5ustar bjrbjr00000000000000xkcd-2.3.1/xkcd.egg-info/top_level.txt0000664000076400007640000000000512374002266017206 0ustar bjrbjr00000000000000xkcd xkcd-2.3.1/xkcd.egg-info/SOURCES.txt0000664000076400007640000000025012374002266016342 0ustar bjrbjr00000000000000LICENSE MANIFEST.in README.md setup.cfg setup.py xkcd.py xkcd.egg-info/PKG-INFO xkcd.egg-info/SOURCES.txt xkcd.egg-info/dependency_links.txt xkcd.egg-info/top_level.txtxkcd-2.3.1/xkcd.egg-info/dependency_links.txt0000664000076400007640000000000112374002266020527 0ustar bjrbjr00000000000000 xkcd-2.3.1/xkcd.egg-info/PKG-INFO0000664000076400007640000000473112374002266015563 0ustar bjrbjr00000000000000Metadata-Version: 1.1 Name: xkcd Version: 2.3.1 Summary: Library to access xkcd.com Home-page: https://github.com/TC01/xkcd Author: Ben Rosser Author-email: rosser.bjr@gmail.com License: MIT Description: xkcd v2.2 - A Python interface to xkcd.com ------------------------------------------ By Ben Rosser Released under MIT License (see LICENSE.txt) This is a Python library for accessing and retrieving links to comics from the xkcd webcomic by Randall Munroe. It is NOT endorsed or made by him, it's an entirely independent project. It makes use of the JSON interface to Randall's site to retrieve comic data. One can create comic objects manually using Comic(number), or can use the helper functions provided- getLatestComic(), getRandomComic(), and getComic()- to do this. Once you have a Comic object, you can access data from it using various provided methods. Documentation is available [here](https://pythonhosted.org/xkcd/). Note that PyPI currently doesn't support Markdown formatting for long_description, hence the ugly rendering here. ## Changelog: ### Version 2.3: * Fixed ASCII bug in Python 2.x * Created Sphinx documentation and uploaded it to pythonhosted.org ### Version 2.2: * Fixed very silly bug with xkcd.getComic() * Added a getExplanation() which returns an explainxkcd link for a Comic(). * Added support for Python 3! ### Version 2.1: * Fixed bugs with Comic.download() function * Added optional parameter to Comic.download() to change name of output file * Added more information to long_description text ## Credits: * Ben Rosser : Developer Keywords: xkcd webcomic Platform: UNKNOWN Classifier: Development Status :: 5 - Production/Stable Classifier: Intended Audience :: Developers Classifier: Intended Audience :: End Users/Desktop Classifier: Topic :: Software Development :: Libraries Classifier: Topic :: Internet :: WWW/HTTP Classifier: Topic :: Internet :: WWW/HTTP :: Dynamic Content Classifier: License :: OSI Approved :: MIT License Classifier: Programming Language :: Python :: 2 Classifier: Programming Language :: Python :: 2.7 Classifier: Programming Language :: Python :: 3 Classifier: Programming Language :: Python :: 3.3 xkcd-2.3.1/README.md0000664000076400007640000000256612374002230013315 0ustar bjrbjr00000000000000xkcd v2.2 - A Python interface to xkcd.com ------------------------------------------ By Ben Rosser Released under MIT License (see LICENSE.txt) This is a Python library for accessing and retrieving links to comics from the xkcd webcomic by Randall Munroe. It is NOT endorsed or made by him, it's an entirely independent project. It makes use of the JSON interface to Randall's site to retrieve comic data. One can create comic objects manually using Comic(number), or can use the helper functions provided- getLatestComic(), getRandomComic(), and getComic()- to do this. Once you have a Comic object, you can access data from it using various provided methods. Documentation is available [here](https://pythonhosted.org/xkcd/). Note that PyPI currently doesn't support Markdown formatting for long_description, hence the ugly rendering here. ## Changelog: ### Version 2.3: * Fixed ASCII bug in Python 2.x * Created Sphinx documentation and uploaded it to pythonhosted.org ### Version 2.2: * Fixed very silly bug with xkcd.getComic() * Added a getExplanation() which returns an explainxkcd link for a Comic(). * Added support for Python 3! ### Version 2.1: * Fixed bugs with Comic.download() function * Added optional parameter to Comic.download() to change name of output file * Added more information to long_description text ## Credits: * Ben Rosser : Developer xkcd-2.3.1/xkcd.py0000664000076400007640000001222212374001057013335 0ustar bjrbjr00000000000000"""Python library for accessing xkcd.com. This is a Python library for accessing and retrieving links to comics from the xkcd webcomic by Randall Munroe. It is NOT endorsed or made by him, it's an entirely independent project. It makes use of the JSON interface to Randall's site to retrieve comic data. One can create comic objects manually using Comic(number), or can use the helper functions provided- getLatestComic(), getRandomComic(), and getComic()- to do this. Once you have a Comic object, you can access data from it using various provided methods. This documentation is not that great, but it should be getting better Soon.""" import json import os import random import sys import webbrowser # Python 3 support! if sys.version_info[0] <= 2: import urllib2 as urllib else: # This is kind of broken but I'm not sure of a better way. import urllib.request as urllib # Define the URLs as globals. xkcdUrl = "http://www.xkcd.com/" # The URL for xkcd. imageUrl = "http://imgs.xkcd.com/comics/" # The root URL for image retrieval. explanationUrl = "http://explainxkcd.com/" # The URL of the explanation. class Comic: def __init__(self, number): global xkcdUrl, imageUrl self.number = number if number <= 0: self.link = "Invalid comic" return self.link = xkcdUrl + str(number) #Get data from the JSON interface jsonString = self.link + "/info.0.json" xkcd = urllib.urlopen(jsonString).read() xkcdData = json.loads(xkcd.decode()) self.title = xkcdData['safe_title'] self.altText = xkcdData['alt'] self.imageLink = xkcdData['img'] # This may no longer be necessary. # if sys.version_info[0] >= 3: # self.title = str(self.title, encoding='UTF-8') # self.altText = str(self.altText, encoding='UTF-8') # self.imageLink = str(self.imageLink, encoding='UTF-8') #Get the image filename offset = len(imageUrl) index = self.imageLink.find(imageUrl) self.imageName = self.imageLink[index + offset:] def __str__(self): return "Comic object for " + self.link def __repr__(self): return "Comic object for " + self.link def getTitle(self): """Returns the title of the comic""" return self.title def getAsciiTitle(self): """Returns the ASCII version of a title, with appropriate try/except.""" asciiTitle = convertToAscii(self.title) return asciiTitle def getAsciiAltText(self): """Returns the ASCII version of alt text, with appropriate try/except.""" asciiAltText = convertToAscii(self.altText) return asciiAltText def getAsciiImageLink(self): """Returns the ASCII version of image link, with appropriate try/except.""" asciiImageLink = convertToAscii(self.imageLink) return asciiImageLink def getAltText(self): """Returns the alt text of the comic""" return self.altText def getImageLink(self): """Returns a URL link to the comic's image""" return self.imageLink def getImageName(self): """Returns the name of the comic's image""" return self.imageName def getExplanation(self): """Returns an explain xkcd link for the comic.""" global explanationUrl return explanationUrl + str(self.number) def show(self): """Uses the webbrowser module to open the comic""" webbrowser.open_new_tab(self.link) def download(self, output="", outputFile=""): """Download the image of the comic, returns the name of the output file""" image = urllib.urlopen(self.imageLink).read() #Process optional input to work out where the dowload will go and what it'll be called if output != "": output = os.path.abspath(os.path.expanduser(output)) if output == "" or not os.path.exists(output): output = os.path.expanduser(os.path.join("~", "Downloads")) if outputFile == "": outputFile = "xkcd-" + str(self.number) + "-" + self.imageName output = os.path.join(output, outputFile) try: download = open(output, 'wb') except: print("Unable to make file " + output) return "" download.write(image) download.close() return output def getLatestComicNum(): """Function to return the number of the latest comic.""" xkcd = urllib.urlopen("http://xkcd.com/info.0.json").read() xkcdJSON = json.loads(xkcd.decode()) number = xkcdJSON['num'] return number def getLatestComic(): """Function to return a Comic object for the latest comic number""" number = getLatestComicNum() return Comic(number) def getRandomComic(): """Function to return a Comic object for a random comic number""" random.seed() numComics = getLatestComicNum() number = random.randint(1, numComics) return Comic(number) def getComic(number): """Function to return a Comic object for a given comic number""" numComics = getLatestComicNum() if number > numComics or number <= 0: print("Error: You have requested an invalid comic.") return Comic(-1) return Comic(number) def convertToAscii(string, error="?"): """Utility function that converts unicode 'string' to ASCII, replacing all unparseable characters with 'error'.""" running = True asciiString = string while running: try: asciiString = asciiString.encode('ascii') except UnicodeError as unicode: start = unicode.start end = unicode.end asciiString = asciiString[:start] + "?" + asciiString[end:] else: running = False return asciiString xkcd-2.3.1/setup.py0000664000076400007640000000644112374002135013550 0ustar bjrbjr00000000000000# Based from http://pythonhosted.org/setuptools/setuptools.html#automatic-script-creation from setuptools import setup, find_packages # Always prefer setuptools over distutils from codecs import open # To use a consistent encoding from os import path here = path.abspath(path.dirname(__file__)) # Get the long description from the relevant file with open(path.join(here, 'README.md'), encoding='utf-8') as f: long_description = f.read() setup( name='xkcd', # Versions should comply with PEP440. For a discussion on single-sourcing # the version across setup.py and the project code, see # http://packaging.python.org/en/latest/tutorial.html#version version='2.3.1', description="Library to access xkcd.com", long_description=long_description, # The project's main homepage. url='https://github.com/TC01/xkcd', # Author details author = "Ben Rosser", author_email='rosser.bjr@gmail.com', # Choose your license license='MIT', # See https://pypi.python.org/pypi?%3Aaction=list_classifiers classifiers=[ # How mature is this project? Common values are # 3 - Alpha # 4 - Beta # 5 - Production/Stable 'Development Status :: 5 - Production/Stable', # Indicate who your project is intended for 'Intended Audience :: Developers', 'Intended Audience :: End Users/Desktop', 'Topic :: Software Development :: Libraries', 'Topic :: Internet :: WWW/HTTP', 'Topic :: Internet :: WWW/HTTP :: Dynamic Content', # Pick your license as you wish (should match "license" above) 'License :: OSI Approved :: MIT License', # Specify the Python versions you support here. In particular, ensure # that you indicate whether you support Python 2, Python 3 or both. 'Programming Language :: Python :: 2', 'Programming Language :: Python :: 2.7', 'Programming Language :: Python :: 3', 'Programming Language :: Python :: 3.3', ], # What does your project relate to? keywords='xkcd webcomic', # You can just specify the packages manually here if your project is # simple. Or you can use find_packages(). # packages=find_packages(exclude=['contrib', 'docs', 'tests*']), py_modules=['xkcd'], # List run-time dependencies here. These will be installed by pip when your # project is installed. For an analysis of "install_requires" vs pip's # requirements files see: # https://packaging.python.org/en/latest/technical.html#install-requires-vs-requirements-files #install_requires=['peppercorn'], # If there are data files included in your packages that need to be # installed, specify them here. If using Python 2.6 or less, then these # have to be included in MANIFEST.in as well. #package_data={ # 'sample': ['package_data.dat'], #}, # Although 'package_data' is the preferred approach, in some case you may # need to place data files outside of your packages. # see http://docs.python.org/3.4/distutils/setupscript.html#installing-additional-files # In this case, 'data_file' will be installed into '/my_data' #data_files=[('my_data', ['data/data_file'])], # To provide executable scripts, use entry points in preference to the # "scripts" keyword. Entry points provide cross-platform support and allow # pip to create the appropriate form of executable for the target platform. # entry_points={ # 'console_scripts': [ # 'calcpkg=calcrepo.calcpkg:main', # ], # }, ) xkcd-2.3.1/MANIFEST.in0000664000076400007640000000004212374002145013564 0ustar bjrbjr00000000000000include README.md include LICENSE xkcd-2.3.1/setup.cfg0000664000076400007640000000026712374002266013664 0ustar bjrbjr00000000000000[build_sphinx] source-dir = docs/source build-dir = docs/build all_files = 1 [upload_sphinx] upload-dir = docs/build/html [egg_info] tag_build = tag_date = 0 tag_svn_revision = 0 xkcd-2.3.1/PKG-INFO0000664000076400007640000000473112374002266013140 0ustar bjrbjr00000000000000Metadata-Version: 1.1 Name: xkcd Version: 2.3.1 Summary: Library to access xkcd.com Home-page: https://github.com/TC01/xkcd Author: Ben Rosser Author-email: rosser.bjr@gmail.com License: MIT Description: xkcd v2.2 - A Python interface to xkcd.com ------------------------------------------ By Ben Rosser Released under MIT License (see LICENSE.txt) This is a Python library for accessing and retrieving links to comics from the xkcd webcomic by Randall Munroe. It is NOT endorsed or made by him, it's an entirely independent project. It makes use of the JSON interface to Randall's site to retrieve comic data. One can create comic objects manually using Comic(number), or can use the helper functions provided- getLatestComic(), getRandomComic(), and getComic()- to do this. Once you have a Comic object, you can access data from it using various provided methods. Documentation is available [here](https://pythonhosted.org/xkcd/). Note that PyPI currently doesn't support Markdown formatting for long_description, hence the ugly rendering here. ## Changelog: ### Version 2.3: * Fixed ASCII bug in Python 2.x * Created Sphinx documentation and uploaded it to pythonhosted.org ### Version 2.2: * Fixed very silly bug with xkcd.getComic() * Added a getExplanation() which returns an explainxkcd link for a Comic(). * Added support for Python 3! ### Version 2.1: * Fixed bugs with Comic.download() function * Added optional parameter to Comic.download() to change name of output file * Added more information to long_description text ## Credits: * Ben Rosser : Developer Keywords: xkcd webcomic Platform: UNKNOWN Classifier: Development Status :: 5 - Production/Stable Classifier: Intended Audience :: Developers Classifier: Intended Audience :: End Users/Desktop Classifier: Topic :: Software Development :: Libraries Classifier: Topic :: Internet :: WWW/HTTP Classifier: Topic :: Internet :: WWW/HTTP :: Dynamic Content Classifier: License :: OSI Approved :: MIT License Classifier: Programming Language :: Python :: 2 Classifier: Programming Language :: Python :: 2.7 Classifier: Programming Language :: Python :: 3 Classifier: Programming Language :: Python :: 3.3