#coding=utf-8 # from nntplib import NNTP from time import strftime, time, localtime day = 24 * 60 * 60 # Number of seconds in one day yesterday = localtime(time() - day) date = strftime('%y%m%d', yesterday) hour = strftime('%H%M%S', yesterday) servername = 'news.mixmin.net' group = 'talk.euthanasia' server = NNTP(servername) ids = server.newnews(group, date, hour)[1] for id in ids: head = server.head(id)[3] for line in head: if line.lower().startswith('subject:'): subject = line[9:] break body = server.body(id)[3] print subject print '-'*len(subject) print '\n'.join(body) server.quit()
def gmane(self): try: gmane = NNTP("news.gmane.org") except: return self.gmane gmane.group(self.group) return gmane
def download_group(name): if not os.path.exists(name): os.mkdir(name) s = NNTP('news.gmane.org') resp, count, first, last, name = s.group(name) print 'Group', name, 'has', count, 'articles, range', first, 'to', last resp, subs = s.xhdr('subject', first + '-' + last) for id, sub in subs: print id with open(os.path.join(name, str(id)), 'wb') as fp: pprint.pprint(s.article(id), stream=fp)
def add_new_jobs(group_name): db = Connection().usenet max_h = db.control.find({"group": group_name}).sort('end', DESCENDING).limit(1)[0]['end'] + 1 if not max_h: max_h = 0 news = NNTP('eu.news.astraweb.com', user='******', password='******') group = dict_group(news, group_name) news.quit() i = max(group['first'], max_h) if max_h > group['last']: return while(i+100000 < group['last']): db.control.insert({'init': i, 'end': i+99999, 'done':False, "group": group_name}) i += 100000 db.control.insert({'init': i, 'end': group['last'], 'done':False, "group": group_name})
def __init__(self, group, server): self.conn = NNTP(server) resp, count, first, last, name = self.conn.group(group) self.group = group self.server = server self.first = int(first) self.last = int(last)
def getItems(self): start = localtime(time()-self.window*day) date = strftime('%y%m%d',start) hour = strftime('%H%M%S',start) server = NNTP(self.servername) ids = server.newnews(self.group,date,hour)[1] for id in ids: lines = server.article(id)[3] message = message_from_string('\n'.join(lines)) title = memssage['subject'] body = message.get_payload() if message.is_multipart(): body = body[0] yield NewsItem(title,body) server.quit()
class NewsGrep: def __init__(self,server,username,password): self.server = NNTP(server, 119,username,password) def __del__(self): pass def __str__(self): pass def list(self): resp, groups = self.server.list() for group, last, first, flag in groups: print group def ls(self,group_name): resp, count, first, last, name = self.server.group(group_name) print 'Group', name, 'has', count, 'articles, range', first, 'to', last resp, subs = self.server.xhdr('subject', first + '-' + last) for id, sub in subs[-10:]: print id, sub
def main(): tree = {} # Check that the output directory exists checkopdir(pagedir) try: print('Connecting to %s...' % newshost) if sys.version[0] == '0': s = NNTP.init(newshost) else: s = NNTP(newshost) connected = True except (nntplib.error_temp, nntplib.error_perm) as x: print('Error connecting to host:', x) print('I\'ll try to use just the local list.') connected = False # If -a is specified, read the full list of groups from server if connected and len(sys.argv) > 1 and sys.argv[1] == '-a': groups = getallgroups(s) # Otherwise just read the local file and then add # groups created since local file last modified. else: (tree, treedate) = readlocallist(treefile) if connected: groups = getnewgroups(s, treedate) if connected: addtotree(tree, groups) writelocallist(treefile,tree) # Read group descriptions readdesc(descfile) print('Creating pages...') createpage(rootpage, tree, '') print('Done')
class NewsGrep: def __init__(self, server, username, password): self.server = NNTP(server, 119, username, password) def __del__(self): pass def __str__(self): pass def list(self): resp, groups = self.server.list() for group, last, first, flag in groups: print group def ls(self, group_name): resp, count, first, last, name = self.server.group(group_name) print 'Group', name, 'has', count, 'articles, range', first, 'to', last resp, subs = self.server.xhdr('subject', first + '-' + last) for id, sub in subs[-10:]: print id, sub
def main(): s = NNTP(settings.nntp_hostname, settings.nntp_port) resp, groups = s.list() # check all of the groups, just in case for group_name, last, first, flag in groups: resp, count, first, last, name = s.group(group_name) print "\nGroup", group_name, 'has', count, 'articles, range', first, 'to', last resp, subs = s.xhdr('subject', first + '-' + last) for id, sub in subs[-10:]: print id, sub s.quit()
def get_items(self): #新闻生成器 server = NNTP(self.servername) resp, count, first, last, name = server.group(self.group) #新闻组信息列表 start = last - self.howmany + 1 resp, overviews = server.over((start, last)) for id, over in overviews: title = decode_header(over['subject']) resp, info = server.body(id) body = '\n'.join(line.decode() for line in info.lines) + '\n\n' yield NewsItem(title, body) server.quit()
def get_items(self): server = NNTP(self.servername) resp, count, first, last, name = server.group(self.group) start = last - self.howmany + 1 resp, overviews = server.over((start, last)) for id, over in overviews: title = decode_header(over["subject"]) resp, info = server.body(id) body = "\n".join(line.decode("latin") for line in info.lines) + "\n\n" yield NewsItem(title, body) server.quit()
def start(self): port = self.port if not port: port = NNTP_PORT self.nntp = NNTP(self.server, port, self.user, self.pw, readermode=1) # only look for messages that appear after we start. Usenet is big. if not self.last: # only do this the first time for g in self.groups: resp, count, first, last, name = self.nntp.group(g) self.last[g] = int(last) if self.debug: print "last[%s]: %d" % (g, self.last[g]) self.timeout = gtk.timeout_add(self.pollInterval*1000, self.doTimeout)
def get_items(self): server = NNTP(self.servername) _, count, first, last, name = server.group(self.group) start = last - self.how_many + 1 _, overviews = server.over((start, last)) for ID, over in overviews: title = decode_header(over['subject']) _, info = server.body(ID) body = '\n'.join(line.decode('latin') for line in info.lines) yield NewsItem(title, body) server.quit()
def whatsnew(date_last_check, group="*", **server): fill = dict() if "user" in server: fill["user"] = server["user"] if "password" in server: fill["password"] = server["password"] if "host" in server: fill["host"] = server["host"] if "port" in server: fill["port"] = server["port"] with NNTP(**fill) as srv: response, groups = srv.newgroups(date_last_check) for g in groups: yield g response, articles = srv.newnews(group, date_last_check) for msg_id in articles: response, info = srv.article(msg_id) yield email.message_from_bytes(b"\r\n".join(info.lines))
def get_items(self): server = NNTP(self.servername) #服务器响应、新闻组包含的消息数、第一条和最后一条消息编号、新闻组名称 resp, count, first, last, name = server.group(self.group) #确定要获取的文章编号区间的起始位置 start = last - self.howmany + 1 resp, overviews = server.over((start, last)) for id, over in overviews: title = decode_header(over['subject']) resp, info = server.body(id) body = '\n'.join(line.decode('latin') for line in info.lines) + '\n\n' yield NewsItem(title, body) server.quit()
def main(): tree = {} # Check that the output directory exists checkopdir(pagedir) try: print 'Connecting to %s...' % newshost if sys.version[0] == '0': s = NNTP.init(newshost) else: s = NNTP(newshost) connected = True except (nntplib.error_temp, nntplib.error_perm), x: print 'Error connecting to host:', x print 'I\'ll try to use just the local list.' connected = False
def get_items(self): server = NNTP(self.servername) resp, count, first, last, name = server.group(self.group) start = last - self.howmany + 1 resp, overviews = server.over((start, last)) for id, over in overviews: title = decode_header(over['subject']) resp, info = server.body(id) body = '\n'.join(line.decode('latin') for line in info.lines) + '\n\n' yield NewsItem(title, body) server.quit()
def getItems(self): server = NNTP(self.servername) (resp, count, first, last, name) = server.group(self.group) (resp, subs) = server.xhdr('subject', (str(first) + '-' + str(last))) for subject in subs[-10:]: title = subject[1] (reply, (num, id, list)) = server.body(subject[0]) # list是一个列表,但是是bytes编码的,需要把每一个元素都解码成string body = [] #print(list) for l in list: body.append(l.decode('gbk')) # 注意,这里用utf-8解码会出现解码错误 #print(body) body = ''.join(body) yield NewsItem(title, body) server.quit()
def __init__(self, article_cache_size=300, cache_file=None): """Initialize local variables""" # Connect to news.gmane.org self.nntp = NNTP('news.gmane.org') # Setting the group returns information, which right now we ignore self.nntp.group('gmane.comp.internationalization.dansk') # Keep a local cache in an OrderedDict, transferred across session # in a pickled version in a file self.article_cache_size = article_cache_size self.cache_file = cache_file if cache_file and path.isfile(cache_file): with open(cache_file, 'rb') as file_: self.article_cache = pickle.load(file_) logging.info('Loaded %i items from file cache', len(self.article_cache)) else: self.article_cache = OrderedDict()
def getItems(self): server = NNTP(self.servername) (resp, count, frist, last, name) = server.group(self.group) (resp, subs) = server.xhdr('subject', (str(frist) + '-' + (last))) for subject in subs[-10:]: title = subject[1] (reply, num, id, list) = server.body(subject[0]) body = ''.join(list) #print(num) #186919 #print(title) #Re: Find out which module a class came from #print(''.join(list))#prano wrote:> But for merely ordinary obfuscation caused by poor... yield NewsItem(title, body) server.quit()
def main(): global desc tree={} # Check that the output directory exists checkopdir(pagedir) try: print('Connecting to '+newshost+'...') if sys.version[0] == '0': s = NNTP.init(newshost) else: s = NNTP(newshost) connected = 1 except (nntplib.error_temp, nntplib.error_perm) as x: print('Error connecting to host:', x) print('I\'ll try to use just the local list.') connected = 0 # If -a is specified, read the full list of groups from server if connected and len(sys.argv) > 1 and sys.argv[1] == '-a': groups = getallgroups(s) # Otherwise just read the local file and then add # groups created since local file last modified. else: (tree, treedate) = readlocallist(treefile) if connected: groups = getnewgroups(s, treedate) if connected: addtotree(tree, groups) writelocallist(treefile,tree) # Read group descriptions readdesc(descfile) print('Creating pages...') createpage(rootpage, tree, '') print('Done')
def __init__(self): try: self.news = NNTP(NEWS_SERVER, NNTP_PORT, NNTP_USERNAME, NNTP_PASSWORD) except NNTPTemporaryError as e: raise SpotError('NNTP', e) except socket.error as e: raise SpotError('Connection', e) self.conn = sqlite3.connect(NEWS_SERVER + '.db') self.conn.row_factory = sqlite3.Row self.cur = self.conn.cursor() self.cur.executescript('''\ PRAGMA synchronous = OFF; PRAGMA journal_mode = MEMORY; PRAGMA temp_store = MEMORY; PRAGMA count_changes = OFF; ''')
def get_items(self): for servername in KNOWN_NNTP_SERVERS: try: server = NNTP(servername) resp, count, first, last, name = server.group(self.group) start = last - self.howmany + 1 resp, overviews = server.over((start, last)) for id, over in overviews: title = decode_header(over['subject']) resp, info = server.body(id) body = '\n'.join( line.decode('latin1') for line in info.lines) + '\n\n' yield NewsItem(title, body, "NNTP NewsGroup " + self.group) server.quit() break except: continue return []
def getItems(self): # yesterday = localtime(time()-self.window*day) # date = strftime('%y%m%d',yesterday) # time = strftime('%H%M%S',yesterday) # create a nntp server s = NNTP(self.servername) resp,count,first,last,name = s.group(self.groupname) resp,overviews = s.over((last-1,last)) for num,over in overviews: title = over.get('subject') resp,body = s.body(num) # create a generator to iterate news if title and body: yield NewsItem(title,body) s.quit()
def getItems(self): start = localtime(time() - self.window * day) date = strftime('%y%m%d', start) hour = strftime('%H%M%S', start) server = NNTP(self.servername) ids = server.newnews(self.group, date, hour)[1] for id in ids: lines = server.article(id)[3] message = message_from_string('\n'.join(lines)) title = message['subject'] body = message.get_payload() if message.is_multipart(): body = body[0] yield NewsItem(title, body) server.quit()
def connect(self): address = net.format_addr((self.hostname, self.port)) self.log.write("Connecting to {}\n".format(address)) if self.port is None: port = () else: port = (self.port,) self.connect_time = time.monotonic() self.nntp = NNTP(self.hostname, *port, **self.timeout) with ExitStack() as cleanup: cleanup.push(self) if self.debuglevel is not None: self.nntp.set_debuglevel(self.debuglevel) self.log.write("{}\n".format(self.nntp.getwelcome())) if self.username is not None: self.log.write("Logging in as {}\n".format(self.username)) with self.handle_abort(): self.nntp.login(self.username, self.password) self.log.write("Logged in\n") cleanup.pop_all()
def fetch(self): """ Fetches all the messages for a given news group uri and return Fetched staus depending on the success and faliure of the task """ try: #eg. self.currenturi = nntp://msnews.microsoft.com/microsoft.public.exchange.setup #nntp_server = 'msnews.microsoft.com' #nntp_group = 'microsoft.public.exchange.setup' self.genre = 'review' try: nntp_server = urlparse(self.currenturi)[1] except: log.exception(self.log_msg("Exception occured while connecting to NNTP server %s"%self.currenturi)) return False nntp_group = urlparse(self.currenturi)[2][1:] self.server = NNTP(nntp_server) try: self.__updateParentSessionInfo() resp, count, first, last, name = self.server.group(nntp_group) last_id = int(last) first_id = self.__getMaxCrawledId(last_id)+1 log.debug("first_id is %d:"%first_id) log.debug("last_id is %d:"%last_id) if last_id >= first_id: resp, items = self.server.xover(str(first_id), str(last_id)) log.debug(self.log_msg("length of items:%s"%str(len(items)))) for self.id, self.subject, self.author, self.date, self.message_id,\ self.references, size, lines in items: self.__getMessages(self.task.instance_data['uri']) self.server.quit() return True except: log.exception(self.log_msg("Exception occured in fetch()")) self.server.quit() return False except Exception,e: log.exception(self.log_msg("Exception occured in fetch()")) return False
def getItems(self): server = NNTP(self.servername) _, count, first, last, name = server.group(self.group) _, subs = server.xhdr( 'subject', (str(first) + '-' + str(last))) for sub in subs[:10]: # default last ten id = sub[0] lines = server.article(id)[3] message = message_from_string('\n'.join(lines)) title = message['subject'] body = message.get_payload() if message.is_multipart(): body = body[0] yield NewsItem(title, body) server.quit()
def getItems(self): """ 书中原例getItems()方法 返回 nntplib.NNTPTemporaryError: 480 NEWNEWS command disabled by administrator #480管理员禁用NEWNEWS命令 def getItems(self): start = localtime(time() - self.window*day) date = strftime('%y%m%d', start) hour = strftime('%H%M%S', start) server = NNTP(self.servername) ids = server.newnews(self.group, date, hour)[1] for id in ids: lines = serverarticle(id)[3] message = message_from_string('\n'.join(lines)) title = message['subject'] body = message.get_payload() if message.is_multipat(): body = body[0] yield NewsItem(title, body) server.quit() """ server = NNTP(self.servername) (resp, count, frist, last, name) = server.group(self.group) (resp, subs) = server.xhdr('subject', (str(frist) + '-' + (last))) for subject in subs[-10:]: title = subject[1] (reply, num, id, list) = server.body(subject[0]) body = ''.join(list) #print(num) #186919 #print(title) #Re: Find out which module a class came from #print(''.join(list))#prano wrote:> But for merely ordinary obfuscation caused by poor... yield NewsItem(title, body) server.quit()
def getItems(self): # 新闻生成器 yesterday = date.today() - timedelta(days=self.window) # 计算新闻获取的起始时间 server = NNTP(self.server_name) # 创建服务器连接对象 ids = server.newnews(self.group, yesterday)[1] # 获取新闻id列表 count = 0 # 创建计数变量 for id in ids: # 循环获取新闻id count += 1 # 计数递增 if count <= 10: # 如果计数小于10 article = server.article(id)[1][2] # 获取指定id的新闻文章 lines = [] # 创建每行新闻内容的列表 for line in article: # 从新闻文章中读取每一行内容 lines.append(line.decode()) # 将每行新闻内容解码,添加到新闻内容列表。 message = message_from_string('\n'.join(lines)) # 合并新闻列表内容为字符串并转为消息对象 title = message['subject'].replace('\n', '') # 从消息对象中获取标题 body = message.get_payload() # 从消息对象中获取到新闻主体内容 if message.is_multipart(): # 如果消息对象包含多个部分 body = body[0] # 获取到的内容中第1个部分获取新闻主体内容 yield NewsItem(title, body) # 生成1个新闻内容对象 else: # 如果超出10条内容 break # 跳出循环 server.quit() # 关闭连接
#!/usr/local/bin/python3 from nntplib import NNTP server = NNTP('news.aioe.org') resp, count, first, last, name = server.group('sci.logic') print('Group', name, 'has', count, 'articles, range', first, 'to', last) resp, subs = server.xhdr('subject', str(first) + '-' + str(last)) for id, text in subs[-10:]: print(id, text) server.quit()
listonly = 0 showhdrs = ['From', 'Subject', 'Date', 'Newsgroups', 'Lines'] try: import sys servername, groupname, showcount = sys.argv[1:] showcount = int(showcount) except: servername = 'news.rmi.net' groupname = 'comp.lang.python' # cmd line args or defaults showcount = 10 # show last showcount posts # connect to nntp server print 'Connecting to', servername, 'for', groupname from nntplib import NNTP connection = NNTP(servername) (reply, count, first, last, name) = connection.group(groupname) print '%s has %s articles: %s-%s' % (name, count, first, last) # get request headers only fetchfrom = str(int(last) - (showcount-1)) (reply, subjects) = connection.xhdr('subject', (fetchfrom + '-' + last)) # show headers, get message hdr+body for (id, subj) in subjects: # [-showcount:] if fetch all hdrs print 'Article %s [%s]' % (id, subj) if not listonly and raw_input('=> Display?') in ['y', 'Y']: reply, num, tid, list = connection.head(id) for line in list: for prefix in showhdrs: if line[:len(prefix)] == prefix:
class Archive(object): @staticmethod def is_diff(body): return bool([line for line in body if line.startswith("diff ")]) def __init__(self, group, server): self.conn = NNTP(server) resp, count, first, last, name = self.conn.group(group) self.group = group self.server = server self.first = int(first) self.last = int(last) def get_number_from_user(self, msg_id): """ Convert something the user might input into a message id. These are: # An NNTP message number # A gmane link that includes the NNTP message number # The original Message-Id header of the message. NOTE: gmane's doesn't include the message number in STAT requests that involve only the Message-Id (hence the convolution of getting all the headers). """ msg_id = re.sub(r".*gmane.org/gmane.comp.version-control.git/([0-9]+).*", r"\1", str(msg_id)) _, n, id, result = self.conn.head(msg_id) for header in result: m = re.match(r"Xref: .*:([0-9]+)\s*$", header, re.I) if m: return int(m.group(1)) else: raise FatalError("No (or bad) Xref header for message '%s'" % msg_id) def get_patch_series(self, user_input, search_limit=100): """ Given an NNTP message number or a Message-Id header return an mbox containing the patches introduced by the author of that message. This handles the case where the threading is right *and* the patches are numbered in a simple scheme: [PATCH] this patch has no replies and stands on its own [PATCH 0/2] this is an introduction to the series |- [PATCH 1/2] the first commit |- [PATCH 2/2] the second commit [PATCH 1/3] this is the first commit |- [PATCH 2/3] and this is the second |- [PATCH 3/3] and this is the third TODO: it would be nice to make the search more efficient, we can use the numbers in [PATCH <foo>/<bar>] to stop early. """ start_id = self.get_number_from_user(user_input) messages = limit(self.messages_starting_from(start_id), search_limit) try: thread = Thread(messages.next()) except StopIteration: raise FatalError("No message at id '%s' using XOVER") n_since_last = 0 for message in messages: if n_since_last > 5: break elif thread.should_include(message): n_since_last = 0 thread.append(message) else: n_since_last += 1 else: raise FatalError('did not find end of series within %s messages', search_limit) for message in self.xover(start_id - 5, start_id -1): if thread.should_include(message): thread.append(message) return self.mboxify(thread) def mboxify(self, thread): """ Convert a thread into an mbox for application via git-am. """ lines = [] for message in thread.in_order(): _, number, msg_id, body = self.conn.body(str(message.number)) # git-am doesn't like empty patches very much, and the 0/X'th patch is # often not a patch, we skip it here. (TODO, warn the user about this) if re.search(r" 0+/[0-9]+", message.subject) and not self.is_diff(body): continue poster = parseaddr(message.poster)[0] date = ctime(mktime(parsedate(message.date))) lines.append("From %s %s" % (poster, date)) lines.append("From: %s" % message.poster) lines.append("Subject: %s" % message.subject) lines.append("Date: %s" % message.date) lines.append("Message-Id: %s" % message.msg_id) lines.append("Xref: %s %s:%s" % (self.server, self.group, message.number)) lines.append("References: %s" % "\n\t".join(message.references)) lines.append("") lines += body lines.append("") return "\n".join(lines) def messages_starting_from(self, start_id): """ Generate all message headers starting from the given id and working upwards. """ while start_id < self.last: next_id = min(start_id + 20, self.last) for message in self.xover(start_id, next_id): yield message start_id = next_id + 1 def xover(self, begin, end): """ Get the headers for the messages with numbers between begin and end. """ if begin == end: return [] _, result = self.conn.xover(str(min(begin, end)), str(max(begin, end))) result = [Message(int(number), subject, poster, date, msg_id, references) for (number, subject, poster, date, msg_id, references, size, lines) in result] return sorted(result, key=lambda x: x.number)
# -*-coding:gbk-*- from nntplib import NNTP server = NNTP('news.foo.bar') print server.group('comp.lang.python.announce')[1]
formatter = logging.Formatter('%(name)-12s: %(levelname)-8s %(message)s') console.setFormatter(formatter) logging.getLogger('').addHandler(console) day = 24 * 60 * 60 yesterday = localtime(time() - day) date = strftime('%y%m%d', yesterday) hour = strftime('%H%M%S', yesterday) servername = 'news.aioe.org' group = 'comp.lang.python' server = NNTP(servername) ids = server.newnews(group, date, hour)[1] logging.info('this is ids') logging.info(ids) for id in ids: print 'this is id', id head = server.head(id)[3] for line in head: if line.lower().startswith('subject:'): subject = line[9:] break body = server.body(id)[3]
import sqlite3 from nntplib import NNTP group = 'alt.binaries.movies.x264' chunk_size = 100000 sql = sqlite3.connect('usenet.db') print "About to start dumping articles from " + group serv = NNTP('news.astraweb.com', 119, 'arealmuto', 'stock1114') resp = serv.group(group) count = int(resp[1]) first = int(resp[2]) last = int(resp[3]) print "There are " + str(count) + " articles to get" print "First: " + str(first) print "Last: " + str(last) print "Using chunks size of: " + str(chunk_size) print "It should take " + str(count/chunk_size) + " requests to finish" id = int(first) i = 0 master_list = [] while id < last: print str(i) +": Getting id's " + str(id) + " - " + str(id + chunk_size) resp, list = serv.xover(str(id), str(id+ chunk_size)) print "Done fetching" print "Adding to master list" for line in list:
from codeTransfer import * import textwrap import re # print strftime('%y%m%d') # start = localtime(time()-60*60*24*365) # print strftime('%y%m%d',start) class SimpleWebSource: def __init__(self, url, titlePattern, bodyPattern): self.url = url self.titlePattern = re.compile(titlePattern) self.bodyPattern = re.compile(bodyPattern) def getItems(self): text = urlopen(self.url).read() titles = self.titlePattern.findall(text) bodies = self.bodyPattern.findall(text) for title in titles: print title[1].decode('utf-8') print bodies bbc_url = 'http://news.sina.com.cn' bbc_title = r'<a target="_blank" href="(.*?)">(.*?)</a>' bbc_body = r'(?s)</a>\s*<br />\s*(.*?)\s*<' # bbc = SimpleWebSource(bbc_url,bbc_title,bbc_body) # bbc.getItems() server = NNTP('129.69.1.59') server.group('camp.lang.python')[0]
#! /usr/bin/python from sys import stdin from nntplib import NNTP from os import environ s = NNTP(environ["NNTPSERVER"]) s.post(stdin) s.quit()
#!/usr/bin/env python # -*- coding: UTF-8 *-* from nntplib import NNTP n = NNTP('your.nntp.server') rsp, ct, fst, lst, grp = n.group('comp.lang.python') rsp, anum, mid, data = n.article('110457') for eachLine in data: print eachLine From: "Alex Martelli" <alex@...> Subject: Re: Rounding Question Date: Wed, 21 Feb 2001 17:05:36 +0100 "Remco Gerlich" <remco@...> wrote: Jacob Kaplan-Moss <jacob@...> wrote in comp.lang.python: So I've got a number between 40 and 130 that I want to round up to the nearest 10. That is: 40 --> 40, 41 --> 50, ..., 49 --> 50, 50 --> 50, 51 --> 60 Rounding like this is the same as adding 5 to the number and then rounding down. Rounding down is substracting the remainder if you were to divide by 10, for which we use the % operator in Python. This will work if you use +9 in each case rather than +5 (note that he doesn't really want rounding -- he wants 41 to 'round' to 50, for ex). Alex >>> n.quit() '205 closing connection - goodbye!'
from nntplib import NNTP servername = 'news.foo.bar' group = 'comp.lang.python.announce' server = NNTP(servername) howmany = 10 resp, count, first, last, name = server.group(group) start = last - howmany + 1 resp, overviews = server.over((start, last)) for id, over in overviews: subject = over['subject'] resp, info = server.body(id) print(subject) print('-' * len(subject)) for line in info.lines: print(line.decode('latin1')) print() server.quit()
#__author: ZhengNengjin #__date: 2018/10/25 from nntplib import NNTP n = NNTP('your.nntp.server') rsp, ct, fst, lst, grp = n.group('comp,lang.python') rsp, anum, mid, data = n.article('110457') for eachLine in data: print(eachLine) n.quit()
class DanskGruppenArchive(object): """Class that provides an interface to Dansk-gruppens emails archive on gmane """ def __init__(self, article_cache_size=300, cache_file=None): """Initialize local variables""" # Connect to news.gmane.org self.nntp = NNTP('news.gmane.org') # Setting the group returns information, which right now we ignore self.nntp.group('gmane.comp.internationalization.dansk') # Keep a local cache in an OrderedDict, transferred across session # in a pickled version in a file self.article_cache_size = article_cache_size self.cache_file = cache_file if cache_file and path.isfile(cache_file): with open(cache_file, 'rb') as file_: self.article_cache = pickle.load(file_) logging.info('Loaded %i items from file cache', len(self.article_cache)) else: self.article_cache = OrderedDict() def close(self): """Quit the NNTP session and save the cache""" self.nntp.quit() if self.cache_file: with open(self.cache_file, 'wb') as file_: pickle.dump(self.article_cache, file_) logging.info('Wrote %i items to cache file', len(self.article_cache)) @property def last(self): """Return the last NNTP ID as an int""" return self.nntp.group('gmane.comp.internationalization.dansk')[3] def _get_article(self, message_id): """Get an article (cached) Args: message_id (int): The NNTP ID of the message Returns: list: List of byte strings in the message """ # Clear excess cache if len(self.article_cache) > self.article_cache_size: self.article_cache.popitem(last=False) # Check if article is in cache and if not, put it there if message_id not in self.article_cache: # nntp.article() returns: response, information # pylint: disable=unbalanced-tuple-unpacking _, info = self.nntp.article(message_id) self.article_cache[message_id] = info return self.article_cache[message_id] @staticmethod def _article_to_email(article): """Convert a raw article to an email object Args: article (namedtuple): An article named tuple as returned by NNTP Returns: email.message: An email message object """ # article lines are a list of byte strings decoded_lines = [line.decode('ascii') for line in article.lines] article_string = '\n'.join(decoded_lines) # Make an email object return email.message_from_string(article_string) def get_subject(self, message_id): """Get the subject of an message Args: message_id (int): The NNTP ID of the the message Returns: str: The subject of the article """ article = self._get_article(message_id) mail = self._article_to_email(article) # The subject may be encoded by NNTP, so decode it return decode_header(mail['Subject']) def get_body(self, message_id): """Get the body of a message Args: message_id (int): The NNTP ID of the the message Returns: str: The body of the article as a str or None if no body could be found or succesfully decoded """ article = self._get_article(message_id) mail = self._article_to_email(article) # Walk parts of the email and look for text/plain content type for part in mail.walk(): if part.get_content_type() == 'text/plain': body = part.get_payload(decode=True) # Find the text encoding from lines like: # text/plain; charset=UTF-8 # text/plain; charset=utf-8; format=flowed # Encoding sometimes has "" around it, decode is OK with that for type_part in part['Content-Type'].split(';'): if type_part.strip().startswith('charset='): encoding = type_part.replace('charset=', '') break else: message = 'Looking for the character encoding in the '\ 'string "%s" went wrong' logging.warning(message, part['Content-Type']) return None # Decode and return the body try: body = body.decode(encoding) except LookupError: message = 'Do not know how to handle a body with '\ 'charset: %s' logging.warning(message, encoding) return None return body def get_attachment(self, message_id, filename): """Get attachment by filename Args: message_id (int): The NNTP ID of the the message filename (str): The filename for the attachment Returns: bytes: The binary content of the attachment """ return self.get_attachments(message_id).get(filename) def get_attachments(self, message_id): """Get attachments Args: message_id (int): The NNTP ID of the the message Returns: dict: Dict with attachments where keys are filenames and values are their binary content """ article = self._get_article(message_id) mail = self._article_to_email(article) attachments = {} # Walk parts of the email and look for application/octet-stream # content type for part in mail.walk(): content_disp = part['Content-Disposition'] if not (content_disp and content_disp.startswith('attachment')): continue # Get the filename from a line like: Content-Disposition: # attachment; filename="hitori.master.da.podiff" filename = None for disp_part in content_disp.split(';'): if disp_part.strip().startswith('filename='): filename = disp_part.strip().replace('filename=', '') # Strip " from filename filename = filename.strip('"') if filename is None: message = 'Unable to extract filename from '\ 'Content-Disposition: %s' logging.warning(message, part['Content-Disposition']) raise Exception('Unable to extract filename') attachments[filename] = part.get_payload(decode=True) return attachments
#date: 06-26-2018 from nntplib import NNTP import nntplib from time import strftime, time , localtime from datetime import datetime, timedelta oneDay = 24 * 60 * 60 yesterday = datetime.now() - timedelta(days=1) #date = strftime('%y%m%d', yesterday) #hour = strftime('%H%M%S', yesterday) servername = 'news.gmane.org' groupname = 'gmane.comp.python.committers' server = NNTP(servername) resp, count, first, last, name = server.group(groupname) resp, overviews = server.over((last -9, last)) for id, over in overviews: print(id, nntplib.decode_header(over['subject'])) ''' ids = server.newnews(group, yesterday)[1] for id in ids: head = server.head(id)[3] for line in head: if line.lower().startswith('subject:'): subject = line[9:] break body = server.body(id)[3]
#!/usr/bin/python from nntplib import NNTP from time import time, localtime, strftime day = 24 * 60 * 60 yesterday = localtime(time() - day) date = strftime('%y%m%d', yesterday) t = strftime('%H%M%S', yesterday) s = NNTP('web.aioe.org') g = 'comp.lang.python.announce' ids = s.newnews(g, date, t)[1] for id in ids: head = s.head(id)[3] for line in head: if line.lower().startswith('subject:'): subject = line[9:] break body = s.body(id)[3] print subject print '-' * len(subject) print '\n'.join(body) s.quit()
#/usr/bin/python # *-*coding:utf-8 *-* #python NNTP # group组返回响应信息 from nntplib import NNTP with NNTP('news.gmane.org') as n: print(n.group('gmane.comp.python.committers')) server = NNTP('web.aioe.org') print(server.group('comp.lang.python.announce')[0]) #('211 4419 1 4419 gmane.comp.python.committers', 4419, 1, 4419, 'gmane.comp.python.committers') #211 100 3923 4023 comp.lang.python.announce ''' ('211 4371 1 4371 gmane.comp.python.committers', 4371, 1, 4371, 'gmane.comp.python.committers') 411 开头意味着服务器没有这个组 nntplib.NNTPTemporaryError: 411 No such group comp.lang.python.announce 211开头 基本上意味着服务器拥有你所请求的组 ('211 98 3911 4008 comp.lang.python.announce', 98, 3911, 4008, 'comp.lang.python.announce') 还有可能得到一个带有类似错误信息的的异常 如果引发了异常 可能是服务器的名字写错了 另外一种可能是在创建服务器对象和调用group方法之间 '超时了', 服务器允许用户保持连接的时间很短(比如10秒) 如果输入速度不够快 把代码放到脚本里, 或着将服务器对象的创建和方法的调用放在同一行内 (以分号隔开)
############################################################################ listonly = 0 showhdrs = ['From', 'Subject', 'Date', 'Newsgroups', 'Lines'] try: import sys servername, groupname, showcount = sys.argv[1:] showcount = int(showcount) except: servername = 'news.gmane.org' groupname = 'gmane.comp.python.general' # cmd line args or defaults showcount = 10 # show last showcount posts # connect to nntp server print 'Connecting to', servername, 'for', groupname from nntplib import NNTP connection = NNTP(servername) (reply, count, first, last, name) = connection.group(groupname) print '%s has %s articles: %s-%s' % (name, count, first, last) # get request headers only fetchfrom = str(int(last) - (showcount-1)) (reply, subjects) = connection.xhdr('subject', (fetchfrom + '-' + last)) # show headers, get message hdr+body for (id, subj) in subjects: # [-showcount:] if fetch all hdrs print 'Article %s [%s]' % (id, subj) if not listonly and raw_input('=> Display?') in ['y', 'Y']: reply, num, tid, list = connection.head(id) for line in list: for prefix in showhdrs: if line[:len(prefix)] == prefix:
import nntplib from nntplib import NNTP s = NNTP('news.gmane.org') resp, count, first, last, name = s.group('gmane.comp.python.committers') print ('Group', name, 'has', count, 'articles, range', first, 'to', last) resp, subs = s.xhdr('subject',first) for id, sub in subs[-10:]: print (id, sub) try : for id, sub in subs[-10:]: print (id, sub) except TypeError as e: print(e) print(s.quit()) # NNTP.
self._file_str.write(str) def __str__(self): return self._file_str.getvalue() week = 7 * 24 * 60 * 60 # Number of seconds in one day start = localtime(time() - week) date = strftime('%y%m%d', start) time = strftime('%H%M%S', start) full_date = date + time servername = 'news.aioe.org' group = 'comp.lang.python.announce' server = NNTP(servername) ids = server.newnews(group, datetime.datetime.strptime(full_date, '%y%m%d%H%M%S'))[1] content = StringBuilder() content.Append(''' <html> <head> <title>Week's News - Python Announce</title> </head> <body> <h1>Week's News - Python Announce</h1> ''')
args = sys.argv if len(args) == 2: print "Resetting lastread and seen.." resetLastread() exit seen = [] newdate = None print "Reading date and seen files" date,seen = loadLastread() for serverentry in servers: server = serverentry[0] newsgroups = serverentry[2] print "Connecting to: " + server s = NNTP(server) for newsgroup in newsgroups: feedGroup(server, newsgroup, date, seen) serverdate = s.date() date = serverdate[1] time = serverdate[2] year = 2000 + int(serverdate[1][0:2]) ## So I got a Y3K problem here. Deal. month = int(serverdate[1][2:4]) day = int(serverdate[1][4:]) hour = int(serverdate[2][0:2]) minute = int(serverdate[2][2:4]) second = int(serverdate[2][4:]) newdate = datetime.datetime(year,month,day,hour,minute,second) print "Server time is " + str(newdate)
from nntplib import NNTP #server = NNTP('news.easynews.com') #server = NNTP('news.giganews.com') #server = NNTP('news.mozilla.org') server = NNTP('news.kornet.net') print server.group('comp.lang.python.announce')[0] """ | group(self, name) | Process a GROUP command. Argument: | - group: the group name | Returns: | - resp: server response if successful | - count: number of articles (string) | - first: first article number (string) | - last: last article number (string) | - name: the group name """ group = server.group('han.test') print repr(group) first = group[2] last = group[3] print "first ", first print "last ", last i = 0 for id in range(int(first), int(last)): i += 1 print(server.article(str(id)))
#! /usr/bin/env python
# -*- coding:utf-8 -*- from nntplib import NNTP servername = 'web.aioe.org' group = 'comp.lang.python.announce' server = NNTP(servername) howmany = 10 resp, count, first, last, name = server.group(group) start = last - howmany + 1 resp, overviews = server.over((start, last)) for id, over in overviews: subject = over['subject'] resp, info = server.body(id) print('subject:', subject) print('-' * len(subject)) for line in info.lines: print(line.decode('latin1')) print() server.quit()
from nntplib import NNTP server = NNTP('news2.neva.ru') print server.group('alt.sex.telephone')
class NewsWatcher(MessageWatcher): def __init__(self, server, groups, user=None, pw=None, port=None, tag=None): MessageWatcher.__init__(self) self.server = server self.groups = groups self.nntp = None # the NNTP connection object self.user = user self.pw = pw self.port = port self.tag = tag self.last = {} self.timeout = None self.pollInterval = 60 self.debug = 0 def __repr__(self): return "<NewsWatcher %s:%s (%s)>" % (self.server, self.port, ",".join(self.groups)) def __getstate__(self): d = MessageWatcher.__getstate__(self) d['nntp'] = None # just in case return d def start(self): port = self.port if not port: port = NNTP_PORT self.nntp = NNTP(self.server, port, self.user, self.pw, readermode=1) # only look for messages that appear after we start. Usenet is big. if not self.last: # only do this the first time for g in self.groups: resp, count, first, last, name = self.nntp.group(g) self.last[g] = int(last) if self.debug: print "last[%s]: %d" % (g, self.last[g]) self.timeout = gtk.timeout_add(self.pollInterval*1000, self.doTimeout) def stop(self): self.nntp.quit() self.nntp = None if self.timeout: gtk.timeout_remove(self.timeout) self.timeout = None def doTimeout(self): self.poll() return gtk.TRUE # keep going def poll(self): #print "polling", self for g in self.groups: resp, count, first, last, name = self.nntp.group(g) for num in range(self.last[g]+1, int(last)+1): try: resp, num, id, lines = self.nntp.article("%d" % num) except NNTPError: continue name = "%s:%d" % (g, int(num)) if self.debug: print "got", name if self.tag: if not filter(lambda line, tag=tag: line.find(tag) != -1, lines): continue self.parseMessage(name, name, time.time(), lines) self.last[g] = int(last)
def read_article(msg_id, **server): with NNTP(**server) as srv: response, info = srv.article(msg_id) return email.message_from_bytes(b"\r\n".join(info.lines), policy=email.policy.SMTPUTF8)
#!/usr/bin/python # nntptest from nntplib import NNTP import datetime import os import sys args = sys.argv if not len(args) == 2: print "usage nntpout.py <hostname>" else: print "Connecting to " + args[1] s = NNTP(args[1],user="******",password="******") print "Posting" rv = s.post(sys.stdin) print "Posted" print "Server said: " + rv
# ids = server.newnews(group, date,hour)[1] # 该方法有问题 # for id in ids: # print(id) # head = server.head(id)[3] # for line in head: # if line.lower().startswith('subject:'): # subjucet = line[9:] # break # body = server.body(id)[3] # # print(subjucet) # print('-' * len(subjucet)) # print('\n'.join(body)) servername = 'web.aioe.org' group = 'comp.lang.python.announce' server = NNTP(servername) (resp, count, first, last, name) = server.group(group) (resp, subs) = server.xhdr('subject', (str(first) + '-' + (last))) for subject in subs[-10:]: title = subject[1] (reply, num, id, list) = server.body(subject[0]) body = ''.join(list) print(num) #186919 print(title) #Re: Find out which module a class came from print( ''.join(list) ) #prano wrote:> But for merely ordinary obfuscation caused by poor... server.quit()