Skip to content

Instantly share code, notes, and snippets.

@bulkan
Last active August 29, 2015 14:05
Show Gist options
  • Save bulkan/c90be94df70c93faa89f to your computer and use it in GitHub Desktop.
Save bulkan/c90be94df70c93faa89f to your computer and use it in GitHub Desktop.
package main
import (
"fmt"
"net/http"
//"encoding/json"
"os"
"github.com/bitly/go-simplejson"
)
type NpmPackage struct {
Name, Description, Homepage, ReadmeFilename string
DistTags map[string]string `json:"dist-tags"`
Maintainers []map[string]string
Keywords []string
License interface{}
Repository, Author, Time, Bugs, Versions map[string]string
}
func main() {
// new Date().getTime() - 900000
response, err := http.Get("http://registry.npmjs.org/-/all/since?stale=update_after&startkey=1407494718050")
if err != nil {
fmt.Printf("%s", err)
os.Exit(1)
}
defer response.Body.Close()
js, err := simplejson.NewFromReader(response.Body)
if err != nil {
panic(err)
}
p := js.Get("redisdown").Interface()
fmt.Println(p)
redisdown := p.(NpmPackage)
fmt.Printf("%s\n", redisdown.Maintainers[0]["email"])
}
package main
import (
"fmt"
"net/http"
"encoding/json"
"os"
)
type NpmPackage struct {
Updated uint64 `json:"_updated"`
Name, Description, Homepage, ReadmeFilename string
DistTags map[string]string `json:"dist-tags"`
Maintainers []map[string]string
Keywords []string
License interface{}
Repository, Author, Time, Bugs, Versions map[string]string
}
type npmpackage NpmPackage
func (p *NpmPackage) UnmarshalJSON(b []byte) (err error){
n, j := uint64(0), npmpackage{}
if err = json.Unmarshal(b, &n); err == nil {
p.Updated = n
return
}
if err = json.Unmarshal(b, &j); err == nil {
*p = NpmPackage(j)
return
}
return
}
func main() {
// new Date().getTime() - 900000
response, err := http.Get("http://registry.npmjs.org/-/all/since?stale=update_after&startkey=1407494718050")
if err != nil {
fmt.Printf("%s", err)
os.Exit(1)
}
var packages map[string]NpmPackage
defer response.Body.Close()
if err := json.NewDecoder(response.Body).Decode(&packages); err != nil {
panic(err)
}
fmt.Printf("%s\n", packages["redisdown"].Maintainers[0]["email"])
}
@bulkan
Copy link
Author

bulkan commented Aug 11, 2014

map[versions:map[0.1.2:latest] name:redisdown dist-tags:map[latest:0.1.2] author:map[name:Hugues Malphettes] license:MIT readmeFilename:README.md time:map[modified:2014-08-08T10:47:53.604Z] description:redis adapter for levelup maintainers:[map[name:hmalphettes email:[email protected]]] keywords:[leveldb leveldown levelup redis] repository:map[url:https://github.com//hmalphettes/redisdown.git type:git]]
panic: interface conversion: interface is map[string]interface {}, not main.NpmPackage

goroutine 16 [running]:
runtime.panic(0x26e080, 0xc2087af440)
        /usr/local/go/src/pkg/runtime/panic.c:279 +0xf5
main.main()
        /Users/bulkan/src/javascript/npmtracker.com/main.go:39 +0x2dc

goroutine 19 [finalizer wait]:
runtime.park(0x14a50, 0x42ceb0, 0x42b9a9)
        /usr/local/go/src/pkg/runtime/proc.c:1369 +0x89
runtime.parkunlock(0x42ceb0, 0x42b9a9)
        /usr/local/go/src/pkg/runtime/proc.c:1385 +0x3b
runfinq()
        /usr/local/go/src/pkg/runtime/mgc0.c:2644 +0xcf
runtime.goexit()
        /usr/local/go/src/pkg/runtime/proc.c:1445

goroutine 23 [IO wait]:
net.runtime_pollWait(0x611638, 0x72, 0x0)
        /private/var/folders/00/0sdwh000h01000cxqpysvccm0035qk/T/makerelease530016500/go/src/pkg/runtime/netpoll.goc:146 +0x66
net.(*pollDesc).Wait(0xc208028060, 0x72, 0x0, 0x0)
        /usr/local/go/src/pkg/net/fd_poll_runtime.go:84 +0x46
net.(*pollDesc).WaitRead(0xc208028060, 0x0, 0x0)
        /usr/local/go/src/pkg/net/fd_poll_runtime.go:89 +0x42
net.(*netFD).Read(0xc208028000, 0xc20802d000, 0x1000, 0x1000, 0x0, 0x610418, 0x23)
        /usr/local/go/src/pkg/net/fd_unix.go:232 +0x34c
net.(*conn).Read(0xc208038048, 0xc20802d000, 0x1000, 0x1000, 0x0, 0x0, 0x0)
        /usr/local/go/src/pkg/net/net.go:122 +0xe7
net/http.noteEOFReader.Read(0x611710, 0xc208038048, 0xc20803c108, 0xc20802d000, 0x1000, 0x1000, 0x43ec60, 0x0, 0x0)
        /usr/local/go/src/pkg/net/http/transport.go:1203 +0x72
net/http.(*noteEOFReader).Read(0xc20803e240, 0xc20802d000, 0x1000, 0x1000, 0xc20804c170, 0x0, 0x0)
        <autogenerated>:124 +0xca
bufio.(*Reader).fill(0xc208004360)
        /usr/local/go/src/pkg/bufio/bufio.go:97 +0x1b3
bufio.(*Reader).Peek(0xc208004360, 0x1, 0x0, 0x0, 0x0, 0x0, 0x0)
        /usr/local/go/src/pkg/bufio/bufio.go:132 +0x101
net/http.(*persistConn).readLoop(0xc20803c0b0)
        /usr/local/go/src/pkg/net/http/transport.go:782 +0x95
created by net/http.(*Transport).dialConn
        /usr/local/go/src/pkg/net/http/transport.go:600 +0x93f

goroutine 17 [syscall]:
runtime.goexit()
        /usr/local/go/src/pkg/runtime/proc.c:1445

goroutine 24 [select]:
net/http.(*persistConn).writeLoop(0xc20803c0b0)
        /usr/local/go/src/pkg/net/http/transport.go:885 +0x38f
created by net/http.(*Transport).dialConn
        /usr/local/go/src/pkg/net/http/transport.go:601 +0x957
exit status 2

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment