Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions NewsReader.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# -*- coding: utf-8 -*-
"""
Created on Tue Jul 25 19:30:12 2017

@author: Sarvaswa
"""
## Script to run updateNews.py

## Import required packages
import os
import csv
from subprocess import Popen, PIPE

## Function to read key from file
def read_key():

with open('key.csv', 'r') as f:
reader = csv.reader(f)
for r in reader:
key = ''.join(r)
return key

if __name__ == '__main__':

## Detect Platform
platform = os.sys.platform.lower()

## Obtain News-API Key
if not os.path.isfile('key.csv'):
print('No Keys Found. Run setup_system.py first to add new key.')
else:
api_key = read_key()

## Run News Reader based on Plaform
if platform[:3]=='win':
print('Windows Detected')
if (not os.path.isfile('commands.bat')):
print('No Commands File Found. Run setup_system.py first to generate commands.')
else:
p = Popen('commands.bat', stdin=PIPE, stdout=PIPE,
stderr=PIPE, shell=True)
stdout, stderr = p.communicate()
print(stdout.decode('cp1252', errors='replace'))
elif platform == 'darwin':
print('Mac OSX Detected')
os.system('python newsUpdate.py ' + api_key)
5 changes: 4 additions & 1 deletion README.md → README_MacOSX.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@

![Alt text](/Images/screenshot.png?raw=true "Screenshot")

Usage:
# Requirements
Progressbar

# Usage:
```python newsUpdate.py <API_key>```

You can get API_key from https://newsapi.org/register
Expand Down
16 changes: 16 additions & 0 deletions README_Windows.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# News Reader for Windows

## It extracts news from all sources of your interest and speaks out the headline. It also displays the headline along with link and descrition on terminal, so that you can explore further.

# Requirements
* Python 3 (Tested on Python 3.5)
* Pyttsx3 (install using ```pip install pyttsx3```)
* Progressbar (install using ```pip install progressbar```)

**NOTE:** If errors occur during progressbar installation, download .whl file for progressbar from Christoph Gohlke's Repository of Windows Binaries for Python at http://www.lfd.uci.edu/~gohlke/pythonlibs/
Install using ```pip install <PATH-TO-FILE>```

# Usage
* Register yourself at https://newsapi.org/register and generate your API-key
* Run setup_system.py from command line using ```python setup_system.py``` and follow the instructions. This needs to be done once during setup.
* Run NewsReader.py from command line by entering ```python NewsReader.py``` to listen to news updates.
66 changes: 42 additions & 24 deletions newsUpdate.py
Original file line number Diff line number Diff line change
@@ -1,35 +1,43 @@
# -*- coding: utf-8 -*-
#oggpnosn
#hkhr

## Import Print Function for backward compatibility
from __future__ import print_function

## Import Required Libraries
import os
import requests
from subprocess import call
from time import sleep
import progressbar


API_URL = "https://newsapi.org/v1/articles?source=%s&sortBy=top&apiKey=%s"
SOURCES_OF_INTEREST = ["the-times-of-india", "the-hindu", "bbc-news", "cnn",
"google-news", "the-economist", "financial-times",
"business-insider", "national-geographic", "hacker-news",
"ars-technica", "techcrunch", "the-verge"]



def say(text):
"""it converts text to speech."""
call(["say", "-v", "Veena", '"%s"'%(text)])


def speakItOut(news):
"""it speaks out news section wise using mac specific "say"."""
## Function to convert text to speech on Supported Platforms
def say(text, platform_id):
"""it converts text to speech on supported platforms"""
if platform_id == 1:
call(["say", "-v", "Veena", '"%s"'%(text)])

elif platform_id == 2:
engine = pyttsx3.init()
engine.setProperty('voice', engine.getProperty('voices')[1])
engine.say(text)
engine.runAndWait()

def speakItOut(news, platform_id):
"""it speaks out news section wise"""
for source_of_interest in SOURCES_OF_INTEREST[::-1]:
say("News from " + source_of_interest)
say("News from " + source_of_interest, platform_id)
headlines = news[source_of_interest]
for headline in headlines[::-1]:
say(headline)
say(headline, platform_id)
sleep(1)


def getAPIContent(api_key):
"""it pings all the sources of interest and gets the news."""
news = {}
Expand All @@ -52,24 +60,34 @@ def getHeadlines(news):
headlines[source_of_interest] = headlinesOfSource
return headlines


def showNewsOnTerminal(news):
"""it shows headline and link on terminal for user to click."""
for source_of_interest in SOURCES_OF_INTEREST:
print "-----------------------------------" + source_of_interest + "\n"
print("-----------------------------------" + source_of_interest + "\n")
articles = news[source_of_interest]
for article in articles:
print "------"
print "\033[1m" + article["title"] + "\033[0m"
print article["url"]
print article["description"]


print("------")
print("\033[1m" + article["title"] + "\033[0m")
print(article["url"])
print(article["description"])

if __name__ == "__main__":
if len(os.sys.argv) == 2:

supported_platforms = ['win32','win64','darwin']
platform_id = -1
platform = os.sys.platform.lower()
if platform in supported_platforms:
# if len(os.sys.argv) == 2:
if platform == 'darwin':
from subprocess import call
platform_id = 1
else:
import pyttsx3
platform_id = 2
api_key = os.sys.argv[1]
news = getAPIContent(api_key)
showNewsOnTerminal(news)
headlines = getHeadlines(news)
speakItOut(headlines)
speakItOut(headlines, platform_id)
else:
print('Unsupported Platform')
1 change: 0 additions & 1 deletion requirements.txt

This file was deleted.

62 changes: 62 additions & 0 deletions setup_system.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
# -*- coding: utf-8 -*-
"""
Created on Tue Jul 25 20:18:52 2017

@author: Sarvaswa
"""

## Setup File

## Import required packages
import os
import csv

## Function to generate commands file
def gen_commands(key):

commands = ['chcp 65001',
'python3 newsUpdate.py ' + key]
numCommands = len(commands)

with open('commands.bat', 'w') as f:
for i,command in enumerate(commands,1):
if i == numCommands:
f.write(command)
else:
f.write(command + '\n')

## Function to store key to file
def add_key():

key = input('Enter News-API key: ')
with open('key.csv', 'w', newline='') as f:
writer = csv.writer(f)
writer.writerow(key)

print('New Key Added')
gen_commands(key)
return key

## Function to read key from file
def read_key():

with open('key.csv', 'r') as f:
reader = csv.reader(f)
for r in reader:
key = ''.join(r)
return key

if __name__ == '__main__':

## Obtain News-API Key
if not os.path.isfile('key.csv'):
api_key = add_key()
else:
api_key = read_key()

## Generate Commands File for Windows
platform = os.sys.platform.lower()
if (not os.path.isfile('commands.bat')) and (platform[:3] == 'win'):
gen_commands(api_key)

print('All Set. Run NewsReader.py to listen to latest news updates!')