jump to content

A small script that I wrote for downloading attachments from a newsgroup. This is not the best way to do it, but it can be used to start one. Ideal would be where there is check for already downloaded attachments, threading for scanning multiple newsgroups and so. Can be quite useful for downloading pictures or mp3 files :-).

#
# simple script to download attachments from a newsgroup
# vsbabu-removethis@hotmail.com
#
import nntplib
import string
import uu

#change the isp, userid and password
s = nntplib.NNTP('news.myisp.net',119,'userid','password')

#change the newsgroup you want to scan
resp, count, first, last, name = s.group('some.newsgroup')

#this will scan only the last 20 messages. change if you want more
first = str(int(last)-20)
resp, subs = s.xhdr('subject', first + '-' + last)
for id, sub in subs:
  #uncomment the if condition if you want to scan for some string
  #if string.find(sub,"FILTERSTRING")>=0:
    in_file=0
    print id, sub, '+++'
    resp, articlenu, id, body = s.body(id)
    for line in body:
      if in_file==0 and string.find(line,'begin ')>=0:
        in_file = 1
        filenm = 'tmpfile'
        try:
          f=open(filenm,'w')
        except:
          in_file=0
          print 'Cannot open file'
      if in_file == 1:
        if string.find(line,'end')==0:
          in_file = 0
          f.write(line+'\n')
          f.close()
          uu.decode(filenm)
          filenm = ''
        else:
          f.write(line+'\n')
    print '\tdone'

s.quit()