-
Notifications
You must be signed in to change notification settings - Fork 1
/
upload.py
151 lines (130 loc) · 6.07 KB
/
upload.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
import cgi
import os
from google.appengine.ext import webapp
from google.appengine.ext.webapp.util import run_wsgi_app
from google.appengine.ext import db
import base64
import sys
import logging
class Torrent(db.Model):
info_hash = db.StringProperty()
content = db.TextProperty()
date = db.DateTimeProperty(auto_now_add=True)
class TorrentURL(db.Model):
torrent = db.StringProperty()
url = db.StringProperty()
class Upload(webapp.RequestHandler):
def post(self):
torrent_url=self.request.get('torrent_url')
logging.debug('requested download of: ' + torrent_url)
torrent64=self.request.get('fileb64')
file_contents=''
if torrent_url is not None and len(torrent_url) > 12: # CHECK IF IT'S URL SUBMISSION
torrent_urls_query = TorrentURL.all()
torrent_urls_query.filter("url =", torrent_url)
if torrent_urls_query.count(1) < 1:
import urllib2
from google.appengine.api import urlfetch
try:
result = urlfetch.fetch(torrent_url)
file_contents = result.content
except:
self.response.out.write('failed to get the file ' + str(sys.exc_info()[0]))
else:
self.response.out.write('url already in store')
elif torrent64 is not None and len(torrent64) > 1: # BASE64 ENCODED FILE
file_contents = base64.b64decode(torrent64)
else:
try: # TRY TO ACCESS FILE CONTENTS
file_contents = self.request.POST['file'].value
except:
self.response.out.write('no url nor file were provided')
file_contents=''
if len(file_contents) > 0:
try: # DO NOT OUTPUT ANYTHING TOO UGLY :P
import bencode
try: # TRY TO DECODE BENCODE
file_dictionary = bencode.bdecode(file_contents)
except:
self.response.out.write('torrent file can\'t be read' + str(sys.exc_info()[0]))
file_dictionary=None
logging.debug('failed read: ' + str(sys.exc_info()[0]))
if file_dictionary is not None:
import sha
info_hash = sha.new(bencode.bencode(file_dictionary['info'])).hexdigest().upper() # create info_hash
import binascii
info_hash_b32 = base64.b32encode(binascii.unhexlify(info_hash))
torrent = Torrent()
torrent.info_hash = info_hash
torrents_query = Torrent.all()
torrents_query.filter("info_hash = ",info_hash)
if torrents_query.count(1) < 1:
torrent.content = "info hash: " + info_hash + "\n"
torrent.content += "magnet link: <a href=\"magnet:?xt=urn:btih:" + info_hash_b32 + "\">magnet:?xt=urn:btih:" + info_hash_b32 + "</a>\n\n"
try: # TRY TO READ THE CREATION DATE
import time
torrent.content += "Created: " + time.asctime(time.gmtime(file_dictionary['creation date'])) + "\n"
except:
torrent.content += "no date in file.\n"
try: # TRY TO ACCESS THE CREATOR STRING
torrent.content += "Created by: " + file_dictionary['created by'] + "\n\n"
except:
torrent.content += "\n"
try: # TRY TO GET COMMENTS FROM THE CREATOR
torrent.content += file_dictionary['comment'] + "\n\n"
except:
torrent.content += "This torrent has no comments.\n\n"
try: #TRY TO ACCESS THE LIST OF FILES IN CASE ITS MULTIFILE TORRENT
file_dictionary['info']['files']
torrent.content += "Files:\n"
for each_file in file_dictionary['info']['files']:
torrent.content += each_file['path'][0] + " - " + str(each_file['length']) + "\n"
except:
try: # IF THE LIST OF FILES IS NOT ACCESSIBLE GET THE NAME OF THE TORRENT
file_dictionary['info']['name']
torrent.content += "File:\n"
torrent.content += file_dictionary['info']['name']
try: # GET THE SIZE OF THE TORRENT
torrent.content += " - " + str(file_dictionary['info']['length']) + "\n"
except:
torrent.content += "\n"
except:
self.response.out.write('bad torrent, no file name declared')
torrent.put() # STORE THE TEXT TO THE DATASTORE
if torrent_url is not None and len(torrent_url) > 12: # IN CASE ITS FROM A FETCH ALSO ADD WHERE IT CAME FROM
torrent_URL = TorrentURL()
torrent_URL.torrent = torrent.info_hash
torrent_URL.url = torrent_url
torrent_URL.put()
from google.appengine.api import memcache
memcache.delete('front_page')
memcache.delete('rss')
memcache.set('fresh', '1')
logging.debug(str(memcache.get('fresh')))
self.redirect('/' + info_hash) # 302 HTTP REDIRECT TO THE TORRENT PAGE
else: # THERES A TORRENT IN THE DATASTORE
if torrent_url is not None and len(torrent_url) > 12:
torrent_urls_query = TorrentURL.all()
torrent_urls_query.filter("url =", torrent_url)
if torrent_urls_query.count(1) < 1:
torrent_URL = TorrentURL()
torrent_URL.torrent = torrent.info_hash
torrent_URL.url = torrent_url
torrent_URL.put()
from google.appengine.api import memcache
memcache.delete(torrent.info_hash)
memcache.delete('page' + torrent.info_hash)
memcache.delete('rss')
memcache.deleter('text')
memcache.set('fresh', '1')
logging.debug(str(memcache.get('fresh')))
self.redirect('/' + info_hash) # 302 HTTP REDIRECT TO THE TORRENT PAGE
else:
self.response.out.write('torrent already here')
except:
self.response.out.write('unexpected error' + str(sys.exc_info()[0]))
application = webapp.WSGIApplication([('/upload', Upload)], debug=True)
def main():
run_wsgi_app(application)
if __name__ == "__main__":
main()