uploading HTML documents to Metaweb

[ permalink ] [ download ]
import sys, re, metaweb

# Read the content of the file specified on the command line
# It must be an HTML file with a <title>
filename = sys.argv[1]
try:
    f = open(filename)
    doc = f.read()
    f.close()
except Exception, e:
    sys.exit(e)

# Search through the document for a title
try: title = re.search("(?i)<title>(.*)</title>", doc).group(1)
except: sys.exit("Document has no title")
    
# Log in to Metaweb.  Put your own username and password here
credentials = metaweb.login('username','password')

# Upload the document content to Metaweb.
# Note that we hardcode the text/html content type.
content_id = metaweb.upload(doc, "text/html", credentials)

# Submit a MQL write query to create a /common/topic and /common/document
# for the uploaded content
result = metaweb.write({'create':'unless_exists',
                        'type':'/common/topic',
                        'id':None,
                        'name':title,
                        'article' : { 'create':'unless_exists',
                                      'type':'/common/document',
                                      'id':None,
                                      'content':content_id }},
                       credentials)
    
# Tell the user what we did
print "Uploaded %s: %s\n\tcontent: %s\n\tdocument: %s %s\n\ttopic: %s %s" % (
    filename, title, content_id,
    result['article']['create'], result['article']['id'], 
    result['create'], result['id'])
hits counter