Last active
December 15, 2015 01:09
-
-
Save tlvince/5177634 to your computer and use it in GitHub Desktop.
Convert GitHub repository information to a directory of YAML files. See https://github.com/tlvince/lab
This file contains 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
build | |
node_modules |
This file contains 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
fs = require 'fs' | |
https = require 'https' | |
yaml = require 'js-yaml' | |
moment = require 'moment' | |
path = require 'path' | |
url = require 'url' | |
querystring = require 'querystring' | |
out = 'build' | |
username = 'tlvince' | |
api = "https://api.github.com/users/#{username}/repos" | |
# https://github.com/ajaxorg/node-github/blob/d70dfbb4227022f33cbbabb096efd84939083300/index.js#L404-L418 | |
getPageLinks = (link) -> | |
link = link.link or link.meta.link if typeof link is "object" and (link.link or link.meta.link) | |
links = {} | |
return links unless typeof link is "string" | |
link.replace /<([^>]*)>;\s*rel="([\w]*)\"/g, (m, uri, type) -> | |
links[type] = uri | |
links | |
getRepos = (uri) -> | |
uri = url.parse uri | |
query = querystring.parse uri.query | |
page = if query.page then query.page else 1 | |
options = | |
hostname: uri.hostname | |
path: uri.path | |
headers: 'User-Agent': 'github2yaml' | |
console.log "Getting repositories from page #{page}…" | |
https.get options, (res) -> | |
body = '' | |
res.on 'data', (chunk) -> | |
body += chunk | |
res.on 'end', -> | |
data = JSON.parse(body) | |
genYAML data | |
link = res.headers.link | |
next = getPageLinks(link).next if link | |
getRepos next if next | |
genYAML = (repos) -> | |
for repo in repos | |
console.log "Parsing #{repo.name}…" | |
filename = "#{path.basename repo.html_url}.yaml" | |
file = path.join out, filename | |
repo = | |
title: repo.name | |
date: moment(repo.created_at).format('YYYY-MM-DD HH:mm:ss ZZ') | |
url: repo.html_url | |
description: repo.description | |
fork: repo.fork | |
repo.tags = repo.language if repo.language | |
contents = yaml.dump repo | |
fs.writeFile file, contents, (err) -> | |
throw err if err | |
fs.mkdir out | |
getRepos api |
This file contains 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
{ | |
"name": "github-to-yaml", | |
"version": "0.1.0", | |
"description": "Convert GitHub repository information to a directory of YAML files", | |
"main": "app.coffee", | |
"repository": "", | |
"keywords": [ | |
"github", | |
"yaml", | |
"api" | |
], | |
"author": "Tom Vincent", | |
"license": "MIT", | |
"readmeFilename": "readme.mkd", | |
"dependencies": { | |
"coffee-script": "~1.6.1", | |
"js-yaml": "~2.0.3", | |
"moment": "~2.0.0" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment