Example #1
0
def get_replies(reply_id):
    import json
    from pattern.web import URL, Twitter

    reply_id = reply_id - 1
    url = URL("https://api.twitter.com/1.1/statuses/mentions_timeline.json",
              method="get",
              query={"since_id": reply_id})

    twitter = Twitter(license=ccpattern)
    url = twitter._authenticate(url)

    user_replies = {}
    bot_replies = {}
    try:
        data = json.loads(url.open().read())
        for reply in data:
            name = reply["user"]["name"].encode('utf-8').strip()
            text = reply["text"].replace("@BotsVsQuotes", "").strip()
            if name == "BotsVsQuotes":
                #bot quotes
                text = text.split(":")
                char_name = text[0]
                bot_replies[char_name] = "".join(text[1:]).strip()
            else:
                #user quotes
                user_replies[name] = text
    except Exception as e:
        print e
        print e.src
        print e.src.read()
        return {}, {}
    return bot_replies, user_replies
Example #2
0
def main():
	table = Datasheet()
	tel = ''
	street = ''
	locality = ''
	title = ''
	for i in range(3):
		page = i+1
		url = 	URL("http://torino.paginegialle.it/pgol/4-veterinari/3-torino/p-%s?mr=50" % page)
		print "collecting from %s" % url
		connection = url.open()
		doc = Document( connection.read() )
		items = doc.by_class('item_sx')
		row = []
		for j, item in enumerate(items):
			divs = item.by_class('address')
			try:	
				title = item.by_class('item_head')[0].by_tag('a')[0].content
			except IndexError, e:
				print >> sys.stderr, "%s" % j, e
				pass
			for z, div in enumerate(divs):
				if div != None:
					try:
						street = div.by_class('street-address')[0].content
						locality = div.by_class('locality')[0].content
						tel = div.by_class('tel')[0].by_class('value')[0].content
					except IndexError, e:
						print >> sys.stderr, "%s" % z, e
						pass
					save = "%s, %s %s, %s \n" % ( plaintext(title), plaintext(street).replace(",", ""), plaintext(locality).replace('(TO)', ''), plaintext(tel).replace(",", "") )
					print >> sys.stderr, save
					row.append(save)
Example #3
0
def reply_tweet(tweet, reply_id, reply_user="******"):
    from pattern.web import URL, Twitter

    tweet = reply_user + " " + tweet
    url = URL("https://api.twitter.com/1.1/statuses/update.json",
              method="post",
              query={
                  "status": tweet,
                  "in_reply_to_status_id": reply_id
              })

    twitter = Twitter(license=ccpattern)
    url = twitter._authenticate(url)

    try:
        # Send the post request.
        url.open()
    except Exception as e:
        print e
        print e.src
        print e.src.read()
Example #4
0
def main():
	table = Datasheet()

	for cap in CAPS:
		url = 	URL("http://www.comuni-italiani.it/001/272/farmacie/cap%s.html" % cap)
		connection = url.open()
		doc = Document( connection.read() )
		items = doc.by_tag("table")
		row = []
		for j, td in enumerate( items[5].by_tag('td') ):
			strcap = "%s, Telefono:" % cap
			save = "%s" % plaintext(td.content).replace('\n', ',', 3).replace("Telefono:", strcap).replace(";", "").replace("Partita Iva", ",Partita Iva") + "\n"
			if save != None:
				row.append( save )
		table.append( row )
		print  "%s ----------------------------------------------------------------------------" % str(j)
		
	table.save("files/farmacie_torino.txt")
Example #5
0
def main():
	table = Datasheet()

	url = 	URL("http://www.comuniecitta.it/torino/elenco-ospedali-di-torino.html")
	connection = url.open()
	doc = Document( connection.read() )
	items = doc.by_class('ulamm')[1:]
	row = []
	for ul in items:
		li = ul.by_tag('li')
		kind = plaintext(ul.previous.content)
		for el in li:
			if el != None:
				save = "%s, %s \n" % ( plaintext(el.content).replace('\n', ','), kind, )
				row.append(save)
	table.append( row )
		
	table.save("files/h_torino.txt")
Example #6
0
def post_tweet(tweet):
    from pattern.web import URL, Twitter
    import json

    url = URL("https://api.twitter.com/1.1/statuses/update.json",
              method="post",
              query={"status": tweet})

    twitter = Twitter(license=ccpattern)
    url = twitter._authenticate(url)

    try:
        # Send the post request.
        data = url.open().read()
    except Exception as e:
        print e
        print e.src
        print e.src.read()
        return None

    data = json.loads(data)
    return int(data[u'id'])
Example #7
0
tweet = "avalancheddar"

# The API for posting is described here:
# # https://dev.twitter.com/rest/reference/post/statuses/update
url = URL("https://api.twitter.com/1.1/statuses/update.json",
          method="post",
          query={"status": tweet})

# We'll use the Twitter._authenticate() method to authenticate ourselves
# as @ccpattern (so the new tweet will appear on @ccpattern's page):
twitter = Twitter(license=ccpattern)
url = twitter._authenticate(url)

try:
    # Send the post request.
    url.open()
except Exception as e:
    print e
    print e.src
    print e.src.read()

# To create your own Twitter bot:

# 1) You need a new e-mail address for the bot (e.g., gmail.com).
# 2) You need a new Twitter account.
# 3) Verify the Twitter account from the e-mail they send you.
# 4) Verify the Twitter account with a mobile phone number (this is mandatory):
#   https://support.twitter.com/articles/110250-adding-your-phone-number-to-your-account
# 5) While logged in in the new account, create a Twitter App:
#   https://apps.twitter.com/app/new
# 6) Modify the app's permissions to "read & write".
Example #8
0
# Tweet to post:
tweet = "avalancheddar"

# The API for posting is described here:
# # https://dev.twitter.com/rest/reference/post/statuses/update
url = URL("https://api.twitter.com/1.1/statuses/update.json", method="post", query={"status": tweet})

# We'll use the Twitter._authenticate() method to authenticate ourselves 
# as @ccpattern (so the new tweet will appear on @ccpattern's page):
twitter = Twitter(license=ccpattern)
url = twitter._authenticate(url)

try:
    # Send the post request.
    url.open()
except Exception as e:
    print e
    print e.src
    print e.src.read()
    
# To create your own Twitter bot:

# 1) You need a new e-mail address for the bot (e.g., gmail.com).
# 2) You need a new Twitter account.
# 3) Verify the Twitter account from the e-mail they send you.
# 4) Verify the Twitter account with a mobile phone number (this is mandatory): 
#   https://support.twitter.com/articles/110250-adding-your-phone-number-to-your-account
# 5) While logged in in the new account, create a Twitter App:
#   https://apps.twitter.com/app/new
# 6) Modify the app's permissions to "read & write".
Example #9
0
        print e.src
        print e.src.read()
        return {}, {}
    return bot_replies, user_replies


if __name__ == '__main__':
    import json
    from pattern.web import URL, Twitter

    # Tweet to post:
    tweet = "test tweet"

    url = URL("https://api.twitter.com/1.1/statuses/update.json",
              method="post",
              query={"status": tweet})

    twitter = Twitter(license=ccpattern)

    url = twitter._authenticate(url)

    try:
        # Send the post request.
        a = json.loads(url.open().read())
        reply_id = a["id"]
        print reply_id
    except Exception as e:
        print e
        print e.src
        print e.src.read()