Quantcast
Channel: ScrollingText - CouchDB
Viewing all articles
Browse latest Browse all 3

Tweet Dump: Resurrection part 2

$
0
0

I don't know about you, but when I hear or read the same thing three or so times from random sources, I pay attention. And the pattern to each of these comments have been about one thing: in the previous post in this thread I did a shell out to curl from Twitter. Not only that, but I used regular expressions for parsing xml. I won't deny it... I made some pretty bad mistakes. The only consolation I have regarding these mistakes is that I made them over a year and a half ago when I was just starting to learn Python and not aware of just how many libraries the standard install includes.

To help with the learning process, I'm going to show the original version as well as the “fixed” version.

Original:

  1. def get_tweets():
  2. final_texts =[]
  3. command ="curl http://twitter.com/statuses/public_timeline.xml | grep -A 3 '<status>'"
  4. process =subprocess.Popen(command, stdout=subprocess.PIPE, error=subprocess.PIPE, shell=True)
  5. (proc, error)= process.communicate()
  6. sys.stdout.flush()
  7.  
  8. if error:
  9. print(error)
  10. sys.exit(1)
  11.  
  12. ids =re.findall('[0-9]{10,13}', proc)
  13. texts =re.findall('<text>[\x20-\x7E]+</text>', proc)
  14.  
  15. #check that the number of tweets ='s the number of ids
  16. # strip <text> fields from front & read
  17. iflen(ids)==len(texts):
  18. final_texts =[ i.rstrip('</text>').lstrip('<text>')for i in texts]
  19.  
  20. return(ids, final_texts)
  21.  
  22. else:
  23. return(0,0)

Fixed:

  1. def get_tweets():
  2. ids =[]
  3. texts =[]
  4. try:
  5. xmltree =xml.etree.ElementTree.parse(
  6. urllib.urlopen(
  7. 'http://twitter.com/statuses/public_timeline.xml'
  8. ))
  9. except:
  10. return(0,0)
  11.  
  12. for x in xmltree.getiterator("status"):
  13. ids.append(x.find("id").text)
  14. texts.append(x.find("text").text)
  15.  
  16. return(ids, texts)

For starters the changes I made were ditching the shell out to curl and got the data from Twitter using the urllib library. Since I was grabbing xml from Twitter, the output from the urllib.urlopen function could then very easily be parsed and sorted by the xml.etree.ElementTree.parse function. And since I had all the data in an xml object, I could very easily get both the tweet text and the Twitter ID number.

I don't think I can stress enough how much cleaner the code is to read in the fixed version. I feel that part of the cleanliness comes from using the built-in libraries instead of trying to hack something together. As an added bonus, since the code uses the Python built-in libraries this code can now run on multiple platforms.

So there you have it, Internets. Once again I have wronged you by making a mistake and have only now gotten around to understanding how horrible of a mistake I made. Can you forgive me? Thank you.

For the super observant of you, one might notice that I also fixed a bug from the original version of get_Tweets and the version from the last thread. Happy Hunting.


Viewing all articles
Browse latest Browse all 3

Latest Images

Trending Articles





Latest Images