예제 #1
0
파일: dlcs.py 프로젝트: yunxingwoo/PCI
def mates(conf, dlcs, *args, **opts):

    """The following was adapted from delicious_mates.
    http://www.aiplayground.org/artikel/delicious-mates/

        % dlcs mates [max_mates[, min_bookmarks[, min_common]]]

    """
   
    max_mates, min_bookmarks, min_common = map(int, opts['mates'].split(','))

    delicious_users = {}
    posts = cached_posts(conf, dlcs, opts['keep_cache'])
    print "Getting mates for collection of %i bookmarks" % len(posts['posts'])

    print "\nUsers for each bookmark:"
    for i, post in enumerate(posts['posts']):

        hash = md5.md5(post['href']).hexdigest()
        #urlfeed = pydelicious.dlcs_feed('urlinfo', urlmd5=hash)
        posts = dlcs_feed('url', count='all', format='rss', urlmd5=hash)
        usernames = [e['author'] for e in posts['entries']]

        print "    %i. %s (%i)" % (i+1, post['href'], len(usernames))
       
        for username in usernames:
            if username != dlcs.user:
                delicious_users.setdefault(username, (0.0, 0))
                (weight, num_common) = delicious_users[username]
                new_weight = weight + 1.0/math.log(len(usernames)+1.0)
                delicious_users[username] = (new_weight, num_common + 1)
    
    print "\n%i candidates from list of %i users" % (max_mates, len(delicious_users))
    friends = {}
    for (username, (weight, num_common)) in value_sorted(delicious_users):
        if num_common >= min_common:

            num_bookmarks = float([e['summary'] for e in 
                dlcs_feed('user_info', format='rss', username='******')['entries'] 
                if e['id'] == 'items'][0])

            print "    %s (%i/%i)" % (username, num_common, num_bookmarks),
            if num_bookmarks >= min_bookmarks:
                friends[username] = (weight*(num_common/num_bookmarks), num_common, num_bookmarks)
                if len(friends) >= max_mates:
                    break
            else:
                print
            time.sleep(1)
    
    print "\nTop %i del.icio.us mates:" % max_mates
    print "username".ljust(20), "weight".ljust(20), "# common bookmarks".ljust(20), "# total bookmarks".ljust(20), "% common"
    print "--------------------------------------------------------------------------------------------"
    for (username, (weight, num_common, num_total)) in value_sorted(friends)[:max_mates]:
        print username.ljust(20),
        print ("%.5f" % (weight*100)).ljust(20),
        print str(num_common).ljust(20),
        print str(int(num_total)).ljust(20),
        print "%.5f" % ((num_common/num_total)*100.0)
예제 #2
0
def main(argv):
    optparser, opts, args = parse_argv(__options__, argv, __usage__)
    kwds = {}
    for k in ('format', 'username', 'tag', 'urlmd5', 'key'):
        v = getattr(opts, k)
        if type(v) != type(None):
            kwds[k] = v
    if opts.clear_screen:
        if os.name == 'posix':
            os.system('clear')
        else:
            os.system('cls')
    matches, candidates = feeds_for_params(**kwds)
    if opts.list_feeds:
        print >> sys.stderr, "Feeds for current parameters (%s)" % kwds
        if matches:
            print "Exact matches:"
        for m in matches:
            print '\t' + m + ':', pydelicious.delicious_v2_feeds[m] % kwds
        if candidates:
            print "Candidates:"
        for m in candidates:
            print '\t' + pydelicious.delicious_v2_feeds[m]
        sys.exit()
    path = args and args.pop(0)
    if not path:
        if len(matches) == 1:
            path = matches[0]
            print >> sys.stderr, "Setting path to %s" % path
        elif matches:
            print >> sys.stderr, "Multiple paths for given parameters, see -L"
            sys.exit()
    assert path in pydelicious.delicious_v2_feeds
    return pydelicious.dlcs_feed(path, **kwds)
예제 #3
0
 def __init__(self, username, password, key, logfile, verbose=False):
     self.inbox = pydelicious.dlcs_feed('user_inbox', format='rss',
                     username=username, key=key)
     self.api = pydelicious.DeliciousAPI(username, password)
     self.posters = json_load(pydelicious.json_network(username))
     self.logfile = logfile
     self.verbose = verbose
예제 #4
0
def main(argv):
	optparser, opts, args = parse_argv(__options__, argv, __usage__)
	kwds = {}
	for k in ('format','username','tag','urlmd5','key'):
		v = getattr(opts, k)
		if type(v) != type(None):
			kwds[k] = v
	if opts.clear_screen:
		if os.name == 'posix':
			os.system('clear')
		else:
			os.system('cls')
	matches, candidates = feeds_for_params(**kwds)
	if opts.list_feeds:
		print >>sys.stderr,"Feeds for current parameters (%s)" % kwds
		if matches:
			print "Exact matches:"
		for m in matches:
			print '\t'+m+':', pydelicious.delicious_v2_feeds[m] % kwds
		if candidates:
			print "Candidates:"
		for m in candidates:
			print '\t'+pydelicious.delicious_v2_feeds[m]
		sys.exit()
	path = args and args.pop(0)
	if not path:
		if len(matches) == 1:
			path = matches[0]
			print >>sys.stderr, "Setting path to %s" % path
		elif matches:
			print >>sys.stderr, "Multiple paths for given parameters, see -L"
			sys.exit()
	assert path in pydelicious.delicious_v2_feeds
	return pydelicious.dlcs_feed(path, **kwds)
예제 #5
0
def mates(conf, dlcs, *args, **opts):
    """The following was adapted from delicious_mates.
    http://www.aiplayground.org/artikel/delicious-mates/

        % dlcs mates [max_mates[, min_bookmarks[, min_common]]]

    """

    max_mates, min_bookmarks, min_common = map(int, opts['mates'].split(','))

    delicious_users = {}
    posts = cached_posts(conf, dlcs, opts['keep_cache'])
    print "Getting mates for collection of %i bookmarks" % len(posts['posts'])

    print "\nUsers for each bookmark:"
    for i, post in enumerate(posts['posts']):

        hash = md5.md5(post['href']).hexdigest()
        #urlfeed = pydelicious.dlcs_feed('urlinfo', urlmd5=hash)
        posts = dlcs_feed('url', count='all', format='rss', urlmd5=hash)
        usernames = [e['author'] for e in posts['entries']]

        print "    %i. %s (%i)" % (i + 1, post['href'], len(usernames))

        for username in usernames:
            if username != dlcs.user:
                delicious_users.setdefault(username, (0.0, 0))
                (weight, num_common) = delicious_users[username]
                new_weight = weight + 1.0 / math.log(len(usernames) + 1.0)
                delicious_users[username] = (new_weight, num_common + 1)

    print "\n%i candidates from list of %i users" % (max_mates,
                                                     len(delicious_users))
    friends = {}
    for (username, (weight, num_common)) in value_sorted(delicious_users):
        if num_common >= min_common:

            num_bookmarks = float([
                e['summary'] for e in dlcs_feed(
                    'user_info', format='rss', username='******')['entries']
                if e['id'] == 'items'
            ][0])

            print "    %s (%i/%i)" % (username, num_common, num_bookmarks),
            if num_bookmarks >= min_bookmarks:
                friends[username] = (weight * (num_common / num_bookmarks),
                                     num_common, num_bookmarks)
                if len(friends) >= max_mates:
                    break
            else:
                print
            time.sleep(1)

    print "\nTop %i del.icio.us mates:" % max_mates
    print "username".ljust(20), "weight".ljust(20), "# common bookmarks".ljust(
        20), "# total bookmarks".ljust(20), "% common"
    print "--------------------------------------------------------------------------------------------"
    for (username, (weight, num_common,
                    num_total)) in value_sorted(friends)[:max_mates]:
        print username.ljust(20),
        print("%.5f" % (weight * 100)).ljust(20),
        print str(num_common).ljust(20),
        print str(int(num_total)).ljust(20),
        print "%.5f" % ((num_common / num_total) * 100.0)
예제 #6
0
    def check(self):
        """Check inbox for new items."""
        
        logfh = None
        
        if os.access(self.logfile, os.F_OK):
            if self.verbose:
                print "[LOG] Log file found. Trying to resume...",
            try:
                # read in previous log data for resuming
                logfh = open(self.logfile, 'r')
                logfh.readline()
                logfh.close()
                if self.verbose:
                    print "OK"
            except IOError:
                # most probably, the log file does not exist (yet)
                if self.verbose:
                    print "failed"
        else:
            # log file does not exist, so there isn't any resume data
            # to read in
            pass

        try:
            if self.verbose:
                print "[LOG] Open log file for appending...",
            logfh = open(self.logfile, 'a')
            if self.verbose:
                print "done"
        except IOError:
            if self.verbose:
                print "failed"
            print "[LOG] ERROR: could not open log file for appending"
            self._cleanup()
            return
        
        # query metadata about each entry from delicious.com
        for index, entry in enumerate(self.inbox.entries):
            url = entry.link
            urlmd5 = md5.new(url).hexdigest()
            if entry.author in self.posters:
                if self.verbose:
                    logfh.write("[LOG] %s Processing entry #%s: '%s'\n" % \
                        (strftime("%Y-%m-%d %H:%M:%S"), index + 1, url))
                try:
                    sleep(1) # be nice and wait 1 sec between connects 
                    urlinfo = json_load(pydelicious.dlcs_feed('urlinfo',
                                urlmd5=urlmd5))
                    if urlinfo:
                        urlinfo = urlinfo[0]
                    else:
                        urlinfo = {}
                    title = urlinfo['title']
                    top_tags = urlinfo['top_tags'] or []
                    tagstr = 'via:%s ' % entry.author + \
                        ' '.join([tag.replace(' ','_') for tag in top_tags]) 
                    self.api.posts_add(url, title, tags=tagstr.strip())
                    if self.verbose:
                        logfh.write("[LOG] %s Saved %s\n" % \
                            (strftime("%Y-%m-%d %H:%M:%S"), url))
                except KeyError:
                    pass
                except pydelicious.DeliciousItemExistsError:
                    if self.verbose:
                        logfh.write("[LOG] %s %s already added\n" % \
                            (strftime("%Y-%m-%d %H:%M:%S"), url))
                except:
                    logfh.write("[LOG] %s ERROR: %s\n" % \
                        (strftime("%Y-%m-%d %H:%M:%S"), sys.exc_info()[0]))
                    # clean up
                    logfh.close()
                    raise
            else:
                logfh.write("[LOG] %s ERROR: %s not in network-aborting %s\n" % :\
                    (strftime("%Y-%m-%d %H:%M:%S"), entry.author, url))
                
        # clean up
        logfh.close()