jaysonjphillips
9/6/2014 - 3:35 AM

archive-twitpic-data.py

# Archive your Twitpic photos and metadata
#
# A cleaned-up fork of Terence Eden's original archiver:
# http://shkspr.mobi/blog/2013/08/exporting-twitpic-images-python/
#
# License: MIT

import urllib
import urllib2
import json
import time
import os

USERNAME = "your_username_goes_here"
NUMBER_OF_PAGES_TO_DOWNLOAD = 5

# Target Page
api = "https://api.twitpic.com/2/users/show.json?username=%s&page=" % USERNAME

# Get the data about the target page
for page in range(1, NUMBER_OF_PAGES_TO_DOWNLOAD+1):
  print page
  raw_data = urllib2.urlopen(api + str(page))
  json_data = json.load(raw_data)

  # Save the page data
  page_file = open("page-%s.json" % page,"w")
  page_file.write(json.dumps(json_data, indent=2))
  page_file.close()

  # Get the info about each image on the page
  images = json_data["images"]

  for item in images:
    file_id = item["short_id"]
    file_type = item["type"]
    file_time = time.mktime(time.strptime(item["timestamp"], "%Y-%m-%d %H:%M:%S"))
    file_url = "https://twitpic.com/show/full/"+file_id
    file_name = file_id + "." + file_type

    # Save the file
    urllib.urlretrieve (file_url, file_name)

    # Set the file time
    os.utime(file_name,(file_time, file_time))