config = open(os.path.expanduser('~') + '/.instapaperrc') for line in config: matches = login.match(line) if matches: args.username = matches.group(1).strip() args.password = matches.group(2).strip() break if '' == args.username: print >> sys.stderr, 'No username:password line found in ~/.instapaperrc' ap.exit(-1) except IOError: ap.error('Please specify a username with -u/--username.') # Log in to the Instapaper API. instapaper = Instapaper(args.username, args.password) (auth_status, auth_message) = instapaper.auth() # 200: OK # 403: Invalid username or password. # 500: The service encountered an error. if 200 != auth_status: print >> sys.stderr, auth_message ap.exit(-1) # Get the Reading List items rlr = ReadingListReader() articles = rlr.read() for article in articles: (add_status,
class Instayc: section = 'instayc' def __init__(self): self.insta = None self.config = self.loadConfig() def loadConfig(self): config = SafeConfigParser() filename = OPTIONS.get('config_filename') if os.path.exists(filename): config.read(filename) else: with open(filename, 'wb') as configfile: config.add_section(self.section) config.set(self.section, 'interests', '') config.write(configfile) return config def saveConfig(self): with open(OPTIONS.get('config_filename'), 'wb') as configfile: self.config.write(configfile) def requires_login(fn): def wrapped(self): try: self.authorize(self.config.get(self.section, 'email'), self.config.get(self.section, 'password')) except NoOptionError: self.login() fn(self) return wrapped def authorize(self, email, password): self.insta = Instapaper(email , password) (code, message) = self.insta.auth() if code == 403: self.login() else: self.config.set(self.section, 'email', email) self.config.set(self.section, 'password', password) self.saveConfig() def login(self): email = raw_input('Instapaper email: ') password = getpass('Instapaper password: '******'interests') if current_interests == '': current_interests = interests else: current_interests = current_interests + ', ' + interests self.config.set(self.section, 'interests', current_interests) self.saveConfig() @requires_login def update(self): print "Updating..." posts = parse(OPTIONS['feeds']['yc']).entries interests = self.config.get(self.section, 'interests').split(',') def map(words): mapping = [] for word in words: mapping.append((word.strip(), 1)) return mapping def reduce(mapping): counted = {} for word, count in mapping: counted[word] = counted.get(word, 0) + count return counted found = [] for post in posts: title = str(post['title'].encode('utf-8')) title_words = [word.lower() for word in title.split(' ')] title_words.extend(interests) counted = reduce(map(title_words)) matches = [word for word, count in counted.items() if count > 1] if matches: found.append((post['link'], title)) self.insta.add_item(post['link'], title) print "Added %d article(s) to Instapaper: \n" % len(found) for link, title in found: print title print ''
print 'File \'~/.readinglist2instapaper/.lastsyncdate\' not found. Using 1970-01-01 00:00:00 as synconize from date.' # Storing our current time for syncing later. # Doing it this way could mean a duplicate article is sync'ed, but I'd rather have a dup then loss an article. print 'Updating ~/.readinglist2instapaper/.lastsyncdate file.' lastsyncdate = parse(datetime.datetime.strftime(datetime.datetime.now(), '%Y-%m-%d %H:%M:%S')) lastsyncdateFile = open(os.path.expanduser('~/.readinglist2instapaper') + '/.lastsyncdate', 'w+') lastsyncdateFile.write(str(lastsyncdate)) # Paranoia.. lastsyncdateFile.seek(0) lastsyncdate = lastsyncdateFile.readline() print 'Using %s as the subsequent syncronized date.' % lastsyncdate # Log in to the Instapaper API. instapaper = Instapaper(args.username, args.password) (auth_status, auth_message) = instapaper.auth() # 200: OK # 403: Invalid username or password. # 500: The service encountered an error. if 200 != auth_status: print >> sys.stderr, auth_message ap.exit(-1) # Get the Reading List items rlr = ReadingListReader() articles = rlr.read( show = all, syncdate = args.syncdate) for article in articles:
class Instayc: section = "instayc" def __init__(self): self.insta = None self.config = self.loadConfig() def loadConfig(self): config = SafeConfigParser() filename = OPTIONS.get("config_filename") if os.path.exists(filename): config.read(filename) else: with open(filename, "wb") as configfile: config.add_section(self.section) config.set(self.section, "interests", "") config.write(configfile) return config def saveConfig(self): with open(OPTIONS.get("config_filename"), "wb") as configfile: self.config.write(configfile) def requires_login(fn): def wrapped(self): try: self.authorize(self.config.get(self.section, "email"), self.config.get(self.section, "password")) except NoOptionError: self.login() fn(self) return wrapped def authorize(self, email, password): self.insta = Instapaper(email, password) (code, message) = self.insta.auth() if code == 403: self.login() else: self.config.set(self.section, "email", email) self.config.set(self.section, "password", password) self.saveConfig() def login(self): email = raw_input("Instapaper email: ") password = getpass("Instapaper password: "******"interests") if current_interests == "": current_interests = interests else: current_interests = current_interests + ", " + interests self.config.set(self.section, "interests", current_interests) self.saveConfig() @requires_login def update(self): print "Updating..." posts = parse(OPTIONS["feeds"]["yc"]).entries interests = self.config.get(self.section, "interests").split(",") def map(words): mapping = [] for word in words: mapping.append((word.strip(), 1)) return mapping def reduce(mapping): counted = {} for word, count in mapping: counted[word] = counted.get(word, 0) + count return counted found = [] for post in posts: title = str(post["title"].encode("utf-8")) title_words = [word.lower() for word in title.split(" ")] title_words.extend(interests) counted = reduce(map(title_words)) matches = [word for word, count in counted.items() if count > 1] if matches: found.append((post["link"], title)) self.insta.add_item(post["link"], title) print "Added %d article(s) to Instapaper: \n" % len(found) for link, title in found: print title print ""