Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added script and few other modification #39

Merged
merged 2 commits into from
May 18, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,10 @@ flags were resolved for the initial import:
- The script `regions.py` lists all regions and some selected sub-regions with their metadata.
- The script `regions-wp.py` shows the Wikipedia URL for the flag page.
- The script `missing.sh` shows all such regions that we don't have flags for.
- The script `make-aliases.sh` makes symlinks for regions that use flag of another
region.
- The script `download-wp.py` downloads missing flags from Wikipedia and generating
optimized SVG and PNG versions.
- The script `make-aliases.sh` makes symlinks for regions that use flag of another region.
- The script `download-wp.sh` downloads missing flags from Wikipedia and optionally you can generate optimized SVG and PNG versions (e.g., download-wp.py true).
- The script `update.sh` automatically regenerates all files and outputs optimized png.
- The script `convert.sh` converts SVG to PNG with ability to specify Width or Height.

You can use the [waveflag script from the Noto fonts project](https://code.google.com/p/noto/source/browse/color_emoji/waveflag.c)
to _wave_ PNG flags.
Expand All @@ -80,7 +80,8 @@ national laws), make a note of it in file `COPYING`.
To download missing flags, run `download-wp.py`.

To update to latest flags from Wikipedia, delete the `html`, `svg`, and `png`
directories, then run `make-aliases.sh` followed by `download-wp.py`.
directories, then run `make-aliases.sh` followed by `download-wp.py` or you can use `update.sh`.



# License
Expand Down
117 changes: 117 additions & 0 deletions convert.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
#!/usr/bin/env sh

echo "Options: w, width, h, height, n, normal, default"
echo -n "Enter your option: "
read OPTION

echo ""

echo "Options: yes, true, 1"
echo -n "Do you want to optimize the converted file? "
read OPTIPNGOP

updatealiases(){
echo "Updating aliases!"
IFS=' '
while read dst src; do
for dir in png; do
s="$dir/$src.$dir"
d="$dir/$dst.$dir"
cp -f "$s" "$d"
done
done < data/ALIASES
}

rsvgconvert() {
case $OPTIPNGOP in
"yes" | "true" | "1")
echo "Converting svg/$region.svg to png/$region.png.tmp"
if ! rsvg-convert $1 -f png -o "png/$region.png.tmp" "svg/$region.svg"; then
echo "ERROR rsvg-convert failed."
rm -f "png/$region.png.tmp"
else
echo "Optimizing png/$region.png!"
if ! optipng -quiet "png/$region.png.tmp"; then
echo "ERROR: optipng failed."
rm -f "png/$region.png.tmp"
continue
else
mv "png/$region.png.tmp" "png/$region.png"
fi
fi
echo ""
;;

*)
echo "Converting svg/$region.svg to png/$region.png" && rsvg-convert $1 -f png -o "png/$region.png" "svg/$region.svg"
;;
esac
}

usersvgconvert() {

WIDTH=$1;
HEIGHT=$2;

if [ $WIDTH -ne 0 ]; then
IFS=' '
cat SOURCES |
while read region htmlurl ; do
if [ -f "svg/$region.svg" ]; then
rsvgconvert "-a -w ${WIDTH}"
else
echo "File svg/$region.svg doesn't exist!"
fi
done
fi

if [ $HEIGHT -ne 0 ]; then
IFS=' '
cat SOURCES |
while read region htmlurl ; do
if [ -f "svg/$region.svg" ]; then
rsvgconvert "-a -h ${HEIGHT}"
else
echo "File svg/$region.svg doesn't exist!"
fi
done
fi

if [ $WIDTH -eq 0 ] && [ $HEIGHT -eq 0 ]; then

IFS=' '
cat SOURCES |
while read region htmlurl ; do
if [ -f "svg/$region.svg" ]; then
rsvgconvert "-a"
else
echo "File svg/$region.svg doesn't exist!"
fi
done
fi

updatealiases
}


case $OPTION in
w | width)
echo -n "Enter the prefered width: "
read WIDTH
usersvgconvert ${WIDTH} 0
;;

h | height)
echo -n "Enter the prefered height: "
read HEIGHT
usersvgconvert 0 ${HEIGHT}
;;

n | normal | default)
usersvgconvert 0 0
;;

*)
echo "Unknown option!"
;;
esac
117 changes: 64 additions & 53 deletions download-wp.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,57 +4,68 @@ IFS=' '

cat SOURCES |
while read region htmlurl ; do

html="html/$region.html"
svg="svg/$region.svg"
png="png/$region.png"

if ! test -s "$html"; then
rm -f "$html" "$svg" "$png"
echo "Downloading: $region $htmlurl"
if ! wget -q -O "$html" "${htmlurl}"; then
echo "ERROR: failed downloading html: ${htmlurl}"
continue
fi
if ! grep -q public_domain $html; then
echo "WARNING: flag NOT in public domain; check license"
fi
fi

if ! test -s "$svg"; then
svgurl=`cat "$html" | LANG=C sed 's@.*href="\(https://upload.wikimedia.org/wikipedia/commons/[^"]*[.]svg\)".*@\1@' | grep '^https://upload.wikimedia.org' | grep -v '/archive/' | head -n1`
if test "x$svgurl" = x; then
echo "ERROR: cannot find SVG URL in $html"
continue
fi
svgdata="`wget -q -O - "$svgurl"`"
if test "x$svgdata" = x; then
echo "ERROR: failed downloading SVG: $svgurl"
continue
fi
echo "$svgdata" > $svg.dos
if ! (dos2unix -q $svg.dos || fromdos $svg.dos); then
echo "ERROR: dos2unix failed."
#XXX rm -f $svg.dos
continue
else
mv $svg.dos $svg
fi
fi

if ! test -s "$png"; then
if ! rsvg-convert $svg > $png.tmp; then
echo "ERROR rsvg-convert failed."
rm -f $png.tmp
continue
fi
if ! optipng -quiet $png.tmp; then
echo "ERROR: optipng failed."
rm -f $png.tmp
continue
else
mv $png.tmp $png
fi
fi


html="html/$region.html"
svg="svg/$region.svg"
png="png/$region.png"

if ! test -s "$html"; then
rm -f "$html" "$svg" "$png"
echo "Downloading: $region $htmlurl"
if ! wget -q -O "$html" "${htmlurl}"; then
echo "ERROR: failed downloading html: ${htmlurl}"
continue
fi
if ! grep -q public_domain $html; then
echo "WARNING: flag NOT in public domain; check license"
fi
fi

if ! test -s "$svg"; then
svgurl=`cat "$html" | LANG=C sed 's@.*href="\(https://upload.wikimedia.org/wikipedia/commons/[^"]*[.]svg\)".*@\1@' | grep '^https://upload.wikimedia.org' | grep -v '/archive/' | head -n1`
if test "x$svgurl" = x; then
echo "ERROR: cannot find SVG URL in $html"
continue
fi
svgdata="`wget -q -O - "$svgurl"`"
if test "x$svgdata" = x; then
echo "ERROR: failed downloading SVG: $svgurl"
continue
fi
echo "$svgdata" > $svg.dos
if ! (dos2unix -q $svg.dos || fromdos $svg.dos); then
echo "ERROR: dos2unix failed."
#XXX rm -f $svg.dos
continue
else
mv $svg.dos $svg
fi
fi

case $1 in
"true" | "yes")
if ! test -s "$png"; then
echo "Converting $svg to $png.tmp"
if ! rsvg-convert $svg > $png.tmp; then
echo "ERROR rsvg-convert failed."
rm -f $png.tmp
continue
else
echo "Optimizing $png!"
if ! optipng -quiet $png.tmp; then
echo "ERROR: optipng failed."
rm -f $png.tmp
continue
else
mv $png.tmp $png
fi
fi
fi
;;

*)
continue
;;
esac
echo ""
done
3 changes: 2 additions & 1 deletion update.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@ python3 ./regions-wp.py > SOURCES
rm -rf html svg png
mkdir -p html svg png

bash ./download-wp.sh
bash ./download-wp.sh true
bash ./make-aliases.sh