-
Notifications
You must be signed in to change notification settings - Fork 1
/
pepenet.py
executable file
·138 lines (108 loc) · 3.69 KB
/
pepenet.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
#!/usr/bin/env python3
import ipfsapi
import os
import pepe
import requests
import socket
import base64
from flask import Flask, request, redirect, render_template
from werkzeug.utils import secure_filename
from argparse import ArgumentParser
import logging
from threading import Thread
from time import sleep
parser = ArgumentParser()
parser.add_argument('-H', default='localhost')
args = parser.parse_args()
ipfs_host = None
for _ in range(5):
try:
ipfs_host = socket.gethostbyname(args.H)
break
except socket.gaierror:
sleep(10)
ipfs_port = 5001
ipfs_web_port = 8080
flask_port = 8000
UPLOAD_FOLDER = 'tmp'
ALLOWED_EXTENSIONS = ('.txt', '.png', '.jpg', '.jpeg')
if not os.path.exists(UPLOAD_FOLDER):
os.makedirs(UPLOAD_FOLDER)
app = Flask(__name__)
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
ipfs = None
pman = None
for _ in range(5):
try:
ipfs = ipfsapi.connect(ipfs_host, ipfs_port)
pman = pepe.PepeMan(ipfs, ipfs_host)
break
except ipfsapi.exceptions.ConnectionError:
sleep(10)
def get_pepes():
result = []
for pepe_hash in pman.get_all_pepes():
logging.debug("Showing: {}".format(pepe_hash))
pepe = {"url": "http://{}:{}/ipfs/{}".format(ipfs_host,
ipfs_web_port,
pepe_hash),
"normieness": "Calculating...",
"hash": pepe_hash
}
result.append(pepe)
if request.headers.get('X-Forwarded-For'):
# We're behind a proxy so the user can't access the ipfs node
for pepe in result:
response = requests.get(pepe["url"])
uri = "data:{};base64,{}"\
.format(response.headers['Content-Type'],
base64.b64encode(response.content).decode("utf-8"))
pepe["url"] = uri
return result
@app.route("/calc_normieness/<pepe_hash>")
def normieness_calc(pepe_hash):
#result = pman.calc_normieness(pepe_hash)
result = (0 ,0)
return "{} ({})".format(*result)
@app.route("/")
def home_page():
return render_template("main.html",
ipfs_info=ipfs.id(),
rare_pepes=get_pepes(),
peer_n=pman.get_peer_number(),
server=ipfs_host,
port=ipfs_port)
@app.route("/download/<pepe_hash>")
def download_file(pepe_hash):
pman.pin_pepe(pepe_hash)
return redirect('/')
@app.route('/upload', methods=['POST'])
def upload_file():
if 'file' not in request.files:
return 'No file part', 400
form_file = request.files['file']
if form_file.filename == '':
return 'No selected file', 400
if form_file.filename.lower().endswith(ALLOWED_EXTENSIONS):
filename = secure_filename(form_file.filename)
file_path = os.path.join(app.config['UPLOAD_FOLDER'], filename)
form_file.save(file_path)
pman.upload_pepe(file_path)
# We delete the file because ipfs has copied it into its repository
os.remove(file_path)
return redirect('/')
else:
return 'Bad extension', 400
def save_pepes():
while True:
pman.save_pepes_lists()
sleep(30)
if __name__ == "__main__":
logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s -'
'%(message)s', datefmt='%m/%d/%Y %I:%M:%S %p')
logger = logging.getLogger()
logger.setLevel(logging.DEBUG)
data_saver_thread = Thread(target=save_pepes)
data_saver_thread.start()
logging.debug("Flask path: {}".format(app.instance_path))
app.run('0.0.0.0', port=flask_port, debug=True)