-
Notifications
You must be signed in to change notification settings - Fork 10
/
coins.py
104 lines (79 loc) · 3.59 KB
/
coins.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
# Shows crypto currency + how many times its been mentioned on twitter within 24hrs + trade symbol +
# percentage of change in mentions + sentiment from twitter by breaking each sentence the coin has been
# mentioned in into individual words then rating the words using a lexicon of words and feelings a score
# has been assigned from -1 to 1, then based on the score a rating of POSITIVE or NEGITIVE is given
# using a threshold. Results are saved in a txt file and printed to terminal
# RUN: python3 coins.py
# OUTPUT: terminal + output.txt
# http://NimbusCapital.Ltd
# @NimbusCapital
# Usefull for adding to trade strategies using internet sentiment and tweet mentions can help spot,
# ie: Pump And Dump
#
import codecs
from bs4 import BeautifulSoup
import requests
import tweepy
from textblob import TextBlob
import sys
import csv
from fake_useragent import UserAgent
#Authenticate / Digital login to twitter
# USE YOUR OWN TWITTER TOKENS PLEASE NOT MINE
# http://apps.twitter.com TO REGISTER FOR TOKEN DONT USE MINE !
consumer_key= ''
consumer_secret= ''
access_token=''
access_token_secret=''
# We set the above varibles to our API keys from TWITTER
# Below we use the TWEEYP libary we imported to auth to twitter API
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
api = tweepy.API(auth)
# We set up a varible containing a url of a API i reversed for a trading platform using HTTP HEADERS
url = 'http://cryptrader.com/W/tweets' # They try stop us using it so we spoof our user agent below
#Picking random useragent / telling user and setting below to use
print("Grabing a random useragent from random useragent API....")
# get a random user agent
randuserAgent = UserAgent(verify_ssl=False).random
print(randuserAgent)#
#
headers = {'User-Agent': randuserAgent} # spoof user agent to stop the block
page = requests.get(url, headers=headers) # grab the page / html
#print(page.content)
#print(page.status_code)
soup = BeautifulSoup(page.content, 'html.parser') # use BEAUTIFULSOUP libary we imported to pass the HTML
tabulka = soup.find("table", {"class" : "data-table"}) # use beautifulsoup libary to get table from the "api"
records = [] # store all of the records in this list
for row in tabulka.findAll('tr'): # we are reversing the "API" basicly from cryptrader here to get "list of altcoins"
col = row.findAll('td')
name = col[0].string.strip()
symbol = col[1].string.strip()
tweetsLastHour = col[2].string.strip()
try:
change = col[3].string.strip()
except:
change = "NULL"
#check for tiwtter sendiment
#Search for tweets
public_tweets = api.search("#" + name)# we use tweepy libary again to search for HASHTAG + NAME
#Sentiment
for tweet in public_tweets: # for every tweet we find mentioned...
text = tweet.text
cleanedtext = text
analysis = TextBlob(cleanedtext) # break it into single words
sentiment = analysis.sentiment.polarity # work out sentiment
if sentiment >= 0: # give it english
polarity = 'Positive'
else:
polarity = 'Negative'
#print(cleanedtext, polarity)
record = '%s|%s|%s|%s|%s' % (name, symbol, tweetsLastHour,change,polarity) # get string ready for output file
records.append(record)
print(name + "|" + symbol + "|" + tweetsLastHour + "|" + change + "|" + polarity) # print to screen !!
fl = codecs.open('output.txt', 'wb', 'utf8') #store to output file
line = ';'.join(records)
fl.write(line + u'\r\n')
fl.close() #end store to output file
# FIN - Scott