User:TheodoreRowy/Sandbox/list of ponies script


 * 1) !/bin/bash

echo "
 * 1) LIST OF PONIES SCRIPT
 * 2) This script reads the article 'List of ponies' and generates the transcluded table from the article.
 * 3) This script is written by a non-programmer, please avert your eyes if you are sensitive to ugly code.
 * 4) This script is released under the MIT license. For the love of God please refactor it in python.
 * 5) http://opensource.org/licenses/MIT


 * 1) Dependencies:
 * 2) bash 4.3.33
 * 3) pywikibot 2.0, configured for mlp.wikia.com
 * 4) coreutils (cut, sort, wc)
 * 5) awk
 * 6) grep
 * 7) sed

"
 * 1)      THIS SCRIPT HAS NO EXCEPTION HANDLING     almost #
 * 1)      THIS SCRIPT HAS NO EXCEPTION HANDLING     almost #

if [ ! "1" == "$(python --version |& grep -c 'Python')" ] then echo "Cannot find python. Exiting." exit 1 fi
 * 1) Make sure python is executable

echo "Please enter the installation directory of pywikibot." echo "Usually pywikibot is insalled in /home/username/.pywikibot" read pwbdir pwbdir=${pwbdir%/} pwb="${pwbdir}/pwb.py"
 * 1) Prompt for pywikibot directory

if [ ! -w. ]   then echo "Cannot write to working directory. Exiting." exit 1 fi if [ ! -d listofponies ] then rm -f listofponies # I hope there was nothing important in that file mkdir listofponies fi cd listofponies
 * 1) Check if the working directory is writable

python ${pwb} listpages -save -page:"List of ponies"
 * 1) Get up-to-date list of ponies

grep "#lst:List" List_of_ponies | grep -o "List.of.[^|]*" | sort -u > pages
 * 1) Find the pages with actual content

python ${pwb} listpages -save -file:"pages"
 * 1) Get up-to-date pages

grep "#lst:List" List_of_ponies | sed -e 's/^.*#lst:\|}}//g' -e 's@[/ ]@_@g' > sections
 * 1) Strip and format page names and sections

length=$(wc -l sections | cut -d " " -f 1)
 * 1) Count the number of sections

let i=1 while [ $i -le ${length} ] do line=$(awk "NR==${i}" sections) file=$(echo ${line} | cut -d '|' -f 1) section=$(echo ${line} | cut -d '|' -f 2) echo "|-" awk "/begin=\"${section}\"/{pony=1}/end=\"${section}\"/{print;pony=0}pony" ${file} let i=i+1 done > content
 * 1) Print each section from beginning to end

{   echo "" # pwb markup echo "User:TheodoreRowy/Sandbox" # pwb name of article echo "This is a sortable list of all ponies which have been mentioned or appear in My Little Pony Friendship is Magic. Official names are in bold. These names are mentioned on the show, on Hasbro's toy line, or stated to be official by the show's crew. Names that are not in bold are placeholder names which are unofficial and will soon be up for vote. Names in gray are up for vote right now. Colors and times are approximate." # lead section awk "/{\|/{f=1}/\|Image/{print;f=0}f" List_of_ponies # table headers sed -e 's/]*>//g' -e 's/{{ambox[^}|]*}}//' -e 's/ //g' content # ponies echo '|}' # table end echo "Generated with List of ponies script." echo "" # pwb markup } > full_list
 * 1) Add header and footer to content

echo "Uploading about 1MB, this may take a while." python ${pwb} pagefromfile -start: -end: -force -notitle -summary:"Updated full list of ponies" -file:"full_list"
 * 1) Upload the full list to the wiki

cd .. printf "\n\n\n\a" echo "Temporary files saved in $(pwd)/listofponies" exit 0