Skip to content

Commit

Permalink
major simplification
Browse files Browse the repository at this point in the history
  • Loading branch information
joshday committed Jan 5, 2020
1 parent 5f0d6e0 commit 5fa8448
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 43 deletions.
26 changes: 4 additions & 22 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,16 @@

# XKCD

A Julia package for retrieving the image (and importantly, the hovertext) for the XKCD webcomic:

[xkcd.com](https://xkcd.com)

## Details

This package uses the function `comic(id=nothing; open=true)` to create:

```julia
struct XKCDComic
id::Int
title::String
img::String
hover::String
end
```

A Julia package for retrieving the image (and importantly, the hovertext) for the XKCD webcomic: [xkcd.com](https://xkcd.com).

## Usage

The `comic` function retrieves the comic's data and optionally opens the image in a browser.

```julia
using XKCD

XKCD.comicdata(552)
XKCD.comic(552; open=false)
# JSON3.Object{Base.CodeUnits{UInt8,JSON3.VectorString{Array{UInt8,1}}},Array{UInt64,1}} with 11 entries:
# :month => "3"
# :num => 552
Expand All @@ -38,8 +24,4 @@ XKCD.comicdata(552)
# :img => "https://imgs.xkcd.com/comics/correlation.png"
# :title => "Correlation"
# :day => "6"

c = XKCD.comic(552)
# XKCDComic (1, "Correlation", https://imgs.xkcd.com/comics/correlation.png)
# Correlation doesn't imply causation, but it does waggle its eyebrows suggestively and gesture furtively while mouthing 'look over there'.
```
23 changes: 4 additions & 19 deletions src/XKCD.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,40 +2,25 @@ module XKCD

using HTTP, JSON3

#-----------------------------------------------------------------------------# comicdata
function comicdata(i::Union{Nothing, Integer} = nothing)
part = isnothing(i) ? "" : "/$i"
JSON3.read(HTTP.get("https://xkcd.com$part/info.0.json").body)
end

#-----------------------------------------------------------------------------# XKCDComic
struct XKCDComic
id::Int
title::String
img::String
hover::String
end

Base.Dict(x::XKCDComic) = Dict(:id=>x.id, :title=>x.title, :img=>x.img, :hover=>x.hover)

function Base.show(io::IO, x::XKCDComic)
println(io, "XKCDComic ($(x.id), \"$(x.title)\", $(x.img))")
printstyled(io, " " * x.hover, color=:light_black)
end

#-----------------------------------------------------------------------------# comic
"""
comic(i=nothing; open=true)
Get comic number `i` (most recent if `nothing`) and optionally open the image in browser. Returns
an `XKCDComic` struct with fields `id`, `title`, `img`, and `hover`.
Get comic number `i` (most recent if `nothing`) and optionally open the image in browser.
"""
function comic(i::Union{Nothing, Int} = nothing; open=true)
data = comicdata(i)
img, hover, title, id = data.img, data.alt, data.safe_title, data.num
img = data.img
open && Sys.isapple() && run(`open $img`)
open && Sys.islinux() && run(`xdg-open $img`)
open && Sys.iswindows() &&run(`start $img`)
XKCDComic(id, title, img, hover)
data
end

end # module
4 changes: 2 additions & 2 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ using Test

@testset "XKCD.jl" begin
c = XKCD.comic(open=false)
@test c.id >= 2250
@test c.num >= 2250

c = XKCD.comic(999, open=false)
@test c.id == 999
@test c.num == 999
@test c.title == "Cougars"
@test c.img == "https://imgs.xkcd.com/comics/cougars.png"
end

2 comments on commit 5fa8448

@joshday
Copy link
Owner Author

@joshday joshday commented on 5fa8448 Jan 5, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request updated: JuliaRegistries/General/7520

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if Julia TagBot is installed, or can be done manually through the github interface, or via:

git tag -a v0.1.0 -m "<description of version>" 5fa844811dbac574a49fdf0f32135eb2250b89ce
git push origin v0.1.0

Please sign in to comment.