We Recommend

Learning Python Learning Python
The authors of Learning Python show you enough essentials of the Python scripting language to enable you to begin solving problems right away, then reveal more powerful aspects of the language one at a time. This approach is sure to appeal to programmers and system administrators who have urgent problems and a preference for learning by semi-guided experimentation.


Posted By

abhiomkar on 03/12/07


Tagged

snipplr python


Versions (?)


snipplr :: getCode()


Published in: Python 


  1. # Snippler - getCode()
  2. # snipplr
  3. # Author : Abhinay Omkar
  4.  
  5. import re,urllib2,sys
  6.  
  7. # input url = "http://snipplr.com/view/8/ajax-edit-in-place/"
  8. try:
  9. inurl = sys.argv[1]
  10. except:
  11. print "Err: url is missing."
  12. sys.exit()
  13. try:
  14. path = sys.argv[2]
  15. except:
  16. path = inurl.split('/')[-2]
  17.  
  18. num = re.sub(r'[a-zA-Z/:\-.]*','',inurl)
  19.  
  20. url = "http://snipplr.com/view.php?codeview&id="+num
  21.  
  22. raw = repr(urllib2.urlopen(url).read())
  23. prog = re.compile(r'<textarea rows="25">(.*)</textarea>').search(raw).group()
  24. open
  25.  
  26. prog = prog.replace('\\r\\n','\n').replace('\\t',' ')[20:-11]
  27. print "File "+path+" Opened..."
  28. print prog
  29. open(path,'w').write(prog)

Report this snippet 

You need to login to post a comment.