473,322 Members | 1,566 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

Random image downloader for newsgroups (first script)

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/ =-

Sep 7 '06 #1
4 3046
Kim schrieb:
SERVER = "news.server.co.uk" #Insert news server here
GROUP = "alt.binaries.pictures.blah" #newsgroup will go here
Just why do I imagine there will be an adult newsgroup in the end....?

SCNR,

Diez
Sep 7 '06 #2
SERVER = "news.server.co.uk" #Insert news server here
GROUP = "alt.binaries.pictures.blah" #newsgroup will go here

Just why do I imagine there will be an adult newsgroup in the end....?
I can see the freshmeat announcement now: "Random Boob Visualizer 1.0"...

Skip
Sep 7 '06 #3

Skip Montanaro wrote:
SERVER = "news.server.co.uk" #Insert news server here
GROUP = "alt.binaries.pictures.blah" #newsgroup will go here
Just why do I imagine there will be an adult newsgroup in the end....?

I can see the freshmeat announcement now: "Random Boob Visualizer 1.0"...

Skip
mmmmmm, boobs.

;-)

Sep 7 '06 #4
Kim wrote:
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()
The following worked for me. It printed text and even
generated viewable images when I went to a pictures group.

I cleaned it up a little. I'm sure it can be cleaner. That's partly
because I didn't want to drift too far from your original and
partly because I don't get much Python practice.

Don't use "file" as a variable name, it overwrites the built in
file object. It likely didn't matter in this case, but would
eventually cause problems.

####random group downloader####
import nntplib
import random
import StringIO
import mimetools
import rfc822
import binascii
#import base64
#import sys
#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

msgFile = StringIO.StringIO("\n".join(text))

# Display mime message information
msg = mimetools.Message(msgFile)
msgFile.seek(0)
print "File type", "=>", msg.gettype()
print "Encoding", "=>", msg.getencoding()
print "plist", "=>", msg.getplist()

# Display rfc822 message information
message = rfc822.Message(msgFile)
msgFile.seek(0)
for k, v in message.items():
print k, "=", v

yenc42 = "".join([chr((i - 42) & 255) for i in range(256)])
yenc64 = "".join([chr((i - 64) & 255) for i in range(256)])

def yenc_decode(fileIn):
# <i>find body</i>
while 1:
line = fileIn.readline()
if not line:
return None
if line[:7] == "=ybegin":
break
# <i>extract data</i>
buffer = []
while 1:
line = fileIn.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]
splitData = line.split("=")
buffer.append(splitData[0].translate(yenc42))
for data in splitData[1:]:
buffer.append(data[0].translate(yenc64).translate(yenc42))
buffer.append(data[1:].translate(yenc42))
return buffer

def uu_decode(fileIn):
# <i>find body</i>
while 1:
line = fileIn.readline()
if not line:
return None
if line[:6] == "begin ":
break
# <i>extract data</i>
buffer = []
while 1:
line = fileIn.readline()
if not line or line[:3] == "end":
break
buffer.append(binascii.a2b_uu(line))
return buffer

# Write the raw message to a text file
outp = open("text.txt","w")
for line in msgFile:
outp.write(line)
outp.close()
msgFile.seek(0)

# Find and convert a yencode or uu encode binary in the message.
image = yenc_decode(msgFile)
if image == None:
msgFile.seek(0)
image = uu_decode(msgFile)

# output the decoded binary data, if any
if image != None:
outp = open("text.jpg","wb")
outp.write("".join(image))
outp.close()

Sep 8 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
by: bhennon | last post by:
Hey all, I have a small php script that calls a random image at the following page. http://www.2006ymcanationals.com/random.php IT WORKS IF I go directly to the above link. I am trying to...
6
by: Olly | last post by:
I've found a basic script, however I also need to add alt and title attributes as well, how would I go about doing this? Here's the script I found: Thanks <script language="JavaScript"> <!--...
9
by: Michael Burtenshaw | last post by:
I would like to make a slide show using random images. The problem is my host is 250.com, and they don't support cgi-programs. Is there another way to accomplish random images?
5
by: Linda | last post by:
Greetings: I have found a random image script that I like. It is located here: http://www.javascriptcity.com/scripts/local/simage3.htm I'd like to edit this to have 4 different slots for...
15
by: David Lozzi | last post by:
Howdy, I have a function that uploads an image and that works great. I love ..Nets built in upload, so much easier than 3rd party uploaders! Now I am making a public function that will take the...
4
by: tshad | last post by:
I am trying to set up an Image authorization where you type in the value that is in a picture to log on to our site. I found a program that is supposed to do it, but it doesn't seem to work. ...
0
by: Kim | last post by:
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...
7
by: Brian | last post by:
Hi there I have been looking for a script that can randomly rotate 8 different images on 1 page. I used to have a script that did this but can't find it now. I have found loads of script that...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.