Changeset 159

Show
Ignore:
Timestamp:
02/12/06 19:31:16 (3 years ago)
Author:
dsandler
Message:

The refactoring monster was here. Now Digg's link-extraction code is factored out, so other sites can be added.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • calcium/trunk/calcium.py

    r158 r159  
    11#!/usr/bin/env python 
     2 
     3""" 
     4Calcium: A script to seed the CoralCDN with pages from various "new links" 
     5feeds.  Calcium reads these feeds through the FeedTree proxy running on 
     6localhost, to ensure prompt discovery of new links (without placing undue 
     7stress on the webserver hoting the feed). 
     8 
     9The idea is that these "new links" will (presumably) show up on these feeds 
     10(and therefore be discovered by Calcium) before they become "hot" (and 
     11consequently unavailable, due to the Digg/Reddit/Slashdot effect). 
     12 
     13See also: 
     14    * FeedTree - http://feedtree.net 
     15    * CoralCDN - http://coralcdn.org 
     16""" 
    217 
    318import sys ; sys.path.append('lib') 
     
    823import coralcache 
    924 
     25from extractors import * 
     26from utils import * 
     27 
    1028class Feed: 
    11     def __init__(self, url, requires_crawl=False): 
    12         self.url = url ; self.requires_crawl = requires_crawl 
    13  
    14 USER_AGENT = 'Calcium/1.0 +http://feedtree.net/ (FeedTree + CoralCDN)' 
    15  
    16 def urlfetch(url, length=-1): 
    17     req = urllib2.Request(url) 
    18     req.add_header('User-Agent', USER_AGENT) 
    19     opener = urllib2.build_opener() 
    20     return opener.open(req).read(length) 
    21      
    22 def coral_seed(url): 
    23     sys.stdout.write('[%s]' % coralcache.coralize(url)) 
    24     page = urlfetch(coral_url, 1) 
    25     return True 
     29    def __init__(self, url, link_extractor=DefaultExtractor): 
     30        self.url = url ; self.link_extractor = link_extractor() 
    2631 
    2732CRAWL_FEEDS = [ 
    28     Feed('http://digg.com/rss/indexdig.xml', requires_crawl=True), 
     33    Feed('http://digg.com/rss/indexdig.xml', link_extractor=DiggExtractor), 
    2934    Feed('http://reddit.com/new.rss'), 
    3035    Feed('http://del.icio.us/rss/popular/'), 
     
    3439 
    3540URL_CACHE_FILE = 'feeds.shelf' 
    36  
    37 links_seen = shelve.open(URL_CACHE_FILE,'c') 
    38  
    39 print "Calcium: loaded %d old URLs" % len(links_seen) 
     41     
     42def coral_seed(url): 
     43    sys.stdout.write('[%s]' % coralcache.coralize(url)) 
     44    page = urlfetch(url, 1) 
     45    return True 
    4046 
    4147 
    42 def feedtree_fetch(url): 
    43     return urlfetch(PROXY_PREFIX + url
     48def main(): 
     49    links_seen = shelve.open(URL_CACHE_FILE,'c'
    4450 
    45 try: 
    46     for feedinfo in CRAWL_FEEDS: 
    47         try: 
    48             sys.stdout.write("Fetching from FeedTree: " + feedinfo.url) 
    49             page = feedtree_fetch(feedinfo.url) 
    50             sys.stdout.write(" (%d b)\n" % len(page)) 
    51             #print page[0:80] 
    52              
    53             doc = feedparser.parse(page) 
    54             #print doc 
    55             # print "  - Title: %s" % doc.title 
    56             print "  - Items: %d" % len(doc.entries) 
     51    print "Calcium: loaded %d old URLs" % len(links_seen) 
    5752 
    58             for e in doc.entries: 
    59                 link = str(e.link) 
    60                 if not link in links_seen: 
    61                     sys.stdout.write(" + Examining feed URL: " + link) 
    62                     if not feedinfo.requires_crawl: 
    63                         coral_url = link 
    64                     else: 
    65                         page = urlfetch(link) 
    66                         soup = BeautifulSoup(page) 
    67 # <div class="news-full" id="main0" style="z-index:1000"><div class="news-body"><h3 id="title1"><a href="http://www.chron.com/disp/story.mpl/ap/politics/3654155.html" > 
    68                         title = soup.first('h3') 
    69                         coral_url = title.a['href'] 
    70      
    71                     sys.stdout.write("\n   => Coralizing new URL: %s "  
    72                             % coral_url) 
    7353 
    74                     coral_seed(coral_url) 
    75                     sys.stdout.write(" (OK)\n"
     54    def feedtree_fetch(url): 
     55        return urlfetch(PROXY_PREFIX + url
    7656 
    77                     links_seen[link] = True 
    78                      
    79         except IOError, e
    80             print "\nIO exception: " + `e` 
    81             #raise e 
    82         except urllib2.HTTPError, e: 
    83             print "\nHTTP exception: " + `e` 
    84             # raise e 
    85         except KeyboardInterrupt: 
    86             print "Interrupted..." 
    87             links_seen.close() 
    88             sys.exit(1
     57    try: 
     58        for feedinfo in CRAWL_FEEDS: 
     59            try
     60                sys.stdout.write("Fetching from FeedTree: " + feedinfo.url) 
     61                page = feedtree_fetch(feedinfo.url) 
     62                sys.stdout.write(" (%d b)\n" % len(page)) 
     63                #print page[0:80] 
     64                 
     65                doc = feedparser.parse(page) 
     66                #print doc 
     67                # print "  - Title: %s" % doc.title 
     68                print "  - Items: %d" % len(doc.entries
    8969 
    90 except KeyboardInterrupt: 
    91     print "Interrupted..." 
     70                for e in doc.entries: 
     71                    link = str(e.link) 
     72                    if not link in links_seen: 
     73                        sys.stdout.write(" + Examining feed URL: " + link) 
     74                        coral_url = feedinfo.link_extractor.get_link(link) 
     75 
     76                        sys.stdout.write("\n   => Coralizing new URL: %s "  
     77                                % coral_url) 
     78 
     79                        coral_seed(coral_url) 
     80                        sys.stdout.write(" (OK)\n") 
     81 
     82                        links_seen[link] = True 
     83                         
     84            except IOError, e: 
     85                print "\nIO exception: " + `e` 
     86                #raise e 
     87            except urllib2.HTTPError, e: 
     88                print "\nHTTP exception: " + `e` 
     89                # raise e 
     90            except KeyboardInterrupt: 
     91                print "Interrupted..." 
     92                links_seen.close() 
     93                sys.exit(1) 
     94 
     95    except KeyboardInterrupt: 
     96        print "Interrupted..." 
     97        links_seen.close() 
     98        sys.exit(1) 
     99 
    92100    links_seen.close() 
    93     sys.exit(1
     101    sys.exit(0
    94102 
    95 links_seen.close() 
    96 sys.exit(0
     103if __name__ == '__main__': 
     104    main(