Random news downloader (first script!)

K

Kim

Random image downloader for specified newsgroup. Hi I'm writing a small script that will download random images from a specified newsgroup. I've imported yenc into the script but I can't open the image or save it. This is my first script so be gentle!

Heres the script


####random group downloader####
import nntplib
import string, random
import mimetools
import StringIO
import rfc822
import sys, base64
import os
import email
import errno
import mimetypes


SERVER = "news.server.co.uk" #Insert news server here
GROUP = "alt.binaries.pictures.blah" #newsgroup will go here

# connect to server
server = nntplib.NNTP(SERVER)

resp, count, first, last, name = server.group(GROUP)

for i in range(10):
try:
id = random.randint(int(first), int(last))
resp, id, message_id, text = server.article(str(id))
except (nntplib.error_temp, nntplib.error_perm):
pass # no such message (maybe it was deleted?)
else:
break # found a message!
else:
raise SystemExit

text = string.join(text, "\n")
file = StringIO.StringIO(text)

msg = mimetools.Message(file)

#display message information
#print "File type", "=>", msg.gettype()
#print "Encoding", "=>", msg.getencoding()
#print "plist", "=>", msg.getplist()

message = rfc822.Message(file)

for k, v in message.items():
print k, "=", v

file = message.fp.read()

def yenc_decode(file):
# <i>find body</i>
while 1:
line = file.readline()
if not line:
return None
if line[:7] == "=ybegin":
break
# <i>extract data</i>
buffer = []
while 1:
line = file.readline()
if not line or line[:5] == "=yend":
break
if line[-2:] == "\r\n":
line = line[:-2]
elif line[-1:] in "\r\n":
line = line[:-1]
data = string.split(line, "=")
buffer.append(string.translate(data[0], yenc42))
for data in data[1:]:
data = string.translate(data, yenc42)
buffer.append(string.translate(data[0], yenc64))
buffer.append(data[1:])
return buffer
#the following should write to a text file
#inp = ("file","r")
#outp = open("text.txt","w")
#for line in file:
#outp.write(line)
#print file
#outp.close()



--------------= Posted using GrabIt =----------------
------= Binary Usenet downloading made easy =---------
-= Get GrabIt for free from http://www.shemes.com/ =-
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,999
Messages
2,570,243
Members
46,835
Latest member
lila30

Latest Threads

Top