def collectNews(company, timeBegin, timeEnd): #change the key for the API in here. This is the AlchemyDataNews KEY = '2190f450728492113ce4e5b880a72eefbea73308' alchemy_data_news = AlchemyDataNewsV1(api_key=KEY) #timeBegin = str(time.mktime(datetime.datetime.strptime(timeBegin, "%d/%m/%Y").timetuple())).split(".")[0] #timeEnd = str(time.mktime(datetime.datetime.strptime(timeEnd, "%d/%m/%Y").timetuple())).split(".")[0] #print(timeBegin) #print(timeEnd) #timeBegin ='now-60d' #timeEnd = 'now' #results = alchemy_data_news.get_news_documents(start='now-60d', end='now', time_slice='12h') # print(json.dumps(results, indent=2)) company_query = '|text=' + company + ',type=company|' results = alchemy_data_news.get_news_documents( start='now-60d', end='now', return_fields=[ 'enriched.url.title', #'enriched.url.url', #'enriched.url.author', #'enriched.url.publicationDate', 'enriched.url.entities.entity.sentiment.type', #'enriched.url.keywords.keyword.sentiment.mixed' 'enriched.url.entities.entity.sentiment.score' ], query_fields={ 'q.enriched.url.enrichedTitle.entities.entity': company_query }) r = json.dumps(results, indent=2) return r
def predictionSentiment(company): """ Get the news from the most recent 2 days :param company: :return: """ #change the key for the API in here. This is the AlchemyDataNews KEY = '2190f450728492113ce4e5b880a72eefbea73308' alchemy_data_news = AlchemyDataNewsV1(api_key=KEY) timeBegin = 'now-2d' timeEnd = 'now' company_query = '|text=' + company + ',type=company|' results = alchemy_data_news.get_news_documents( start=timeBegin, end=timeEnd, return_fields=[ 'enriched.url.title', 'enriched.url.entities.entity.sentiment.type', 'enriched.url.entities.entity.sentiment.score' ], query_fields={ 'q.enriched.url.enrichedTitle.entities.entity': company_query }) r = json.dumps(results, indent=2) f = open("/home/kid/Github/Oracle/watson/jsonp2.json", 'w') f.write(str(r))
def __init__(self, config): self.config = config self.alchemy_language = watson_developer_cloud.AlchemyLanguageV1( api_key=self.config['api_key']) self.alchemy_data_news = AlchemyDataNewsV1( api_key=self.config['api_key']) self.tone_analyzer = ToneAnalyzerV3(version='2016-05-19 ', username=self.config['username'], password=self.config['password'])
def __init__(self): self.alchemy_language = AlchemyLanguageV1(api_key=self.api_key) self.alchemy_data_news = AlchemyDataNewsV1(api_key=self.api_key) # self.tone_analyzer = ToneAnalyzerV3(version='2016-05-19 ', username=self.config['username'], password=self.config['password']) self.client = MongoClient('localhost', 27017) self.database = self.client[self.database_name] self.news_source_article_data = self.database[ 'news_source_article_data'] self.historic_political_article_data = self.database[ 'historic_political_article_data']
def search_from_keywords(keywords): from watson_developer_cloud import AlchemyDataNewsV1 import json from API_Key import * from Keyword_input import extract_keywords alchemy_data_news = AlchemyDataNewsV1(api_key = api_key_chosen) # keywords is a dicationary of lists # {relations : [], negations : [], dates : []} # relations is a list [(list subjects, list of objects, relation)]. # List of subjects can be ['president','Trump'] or just ['member'] # negations and dates can be empty. Else : date = [(datetime, original text)] relations = keywords[0] negations = keywords[1] dates = keywords[2] interesting_keywords = [] for intermediate_list in list(keywords['subjects'].values()): interesting_keywords += intermediate_list for word in keywords['objects']: if word not in interesting_keywords: interesting_keywords.append(word) tts='A[' for keyword in interesting_keywords: tts += keyword+'^' tts = tts[:-1]+']' results = alchemy_data_news.get_news_documents( start='now-1d', end='now', return_fields=['enriched.url.title', 'enriched.url.url', 'enriched.url.text', 'enriched.url.author', 'enriched.url.publicationDate'], query_fields={'q.enriched.url.text': tts}) #print(json.dumps(results, indent=2)) text_list = [doc['source']['enriched']['url']['text'] for doc in results['result']['docs']] keywords_list = [extract_keywords(text) for text in text_list] return keywords_list
def __init__(self, arg): self.application_state = dict() self.application_state.update({"call_count": 0}) self.application_state.update({"task_log": "parser init"}) self.arg = arg self.alchemy_data_news = AlchemyDataNewsV1(api_key=self.arg["key"]) self.news_doc_responds = list() self.next = 'None' self.url_base_url = "https://access.alchemyapi.com/calls/data/GetNews?" self.url_key = "apikey=" self.url_returns = "&return=" self.retrun_fields_default = "enriched.url.title,enriched.url.url,enriched.url.enrichedTitle.entities,enriched.url.enrichedTitle.docSentiment,enriched.url.enrichedTitle.concepts" self.url_start_date = "&start=" self.url_end_date = "&end=" self.url_q_field = "&q." self.q_field_default = "enriched.url.cleanedTitle=" self.url_count = "&count=" self.url_outputMode = "&outputMode=json" """alternatively, use requests library to call the api directly and
def searchNews(self, text): #4e9a752f7bc8c85ee8a88f441b7ddb24db2c39c0 #a4fee8f87cfbfe973fa8cfd3454e91d6f5cd8e8c #63af3e40799acb3b2819f59130b03db239a8767a #16bc58b14b5ee52a14d817475173f347333f5a0e #4e1cb9a6ee6aa63df294081a617e4badcc9b5d33 alchemy_data_news = AlchemyDataNewsV1( api_key='4e1cb9a6ee6aa63df294081a617e4badcc9b5d33') ''' results = alchemy_data_news.get_news_documents(start='now-1h', end='now', time_slice='1h') print(json.dumps(results, indent=2)) ''' results = alchemy_data_news.get_news_documents( start='1453334400', end='1455444500', max_results=10, return_fields=[ 'enriched.url.title', 'enriched.url.url', 'enriched.url.author', 'enriched.url.publicationDate' ], query_fields={ 'q.enriched.url.enrichedTitle.entities.entity': '|text={},type=company|'.format(text) }) #print(json.dumps(results)) temp = json.dumps(results) _dict = ast.literal_eval(temp) res = _dict['result']['docs'] print(res) print("sdfsdfsdfs") infolist = [] for i in range(0, len(res)): info = {} link = res[i]['source']['enriched']['url']['url'] title = res[i]['source']['enriched']['url']['title'] info['title'] = title info['url'] = link infolist.append(info) print(infolist) print('AlchemyData finish') return infolist
from __future__ import print_function import json from watson_developer_cloud import AlchemyDataNewsV1 alchemy_data_news = AlchemyDataNewsV1(api_key='YOUR API KEY') results = alchemy_data_news.get_news_documents(start='now-7d', end='now', time_slice='12h') print(json.dumps(results, indent=2)) results = alchemy_data_news.get_news_documents( start='1453334400', end='1454022000', return_fields=['enriched.url.title', 'enriched.url.url', 'enriched.url.author', 'enriched.url.publicationDate'], query_fields={ 'q.enriched.url.enrichedTitle.entities.entity': '|text=IBM,type=company|'}) print(json.dumps(results, indent=2))
app = Flask(__name__) app.config['SQLALCHEMY_DATABASE_URI'] = DATABASE_URL db = SQLAlchemy(app) CORS(app) from Author import Author @app.route("/author/<author_name>/taxonomy/<taxonomy>") def api(author_name, taxonomy): return json.dumps(rate(author_name, taxonomy)) alchemy_language = AlchemyLanguageV1(api_key=API_KEY) alchemy_data_news = AlchemyDataNewsV1(api_key=API_KEY) personality_insights = PersonalityInsightsV3(version='2016-10-20', username=API_USERNAME, password=API_PASSWORD) @app.route("/author/list") def list_authors(): cache = CachedAuthor.query.all() cache = [{ 'name': author.name, 'objectivity': author.objectivity } for author in cache] ret = {"status": "OK", "result": cache} return json.dumps(ret)
import json from watson_developer_cloud import AlchemyDataNewsV1 alchemy_data_news = AlchemyDataNewsV1( api_key='c851400276c1acbd020210847f8677e6d1577c26') results = alchemy_data_news.get_news_documents(start='now-7d', end='now', time_slice='12h') print(json.dumps(results, indent=2)) results = alchemy_data_news.get_news_documents( start='1453334400', end='1454022000', return_fields=[ 'enriched.url.title', 'enriched.url.url', 'enriched.url.author', 'enriched.url.publicationDate' ], query_fields={ 'q.enriched.url.enrichedTitle.entities.entity': '|text=IBM,type=company|' }) print(json.dumps(results, indent=2))
import json import sys from watson_developer_cloud import AlchemyDataNewsV1 alchemy_data_news = AlchemyDataNewsV1( api_key='2190f450728492113ce4e5b880a72eefbea73308') #results = alchemy_data_news.get_news_documents(start='now-7d', end='now') #print(json.dumps(results, indent=2)) results = alchemy_data_news.get_news_documents( start='now-60d', end='now', return_fields=[ 'enriched.url.title', #'enriched.url.url', #'enriched.url.author', 'enriched.url.publicationDate' ], query_fields={ 'q.enriched.url.enrichedTitle.entities.entity': '|text=AAPL,type=company|' }) print(json.dumps(results, indent=2)) sys.exit(0) f = open("/home/kid/Github/Oracle/watson/test.json", 'w') f.write(str(results))