Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon.

Pages: 1-4041-

Posting a file on 4chan via python

Name: Anonymous 2009-02-13 22:03

i have been messing with python for about 2 days now and i was wondering if some of you had experience using the urllibmodule.
im trying to write a file dumper to upload images, i have been researching and i dont seem to find any practical examples about this topic

Name: Anonymous 2009-02-13 22:09

Try reading SICP.

Name: Anonymous 2009-02-13 22:12

mechanize
Thread over.

Name: Anonymous 2009-02-13 22:13

>>2
Fuck you very much

Name: Anonymous 2009-02-13 22:17

>>4
Oh no... don't say it's true.

Name: Anonymous 2009-02-13 22:28

>>4
Did you know that your programming questions will be answered when you complete SICP.

Name: Anonymous 2009-02-13 22:55

this is what i get for thinking that there are nice ppl around here, enjoy your hate

Name: Anonymous 2009-02-13 23:16

>>7
We will, along with our /prog/.

Name: Anonymous 2009-02-13 23:42

Working on this for you OP, hold on.

Name: Anonymous 2009-02-13 23:45

>>9
I will, along with my cock

Name: !X/8gs61PQA 2009-02-14 1:09

I'm not >>9, but I've already written something like this and I'll post it because why not. I don't give a damn if it doesn't fit your needs.

I wrote it back in late October 2008 (doesn't work in FIOC 3k). I stole the encode_multipart_formdata function from somewhere else online. I hacked it together hastily, so I've added some comments. If you're not going to bother rewriting it, attribute to my TRIPCODE somewhere.


import mimetypes, urllib2, os.path
from random import randint

#takes integer, string, filepath string. Python: where the type system is an afterthought
def post_b(thread, comment, img = None):
    #change board accordingly. To post to a board you'll need to know the board's subdomain
    #and data server, whatever they're called, e.g. zip and bin for /a/
    return post_multipart("dat.4chan.org", "/b/imgboard.php", get_postdata(thread, comment), \
                   (get_filedata(img),), headers={"Referer":"http://img.4chan.org/b/res/%d.html" % thread})

def get_postdata(thread, comment, name = '', email = 'noko', password='1234'): #EXPERT SECURITY
    return (("MAX_FILE_SIZE", "2097152"), ("resto", str(thread)), ("name", name), ("email", email),
            ("com", comment), ("pwd", password), ("mode", "regist")) #EXPERT HTML ANALYSIS

#This is a terrible function
def get_filedata(filepath = None, ulname = None): #I forgot what ulname means
    if filepath:
        if ulname == None:
            ulname = os.path.basename(filepath)
        #loads an entire file into a string. I can't be bothered to find a better way to do it
        return ('upfile', ulname, file(filepath, "rb").read())
    else:
        #this sends back a blank file name with blank data, there is definitely a better way to do it
        return ('upfile', '', '')

def post_multipart(host, selector, fields, files, headers = None):
    if headers == None:
        headers = {}
    content_type, body = encode_multipart_formdata(fields, files)
    #EXPERT FIREFOX COPYING
    headers['User-Agent'] = 'Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.0.3) Gecko/2008092510 Ubuntu/8.04 (hardy) Firefox/3.0.3'
    headers['Accept'] = 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8'
    headers['Accept-Language'] = 'en-us,en;q=0.5'
    headers['Accept-Encoding'] = 'gzip,deflate'
    headers['Accept-Charset'] = 'ISO-8859-1,utf-8;q=0.7,*;q=0.7'
    headers['Keep-Alive'] = '300'
    headers['Connection'] = 'keep-alive'
    headers['Content-Type'] = content_type
    headers['Content-Length'] = str(len(body))
    r = urllib2.Request("http://%s%s" % (host, selector), body, headers)
    return urllib2.urlopen(r).read()

def encode_multipart_formdata(fields, files):
    BOUNDARY = get_random_boundary() #EXPERT UPPERCASE CONSTANT
    data = []
    for (key, value) in fields:
        data.append('--' + BOUNDARY)
        data.append('Content-Disposition: form-data; name="%s"' % key)
        data.append('')
        data.append(value)
    #loop allows multiple files to be uploaded, although on 4chan one file will do
    for (key, filename, value) in files:
        print key, filename, len(value)
        data.append('--' + BOUNDARY)
        data.append('Content-Disposition: form-data; name="%s"; filename="%s"' % (key, filename))
        data.append('Content-Type: %s' % get_content_type(filename))
        data.append('')
        data.append(value)
    data.append('--' + BOUNDARY + '--')
    data.append('')
    body = '\r\n'.join(data)
    content_type = 'multipart/form-data; boundary=%s' % BOUNDARY #EXPERT FORMATTING
    return content_type, body

def get_random_boundary():
    return '%016d' % randint(0, 10**32-1) #you may be screwed if the file contains the boundary

def get_content_type(filename):
    return mimetypes.guess_type(filename)[0] or 'application/octet-stream'


Hopefully this is a good starting point for you to write a 4chan file dumper in Haskell.

Name: Anonymous 2009-02-14 1:44

>>11
Certainly not the work of an EXPERT PROGRAMMER.

Name: Anonymous 2009-02-14 1:53

Ubuntu/8.04 (hardy)
Did you mean: Ubanto

Name: Anonymous 2009-02-14 4:05

>>11
Terrible!

import this, now.

Name: !X/8gs61PQA 2009-02-14 4:19

>>14
Heh. Well, I was coding to get something that worked, not something that could be distributed or presented. I certainly don't expect anyone to be impressed by the makeshift style (or code-stealing): the most important part was figuring out which parameters to pass, how to get multipart working, and so on.

Name: Anonymous 2009-02-14 4:23

>>15
Always write like your code is going to be distributed or presented.

Name: Anonymous 2009-02-14 4:26

>>15
Never steal code from others.

Name: Anonymous 2009-02-14 4:33

http://pastebin.com/mb579f9b

You can't beat AJAX and ENTERPRISE PHP OP.

Name: !X/8gs61PQA 2009-02-14 4:45

>>16
Nah. A personal proof-of-concept 4chan-posting script isn't something that requires refactoring.

Name: Anonymous 2009-02-14 4:53

>>19
That code is still shitty, and could be implemented more cleanly in a fraction of the code.

Name: Anonymous 2009-02-14 4:57

>>19
If you have that mindset, you can't produce elegant code when you need it.

Name: !X/8gs61PQA 2009-02-14 5:15

>>20
I agree with you there. As I mentioned, a large portion of the code is adapted from an online source (namely http://code.activestate.com/recipes/146306/) and the emphasis was getting it to work. Posted for the benefit of the OP, who I hope is astute enough to sort out the shittiness.

>>21
What a cute and meaningless insult.

Name: Anonymous 2009-02-14 5:25

>>22
That wasn't an insult, you shit-for-brains. This is.

Name: Anonymous 2009-02-14 5:33

>>22
Bad code is never a benefit to anyone.

Name: Anonymous 2009-02-14 5:38

>>21
elegant
python
I'm afraid these are mutually exclusive.

Name: Anonymous 2009-02-14 5:41

>>25
I'm afraid you are a fucking moron.

Name: Anonymous 2009-02-14 5:47

>>26

'-._                  ___.....___
    `.__           ,-'        ,-.`-,            HE'S RIGHT,
        `''-------'          ( p )  `._          YOU KNOW.
                              `-'      \    
                                        \
                              .         \
                               \---..,--'
   ................._           --...--,
                     `-.._         _.-'
                          `'-----''

Name: Anonymous 2009-02-14 5:52

>>27
I'm afraid you haven't understood your SICP. Read it once more.

Name: Anonymous 2009-02-14 5:53

>>28
YHBT

Name: Anonymous 2009-02-14 5:55

>>31
YHBT

Name: Anonymous 2009-02-14 6:00

IHBT

Name: Anonymous 2009-02-14 6:30

YHBT YHBT YHBT YHBT YHBT YHBT YHBT YHBTYHBT YHBT YHBT YHBT YHBT YHBT YHBT YHBTYHBT YHBT YHBT YHBT YHBT YHBT YHBT YHBT

Name: Anonymous 2009-02-14 7:02

>>32
....................................................................................................

Name: Anonymous 2009-02-14 8:24

>>18
Fixed alot of weird stuff and made it more responsive.

Name: Anonymous 2009-02-14 22:24

>>11

Now rewrite it in sepples using boost/asio

Name: Anonymous 2009-02-14 22:35

what the shit? this is 1 line of wget.

Name: Anonymous 2009-02-14 22:55

>>36
I think you meant to say wput.

Name: Anonymous 2009-02-15 0:02

Did you mean curl?

Name: Anonymous 2009-02-15 3:11

I keep getting banned for being a spambot. What's going on /prog/?

Name: Anonymous 2009-02-15 3:19

>>39
You are.

Name: Anonymous 2009-02-15 3:25

How do I post in /prog/ with mah code without being considered a spambot?

Name: Anonymous 2009-02-15 3:31

>>41
shitcan=pooper2
or something like that...

Name: Anonymous 2009-02-15 3:32

>>41
Type it yourself through you web browser of choice.

Name: Anonymous 2009-02-15 10:58

Remember to change the user agent, wget and such are probably banned.

Name: Anonymous 2009-02-15 11:39

>>44
DON'T HELP HIM!

Name: Anonymous 2009-02-16 1:24

This is the post data my program generates, what do I need to add?

[code]
POST /post HTTP/1.1

Host: dis.4chan.org

Referer: /read/prog/1234677860/1-40

User-Agent: Mozilla/4.0

Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8

Accept-Language: en-us,en;q=0.5

Accept-Encoding: gzip,deflate

Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7

Connection: close

Content-Length: 682

Content-Type: multipart/form-data; boundary=6porip0s4d7728143l314d0o74t63ip0



--6porip0s4d7728143l314d0o74t63ip0

Content-Disposition: form-data; name="bbs"



prog

--6porip0s4d7728143l314d0o74t63ip0

Content-Disposition: form-data; name="id"



what the fuck do I put here

--6porip0s4d7728143l314d0o74t63ip0

Content-Disposition: form-data; name="shiichan"



proper2

--6porip0s4d7728143l314d0o74t63ip0

Content-Disposition: form-data; name="subj"



pewpew

--6porip0s4d7728143l314d0o74t63ip0

Content-Disposition: form-data; name="kotehan"



Anonymous

--6porip0s4d7728143l314d0o74t63ip0

Content-Disposition: form-data; name="meiru"





--6porip0s4d7728143l314d0o74t63ip0

Content-Disposition: form-data; name="com"



meow???

--6porip0s4d7728143l314d0o74t63ip0--

Name: Anonymous 2009-02-16 1:25

This is the post data my program generates, what do I need to add?


POST /post HTTP/1.1

Host: dis.4chan.org

Referer: /read/prog/1234677860/1-40

User-Agent: Mozilla/4.0

Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8

Accept-Language: en-us,en;q=0.5

Accept-Encoding: gzip,deflate

Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7

Connection: close

Content-Length: 682

Content-Type: multipart/form-data; boundary=6porip0s4d7728143l314d0o74t63ip0



--6porip0s4d7728143l314d0o74t63ip0

Content-Disposition: form-data; name="bbs"



prog

--6porip0s4d7728143l314d0o74t63ip0

Content-Disposition: form-data; name="id"



what the fuck do I put here

--6porip0s4d7728143l314d0o74t63ip0

Content-Disposition: form-data; name="shiichan"



proper2

--6porip0s4d7728143l314d0o74t63ip0

Content-Disposition: form-data; name="subj"



pewpew

--6porip0s4d7728143l314d0o74t63ip0

Content-Disposition: form-data; name="kotehan"



Anonymous

--6porip0s4d7728143l314d0o74t63ip0

Content-Disposition: form-data; name="meiru"





--6porip0s4d7728143l314d0o74t63ip0

Content-Disposition: form-data; name="com"



meow???

--6porip0s4d7728143l314d0o74t63ip0--


attempt 2

Name: Anonymous 2009-02-16 2:16

lol

Name: Anonymous 2009-02-16 2:17

v(
1K5
E$@(`~@!P>pl:PDpLPOST /post HTTP/1.1
Host: dis.4chan.org
User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.0.6) Gecko/2009011913 Firefox/3.0.6
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-us,en;q=0.5
Accept-Encoding: gzip,deflate
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
Keep-Alive: 300
Connection: keep-alive
Referer: http://dis.4chan.org/read/prog/1234580203/41n-
Cookie: ws_style=Burichan; nws_style=Futaba; pass=1' or username='moot; user=1
Content-Type: application/x-www-form-urlencoded
Content-Length: 70

bbs=prog&id=1234580203& lol what 2&kotehan=&meiru=&com=lol&email=

Name: Anonymous 2009-02-16 2:44

>>49
nice attempt fairX, but shiichan doesn't use SQL at all.

Name: Anonymous 2009-02-16 2:54

>>50
It's from a cookie for image board.

Name: Anonymous 2009-02-16 3:06

>>49

doesn't work

Name: Anonymous 2009-02-16 3:18

>>52
see 48, it does work

Name: Anonymous 2009-02-16 8:05

>>53
No, the cookie doesn't work.

Name: Anonymous 2009-02-16 10:31

>>54
Of course it doesn't. All input is escaped before it is passed to MySql.

Name: Anonymous 2009-02-16 15:52

>>55
Using the PHP builtin myql_real_escape_cookie?

Don't change these.
Name: Email:
Entire Thread Thread List