Su IRC c’è spesso bisogno di usare un servizio di nopaste per evitare di inondare il canale di righe e righe di testo. Spesso c’è bisogno di inviare un file completo sul nopaste, così “ispirandomi a” (leggere: “copiando”) nopaste.pl di dfa ho scritto un programma in python che permette di inviare direttamente a http://rafb.net/nopaste il file, o in alternativa quello che riceve dallo stdin.L’unico vantaggio rispetto al programma di dfa è che questo non ha bisogno di installare librerie varie (per la versione in Perl è necessario installare LWP::UserAgent).
L’uso del programma è estremamente semplice:
pynopaste <content> [description]
Invia il file content, il campo description è opzionale e permette di aggiungere la descrizione da mostrare sul nopaste. L’alternativa è inviare il testo tramite stdin, ad esempio:
for i in *.config ; do echo "========== $i ==========" ; cat $i ; done | pynopaste
Questo codice concatena tutti i file con estensione .config nella cartella corrente e lo invia a pynopaste.
Ecco il codice:
#!/usr/bin/python
# pynopaste.pl -- nopaste files from command line
# Copyright (C) 2007, IppatsuMan < ippatsuman * gmail >
#
# *slightly* (yeah, right...) inspired by a work by Davide
# Angelocola ( http://dfa.slackware.it )
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
# USA.
# usage:
# pynopaste file [description]
# pynopaste <stdin>
import os, sys, urllib
from urllib2 import Request, urlopen, URLError, HTTPError
if __name__ == "__main__":
availableext = {
('pl', 'pm') : 'Perl',
('cpp', 'cc', 'hh') : 'C++',
('c', 'h') : 'C',
('cs') : 'C#',
('pas') : 'Pascal',
('bas', 'vb') : 'Visual Basic',
('java') : 'Java',
('py') : 'Python',
('rb') : 'Ruby',
('sql') : 'SQL',
('php') : 'PHP'}
desc = ""
lang = "Plain Text"
if len(sys.argv)>1:
try:
f = open(sys.argv[1])
lines = f.readlines()
vext = ""
f.close()
# get the file extension
fileext = os.path.splitext(sys.argv[1])[1][1:].lower()
for ext in availableext.keys():
if fileext in ext:
try:
lang = availableext[ext]
except:
lang = 'Plain Text'
break
if len(sys.argv)>2:
desc = sys.argv[2]
except IOError, e:
print e
else:
lines = sys.stdin.readlines()
if len(desc) == 0:
for line in lines:
if len(line)>0:
desc = line
break
req = Request("http://rafb.net/paste/paste.php")
try:
username = os.environ["LOGNAME"]
except:
username = "Anonymous"
data = urllib.urlencode({"nick" : username[:30], "text" : "".join(lines), "desc" : desc[:50], "lang" : lang})
try:
response = urlopen(req, data)
except HTTPError, e:
print "HttpError: "+e.code
except URLError, e:
print "URLError: "+e.reason
else:
resurl = response.geturl()
if "toofast" in resurl:
print "Too fast, send again"
else:
print resurl
N.B.: non sono affatto un guru di Python (in effetti lo studiacchio nei ritagli di tempo) e non è affatto detto che questo codice sia il modo migliore per realizzare il programma né tanto che sia privo di bug. Se avete appunti di stile lasciate pure un commento :)
Update 03/05/2009: aggiornati i link del programma originale di dfa.