CLI twitpic

6 Mar 2010

by hemanth

  1. #!/bin/bash
  2. #==================================================================
  3. #
  4. # FILE: cli-twitpic.bash
  5. #
  6. # USAGE: ./cli-twitpic.bash
  7. #
  8. # DESCRIPTION: Allows you to upload pic from cli to twitpic and # also update the same as you status in twitter
  9. #
  10. # OPTIONS: ---
  11. # REQUIREMENTS: ---
  12. # BUGS: ---
  13. # NOTES: ---
  14. # AUTHOR: Hemanth H.M (), hemanth.hm@gmail.com
  15. # VERSION: 1.0
  16. # CREATED: 03/06/2010 01:45:02 AM IST
  17. # REVISION: ---
  18. #=================================================================#
  19.  
  20. URL=http://twitpic.com/api/uploadAndPost
  21.  
  22. # Fields to post in (post data should be formatted as multipart/form-data):
  23. #- media (required) - Binary image data
  24. #- username (required) - Twitter username
  25. #- password (required) - Twitter password
  26. #- message (optional) - Message to post to twitter. The URL of the #image is automatically added.
  27.  
  28. #Sample response:
  29.  
  30. #<?xml version="1.0" encoding="UTF-8"?>
  31. #<rsp status="ok">
  32. #<statusid>4567</statusid>
  33. #<userid>4567</userid>
  34. #<mediaid>hmh123</mediaid>
  35. #<mediaurl>http://twitpic.com/hmh123</mediaurl>
  36. #</rsp>
  37.  
  38. # Read input to an array BASHv4 only, other version tweak to code # to read -a to read it to normal array or use individual variables
  39.  
  40. # Declares an associative array called data
  41. declare -A data
  42.  
  43. data[USER]=$1
  44. data[PASS]=$2
  45. data[PIC]="$3"
  46. data[TWIT]=$4
  47.  
  48. # Validate inputs
  49. for key in USER PASS PIC TWIT; do
  50. [[ data[$key]="" ]] && echo "Enter $key:" && read data[$key]
  51. done
  52.  
  53. # Check if the pic file exists
  54. [[ -f ${data[PIC]} ]] || echo Error: FileNotFound ${data[PIC]} && exit 1
  55.  
  56. echo Uploading...
  57. $CURL \
  58. --form username=$TWITTER_USER \
  59. --form password=$TWITTER_PW \
  60. --form media=@"$PHOTO" \
  61. --form message="$TWEET_MSG" $UPLOAD_URL
  62.  
  63. # Check if all went fine
  64. [[ $? = 6 ]] && echo "Error will uploading! Check credentials" && exit 1
  65. echo "Pic uploaded!"
Share this