def getItems(self): start = localtime(time() - self.window * day) date = strftime('%y%m%d', start) hour = strftime('%H%M%S', start) full_date = date + time server = NNTP(self.servername) ids = server.newnews( self.group, datetime.datetime.strptime(full_date, '%y%m%d%H%M%S'))[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 whatsnew(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"] idx = _indexServer(**server) if idx in servers_lastcheck and servers_lastcheck[idx] is not None: date_last_check = servers_lastcheck[idx] else: date_last_check = datetime.now() if idx not in servers_lastseen: servers_lastseen[idx] = [] with NNTP(**fill) as srv: response, servers_lastcheck[idx] = srv.date() 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: if msg_id not in servers_lastseen[idx]: servers_lastseen[idx].append(msg_id) response, info = srv.article(msg_id) yield email.message_from_bytes(b"\r\n".join(info.lines)) # Clean huge lists if len(servers_lastseen[idx]) > 42: servers_lastseen[idx] = servers_lastseen[idx][23:]
def getItems(self): server = NNTP(self.servername) group = server.group(self.group) first = group[2] last = str(int(group[2]) + 10) #last = str(int(group[2]) + 2) for id in range(int(first), int(last)): lines = server.article(str(id))[3] message = message_from_string('\n'.join(lines)) title = message['subject'] #print(repr(dir(message))) print(message.get_content_type()) if 'text' in message.get_content_type(): body = message.get_payload() else: body = 'Body is not text' if 'ybegin' in message.get_payload(): body = 'hmm' if message.is_multipart(): #body = body[0] body = 'Multi part is not implemented yet' yield NewsItem(title, body) server.quit()
def gmane(self): try: gmane = NNTP("news.gmane.org") except: return self.gmane gmane.group(self.group) return gmane
def __init__(self, serverName, group, howmany): super().__init__() self.serverName = serverName self.type = Source.NNTP self.group = group self.howmany = howmany self.server = NNTP(self.serverName)
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 use_NNTP(): for server in NNTP_server_list: print server server = raw_input('Which NNTP_server do you want to go?\n') while True: if server not in NNTP_server_list: print 'invalid srever name,try again!' server = raw_input() else: break s = NNTP(server) print "s = NNTP('news.newsfan.net') is OK!" (resp, lst) = s.list() while True: for i, elem in enumerate(lst): print i, elem[0].decode('gbk') num = raw_input('Which group do you want to go?\n') try: rsp, ct, first, last, grp = s.group(lst[int(num)][0]) print "Article's range is:%s to %s." % (first, last) (resp, subs) = s.xhdr('subject', (str(first) + '-' + str(last))) except: print format_exc() print 'invalid input!try again!!!' sleep(3) continue for subject in subs: try: print subject[0], subject[1].decode('gbk') except: print subject[0], subject[1] while True: try: number = raw_input('Which article do you want to read?\n') if number == 'q': break f = open("NNTPfile", 'w') (reply, num, id, list) = s.body(str(number), f) f = open("NNTPfile", 'r') for eachLine in f: try: print eachLine.decode('gbk'), except: print eachLine f.close() print 'Press any to continue...(Press q to return...)' if raw_input() == 'q': break except: print format_exc() print 'invalid input!try again!!!' sleep(3) s.quit() return
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("latin") for line in info.lines) + "\n\n" yield NewsItem(title, body) server.quit()
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 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_func(year): nntp = NNTP('news.alaska-software.com') print(year) for month in range(1, 12): for day in range(1, 28): date = datetime.date(year, month, day) groups = nntp.newgroups(date)[1] for group in groups: try: news = nntp.newnews(group, date) except nntplib.NNTPPermanentError: continue else: print(news)
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 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 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 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 __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 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 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 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 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 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() # 关闭连接
# -*- 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()
#!/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()
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)))
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:
#!/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()
#__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()
#/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秒) 如果输入速度不够快 把代码放到脚本里, 或着将服务器对象的创建和方法的调用放在同一行内 (以分号隔开)