User:Roelroscama/trim3/protoyping3: Difference between revisions
Roelroscama (talk | contribs) No edit summary |
Roelroscama (talk | contribs) |
||
Line 1: | Line 1: | ||
= Various code prototypes = | = Various code prototypes = | ||
This tremester resulting in a whole range of small scripts that have served as tools for projects or research. | This tremester resulting in a whole range of small scripts that have served as tools for projects or research or leisure or convenience. | ||
== Batch.py == | == Batch.py == |
Latest revision as of 06:49, 1 July 2013
Various code prototypes
This tremester resulting in a whole range of small scripts that have served as tools for projects or research or leisure or convenience.
Batch.py
The basis of many of the scripts I wrote this semester. Executes a os command for every line of the file. This examples executes a traceroute on a list of URLs.
#!/usr/bin/python
# a script to batch terminal commands from a list of things in a file.
import os, sys
fn = sys.argv[1]
f = open(fn, 'r')
for i in f:
i = i.split()
str1 = ' '.join(i)
print "////////////////////////////////////////////////////////////////"
print "Current item in list:", str1
command = 'traceroute -q 2 -m 20 '+str1+' > AlexaTop20/'+str(str1)+'.txt'
print "Executing:", command, '\n'
os.system(command)
Google TTS API
A script that allows the user convert a text to speech using google translate's unofficial TTS api. Was used in making the Telewar films.
#!/usr/bin/python
# a script to change text into speech (.mp3) using Google TTS. It reads the text from a file.
# every line of the text becomes a new soundfile. Google has a limit of 100 chars per query.
import os, sys
fn = sys.argv[1]
f = open(fn, 'r')
for i in f:
str1 = i.strip('\n')
urlstr1 = str1.replace(' ', '%20') #make text URL proof
str2 = str1.replace(' ','-')
print "////////////////////////////////////////////////////////////////"
print "Current item in list:", str1,"|||"
command1 = 'curl -A "Mozilla" "http://translate.google.com/translate_tts?tl=en&q="'+str(urlstr1)+'"" > '+str(str2)+'.mp3'
print "CURLING:", command1, '\n'
os.system(command1)
ScamDex Email retriever
A script that crawls through scamdex.com, opens the e-mails from the scam email archive one by one and stores any email adresses it finds in a file. Scamdex is a very weird website that has a large archive of scam emails (since 2000). Also it has weird class names for it's tables.
# Script to scrape scammer's e-mails off Scamdex.com.
# The website contains monthly lists of received spam-emails ordered in a large
# list of hrefs on the page. Very annoying to click through manually.
# The first function get's all the hrefs from the lists.
from BeautifulSoup import BeautifulSoup as Bsoup
import urllib2, re
def GetFirstLinks():
url ='http://www.scamdex.com/scam-database/index_2013-3.php'
html = urllib2.urlopen(url)
soup = Bsoup(html)
content = soup.findAll('a', attrs={'class':'indexSubject'})
links = []
print "//////////////////////////////////////////////////////////////"
print "GETTING ADRESSES FROM", url
print "//////////////////////////////////////////////////////////////"
for link in content:
links.append(link['href'])
return links
def Test(x):
print str(x)
def GetEmailFromLink(x):
for url in x:
url = ''.join(url)
fullurl = 'http://www.scamdex.com'+url
html = urllib2.urlopen(fullurl)
soup = Bsoup(html)
content = soup.findAll('th', attrs={'class':'anal'})
email_pattern = re.compile("[-a-zA-Z0-9._]+@[-a-zA-Z0-9_]+.[a-zA-Z0-9_.]+")
email = re.findall(email_pattern,str(content))
email = ''.join(email)
print email
a = GetFirstLinks()
GetEmailFromLink(a)
Automatic Mailer
Uses SMTP lib to send an email at random intervals. To a list of adreses in a file.
#!/usr/bin/python
import smtplib, os, sys, time
from random import randint
def force_movie_mailer(TO, TEXT):
FROM = 'from'
SUBJECT = 'subject'
username = '@gmail.com'
pw = 'pw'
MESSAGE = """\From: %s\nTo: %s\nSubject: %s\n\n%s
""" % (FROM, TO, SUBJECT, TEXT)
try:
server = smtplib.SMTP('smtp.gmail.com:587')
server.ehlo()
server.starttls()
server.login(username, pw)
server.sendmail(FROM, TO, MESSAGE)
server.close()
print 'email sent to:', TO, TEXT
except:
print 'failed to send! to',TO,TEXT
TO ='@gmail.com'
TEXT = """
this
is
the
text
"""
fn = sys.argv[1]
f = open(fn, 'r')
for i in f:
TO = i.strip('\n')
force_movie_mailer(TO, TEXT)
time.sleep(randint(1,15))
Bitonic JSON Reader
Very early in the trimester I made a script that would check Bitonic.nl for their bitcoin exchange rates. Bitonic is a website where one can easily buy and sell bitcoins using ideal. A modified version of this script runs as a cron and will send me an email once the exchange rate hits a certain amount. Nice tool for bitcoin speculation.
#!/usr/local/bin/python
import urllib2, json
from time import gmtime, strftime
#price to buy
def CostOfBuying(amount):
data = urllib2.urlopen('https://bitonic.nl/json/?part=rate_convert&check=btc&btc='+str(amount)+'')
j = json.load(data)
euros = j.get('euros')
balance = j.get('balance')
currentTime = strftime("%Y-%m-%d %H:%M:%S", gmtime())
#with open("/Volumes/The Warehouse/Current/Third Trimester/Prototyping/Bitonic Json Reader/bitonic history", "a") as historylog:
# historylog.write(str(euros)+'\t'+str(currentTime)+'\t'+str(amount)+'\n')
print ''+str(amount)+' BTC is bought for', euros, '\n'
print 'Available', balance,'BTC',
def CostOfSelling(amount):
data = urllib2.urlopen('https://bitonic.nl/json/sell?part=offer&check=btc&btc='+str(amount)+'')
j = json.load(data)
euros = j.get('euros')
minimum = j.get('minimum')
currentTime = strftime("%Y-%m-%d %H:%M:%S", gmtime())
#with open("/Volumes/The Warehouse/Current/Third Trimester/Prototyping/Bitonic Json Reader/bitonic history_saleprices", "a") as historylog:
# historylog.write(str(euros)+'\t'+str(currentTime)+'\t'+str(amount)+'\n')
print ''+str(amount)+' BTC sells for', euros, '\n'
print 'Minimum amount to sell', minimum, 'BTC', '\n'
print currentTime
print "///////////////////////////////////////////////////////////"
print "\n"
print "WELCOME TO ROEL's BTC CONVERSION RATE BROADCAST"
print "\n"
CostOfBuying(1)
print "\n"
CostOfSelling(1)
print "\n"
print "///////////////////////////////////////////////////////////"
Synonym Replacement Writing Machine
Written during an hour long prototyping session for the Writing Machines workshop. It uses the NLTK library. Looks for nouns in a text and tries to replace them with a random synonym. However often the 'synonyms' are barely synonyms, just different nouns. As a special feature it kills any formatting and removes any special characters that the text had.
#Synonym Replacement Machine
#Reading Writing Research Methodologies Class on Writing Machines -> Roel RA 2013
#copy paste at will
import sys, re
from nltk.corpus import lin_thesaurus as thes
import nltk,random
#open the file and convert it into the constituent words
fn = sys.argv[1]
f = open(fn, 'r')
text = f.read()
words = re.findall(r'\w+', text) #find all idividual words in the text
newwords = []
for i in words: # for every word
a = nltk.word_tokenize(i)
b = nltk.pos_tag(a) # identify what kind of word it is (noun adjective etc)
if b[0][1] == 'NN': # if the word is a noun that has a synonym
try:
i = thes.synonyms(i, fileid="simN.lsp")[random.randint(1,10)] # replace the word in question with a random synonym from the thesaurus
except: #if it doesnt have a synonym nevermind
pass
newwords.append(i) # add it to the new text
str1 =' '.join(newwords) # that is converted to string text
print str1 #
Pandora's Unboxing
A script that creates an empty .zip and then proceeds to open these. That somehow magically creates more empty .zips in osx. Runs as a fork bomb of some kind. It doesn't freeze the system but keeps it quite occupied. First script that I packaged and distributed as an app.
You can get the app on | the Worm Mediaclub website
#!/usr/local/bin/python
def counter(low, high):
current = low
while current <= high:
yield current
current += 1
fp = os.path.join(os.path.expanduser('~'), 'Desktop')
def fork():
for line in open('skull.txt'):
line = line.strip('\n')
print line
print '\n'
print "MC"
for c in counter(0, 9):
box = 'touch '+fp+'/pandoras-box'+str(c)+'.zip'
unbox = 'open '+fp+'/pandoras-box'+str(c)+'.zip'
os.system(box)
os.system(unbox)
fork()
fork()