Skip to content

Instantly share code, notes, and snippets.

@chengdujin
Created March 2, 2012 18:11
Show Gist options
  • Select an option

  • Save chengdujin/1960128 to your computer and use it in GitHub Desktop.

Select an option

Save chengdujin/1960128 to your computer and use it in GitHub Desktop.
simply pull hacker news items from the api, 30 items/one pull. and update news against old ones in mongodb
#!/usr/bin/python
# -*- coding: utf-8 -*-
##
# this script serves to grab the latest items in the hacker news
# stream, and store them into the mongodb
#
# @author Yuan JIN
# @contact chengdujin@gmail.com
# @since 2012.02.29
# @latest 2012.03.01
'''
Copyright (c) 2012-2013, Yuan JIN
All rights reserved.
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
    * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
    * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
    * Neither the name of Redis nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
'''
# reload the script encoding
import sys
reload(sys)
sys.setdefaultencoding("UTF-8")
def update_and_insert(db, doc):
'dealing with mongodb'
cursor = db.hn_articles.find({'title':'%s' % doc['title']})
if cursor.count() > 0:
for record in cursor:
updated = False
if record['postedago'] <> doc['postedago']:
db.hn_articles.update(record, doc)
updated = not updated
elif record['commentcount'] <> doc['commentcount']:
print doc
db.hn_articles.update(record, doc)
updated = not updated
elif record['points'] <> doc['points']:
db.hn_articles.update(record, doc)
updated = not updated
#if updated:
# print '* "%s" is updated!' % doc['title']
else:
#print '+ "%s" is added!' % doc['title']
db.hn_articles.insert(doc)
def parse_and_store(data):
'parse the xml and store the result in mongodb'
from pymongo import Connection
con = Connection('localhost', 27017)
db = con.hn_articles
from BeautifulSoup import BeautifulStoneSoup
root = BeautifulStoneSoup(data)
page_items = root.contents[2].contents[1].contents
for page_item in page_items:
if page_item <> '\n':
doc = {}
for item in page_item.contents:
if item <> '\n':
doc[item.name] = str(item.string)
update_and_insert(db, doc)
def data_recv():
'start a http get request'
import httplib
con = httplib.HTTPConnection('api.ihackernews.com')
con.request("GET", "/page?format=xml")
resp = con.getresponse()
data = resp.read()
con.close()
return data
def main():
'entrance to http get, parsing and storing'
data = data_recv()
parse_and_store(data)
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment