Dr. Mark Humphrys

School of Computing. Dublin City University.

Online coding site: Ancient Brain

 

Search:


Einstein - Flickr

  

Get images from Flickr

Write a shell script to get images from the Flickr image sharing site and make a web page to display them.

Background


The script

Shell script usage ideally would be like:
 flickr (tag) 
Get latest images tagged with this tag, and make a web page to display them.
For example:
 flickr belfast
 flickr dublin 
Send the output into this file:
 $HOME/flickr.html 
  

Lab exam needs

For reasons explained before, in the lab exam we need extension .sh and call it with "./" in front of it.
Also for this lab we will call it flickr6.

More lab exam needs - Reduce network requests

  

Getting started

Right click on one of the feeds above and save into a file called feed.json
The entire practical is now manipulating that file to build a webpage.

Now the structure of your program will be:

# comment and uncomment this line:
exec > [output file]

cat feed.json | [various commands to build a webpage]

And we run it with no argument, just:

 ./flickr6.sh  
You can comment and uncomment the "exec" line to switch between output to screen and output to file.
  

HTML snippets

  1. At first the JSON you get back looks weird.
  2. Then you notice there are snippets of usable HTML in it, like this:
     "description": " HTML payload (encoded) ", 
  3. For each photo, we are going to extract the descriptions and the list of tags.
  4. You can use egrep to extract lines containing either X or Y using:
     egrep 'X|Y' 
  5. The patterns we want to look for are:
    "description":
    "tags":
    
  6. Your program will now have a line:

    cat feed.json | [egrep command]
    

  7. Test that your program now extracts the descriptions and lists of tags, and nothing else.

  

Fix the snippets

  1. The descriptions are HTML snippets, encoded. Decode them to normal HTML.
  2. You can do this using tr to remove one character. What character?
  3. Your program will now have a line:

    cat feed.json | [egrep command] | [tr command]  
    

  4. Set the "exec" command to output to a file. Now double click on the output file and you will actually see the images.

  

Final tidy

  1. It needs some tidying. Use sed to insert some space before each item:

     
    EXTRA="<hr style='margin-top:70'><h1> next photo </h1>"
    
    cat feed.json | [egrep command] | [tr command] | [sed command]
    
    # sed command will change:
    #  <p>
    # to:
    #  $EXTRA<p>
    # only make that change once on each line
    

  2. Now double click on the output file and you will see the images better laid out, with some nice space and a title before each one.
  

Upload to Einstein

Einstein expects each description line to be all on one line, and each tags line to be all on one line, and there be no other lines:
.. description line ...
.. tags line ...
.. description line ...
.. tags line ...

You can insert extra HTML on a line (like we did with the vertical space). But do not make new lines, or combine lines, with the version you send to Einstein, or Einstein will get confused.

  

Afterwards (optional)

After you have got your marks, you can fix up the program as follows:
  1. The program will do the wget every time it runs.
  2. Construct the feed URL based on the command-line argument.
  3. You need to surround the URL with quotes because it has character "&" in it.
  4. Remember the two types of quotes.
  5. Use wget to fetch the feed and then go straight to the pipe. No need to have an intermediate file feed.json
  6. You could do some more tidying up. There is some loose text that needs tidying up. You could make lots of improvements.
On a normal setup, it does not need .sh, it can be called anything, and it will be in the PATH.
So we can call it flickr and run it like:
 flickr belfast
 flickr dublin