Browse Source

Migrate JSON logic to api project

pull/608/head
davemachado 6 years ago
parent
commit
b0409e8ae2
4 changed files with 1 additions and 102 deletions
  1. +0
    -8
      build/build.sh
  2. +0
    -23
      build/deploy.sh
  3. +1
    -21
      build/main.sh
  4. +0
    -50
      build/md2json.py

+ 0
- 8
build/build.sh View File

@@ -1,8 +0,0 @@
#!/bin/bash

# create json directory if not already present
mkdir -p ../json
# parse API README and print (minified) JSON to stdout, redirect to /json
./md2json.py ../README.md > ../json/entries.min.json
# beautify the previously created JSON file, redirect to /json
python -m json.tool ../json/entries.min.json > ../json/entries.json

+ 0
- 23
build/deploy.sh View File

@@ -1,23 +0,0 @@
#!/bin/bash

set -o errexit -o nounset

rev=$(git rev-parse --short HEAD)

mkdir deploy
cd deploy

git init
git config --global user.name $GH_USER
git config --global user.email $GH_EMAIL

git remote add upstream "https://$GH_TOKEN@github.com/toddmotto/public-apis.git"
git fetch upstream
git reset upstream/master

mv ../../json .

git add json/
git commit -m "rebuild JSON at ${rev}" -m "[ci skip]"
git push upstream HEAD:master


+ 1
- 21
build/main.sh View File

@@ -8,21 +8,11 @@ if [[ $? != 0 ]]; then
exit 1
fi
echo "format validation passed!"
./build.sh
if [[ $? != 0 ]]; then
echo "JSON build failed!"
else
echo "JSON build success!"
fi
if [ "$TRAVIS_BRANCH" == "master" ]
then
echo "Master build - deploying JSON"
./deploy.sh
fi
if [ "$TRAVIS_PULL_REQUEST" == "false" ]; then
echo "running on $TRAVIS_BRANCH branch - skipping Pull Request logic"
exit 0
fi

echo "running on Pull Request #$TRAVIS_PULL_REQUEST"
DIFF_URL="https://patch-diff.githubusercontent.com/raw/toddmotto/public-apis/pull/$TRAVIS_PULL_REQUEST.diff"
curl $DIFF_URL > diff.txt
@@ -35,16 +25,6 @@ cat additions.txt
echo "------- END ADDITIONS ------"
LINK_FILE=additions.txt

echo "checking if /json was changed..."
if egrep "\+{3}\s.\/json\/" diff.txt > json.txt; then
echo "JSON files are auto-generated! Please do not update these files:"
cat json.txt
exit 1
else
echo "/json check passed!"
rm json.txt
fi

echo "running link validation..."
./validate_links.py $LINK_FILE
if [[ $? != 0 ]]; then


+ 0
- 50
build/md2json.py View File

@@ -1,50 +0,0 @@
#!/usr/bin/env python3

import json
import sys


def markdown_to_json(filename, anchor):
"""Convert a Markdown file into a JSON string"""
category = ""
entries = []
with open(filename) as fp:
lines = (line.rstrip() for line in fp)
lines = list(line for line in lines if line and
line.startswith(anchor) or line.startswith('| '))
for line in lines:
if line.startswith(anchor):
category = line.split(anchor)[1].strip()
continue
chunks = [x.strip() for x in line.split('|')[1:-1]]
entry = {
'API': chunks[0],
'Description': chunks[1],
'Auth': None if chunks[2].upper() == 'NO' else chunks[2].strip('`'),
'HTTPS': True if chunks[3].upper() == 'YES' else False,
'CORS': chunks[4].strip('`'),
'Link': chunks[5].replace('[Go!]', '')[1:-1],
'Category': category,
}
entries.append(entry)
final = {
'count': len(entries),
'entries': entries,
}
return json.dumps(final)


def main():
num_args = len(sys.argv)
if num_args < 2:
print("No .md file passed")
sys.exit(1)
if num_args < 3:
anchor = '###'
else:
anchor = sys.argv[2]
print(markdown_to_json(sys.argv[1], anchor))


if __name__ == "__main__":
main()

Loading…
Cancel
Save