Question

I would like to write a program to upload files on http://www.tinyupload.com/, so I searched a method to upload a form. I have written this code to upload a file:

import urllib.request
import urllib.parse
import http.cookiejar
import re

# Use cookies
cookie = http.cookiejar.CookieJar()
urllib.request.install_opener(urllib.request.build_opener(urllib.request.HTTPCookieProcessor(cookie)))

# A function to download an url (GET)
def urldownload(url):
    try:
        page = urllib.request.urlopen(url)
        return page.read().decode('iso-8859-2')
    except urllib.error.HTTPError:
        return False

# Get the url from the form to upload
def geturl(html):
        regex = re.compile('\<form action="(.*?)\" name="upload_form"', re.S)
        url = regex.findall(html)[0]
        return(str(url))

# Get the sid from the url
def getsessionid(url):
        return url[-26:]

# Upload a file
def upload(file):
    url = geturl(urldownload('http://s000.tinyupload.com/index.php'))
    sessionid = getsessionid(url)
    f = open(file).read()
    data = {'MAX_FILE_SIZE': '52428800',
            'uploaded_file': f,
            'file_description': 'File: %s' % (file),
            'sessionid': sessionid}
    data = urllib.parse.urlencode(data)
    result = urllib.request.urlopen(url, data.encode('iso-8859-2'))
    return(result.read().decode('iso-8859-2'))
    #return(str(result.info()))

I should go to the page where is the link to download the file, but I have the form. What is wrong??

Was it helpful?

Solution

There is more simple way to upload file - use requests library.

import requests

session = requests.Session()

index_url   = 'http://s000.tinyupload.com/index.php'
upload_url  = 'http://s000.tinyupload.com/cgi-bin/upload.cgi?sid='

index_request = session.get(index_url)
PHPSESSID = index_request.cookies['PHPSESSID']

files = {'file': open('bitcoin.pdf', 'rb')}
r = requests.post(upload_url+PHPSESSID, files=files)

#Print "File upload finished" page
print r.text

#Print download link
import re
print re.search('http://s000\.tinyupload.com/\?file_id=[^<]+',r.text).group(0)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top