/usr/lib/python2.7/dist-packages/xkcd.py is in python-xkcd 2.3.1-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 | """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
|