diff --git a/README.md b/README.md index 2841b71..62f6e31 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,48 @@ # News API SDK for Python -Coming soon... this is where our officially supported SDK for Python is going to live. -*** +## Installation +Download this repository on your machine, then open a terminal in the folder. To install type: +```bash +python setup.py install +``` +(please note that sudo may be required) +To check the installation, open a terminal and type: +```bash +python +``` +then, try to import the package: +```python + from newsapi import newsapi +``` +If no error appears, the package have been succesfully installed. -## Developers... we need you! -We need some help fleshing out this repo. If you're a Python dev with experience building PyPI-compatible libraries and web API wrappers, we're offering a reward of $250 to help us get started. For more info please email support@newsapi.org, or dive right in and send us a pull request. +##Example +```python +from newsapi import newsapi + +apiKey = "2809dbe4cfe044708cacd93879af483e" + +#Get the list of sources +newsapi.getSources(apiKey = apiKey) +#get the list of sources in italian +newsapi.getSources(apiKey = apiKey, language="it") +#get the list of sources from germany +newsapi.getSources(apiKey = apiKey,country="de") + + +#Get all articles about Bitcoin +newsapi.getEverything(apiKey = apiKey,q="Bitcoin") +#All articles mentioning Apple from yesterday, sorted by popular publishers first +newsapi.getEverything(apiKey = apiKey,q="Apple",dateFrom="2017-12-10",sortBy="popularity") +#All articles published by the WSJ and NY Times +newsapi.getEverything(apiKey,source="the-wall-street-journal,the-new-york-times") + +#Top headlines from BBC News +newsapi.getTopHeadlines(apiKey=apiKey,source="bbc-news") +#Top headlines from The Next Web and The Verge +newsapi.getTopHeadlines(apiKey=apiKey,source="the-next-web,the-verge") +#Top headlines about Trump +newsapi.getTopHeadlines(apiKey=apiKey,q="Trump") +#Top headlines from business sources in English +newsapi.getTopHeadlines(apiKey=apiKey,category="business",language="en") +``` diff --git a/example.py b/example.py new file mode 100644 index 0000000..58bea73 --- /dev/null +++ b/example.py @@ -0,0 +1,27 @@ +from newsapi import newsapi + +apiKey = "2809dbe4cfe044708cacd93879af483e" + +#Get the list of sources +newsapi.getSources(apiKey = apiKey) +#get the list of sources in italian +newsapi.getSources(apiKey = apiKey, language="it") +#get the list of sources from germany +newsapi.getSources(apiKey = apiKey,country="de") + + +#Get all articles about Bitcoin +newsapi.getEverything(apiKey = apiKey,q="Bitcoin") +#All articles mentioning Apple from yesterday, sorted by popular publishers first +newsapi.getEverything(apiKey = apiKey,q="Apple",dateFrom="2017-12-10",sortBy="popularity") +#All articles published by the WSJ and NY Times +newsapi.getEverything(apiKey,source="the-wall-street-journal,the-new-york-times") + +#Top headlines from BBC News +newsapi.getTopHeadlines(apiKey=apiKey,source="bbc-news") +#Top headlines from The Next Web and The Verge +newsapi.getTopHeadlines(apiKey=apiKey,source="the-next-web,the-verge") +#Top headlines about Trump +newsapi.getTopHeadlines(apiKey=apiKey,q="Trump") +#Top headlines from business sources in English +newsapi.getTopHeadlines(apiKey=apiKey,category="business",language="en") \ No newline at end of file diff --git a/newsapi/__init__,py b/newsapi/__init__,py new file mode 100644 index 0000000..77d37bc --- /dev/null +++ b/newsapi/__init__,py @@ -0,0 +1,3 @@ +from . import newsapi + +__version__ = '0.0.1.1' #Version Contro0 diff --git a/newsapi/newsapi.py b/newsapi/newsapi.py new file mode 100644 index 0000000..029eee0 --- /dev/null +++ b/newsapi/newsapi.py @@ -0,0 +1,176 @@ +# -*- coding: utf-8 -*- +""" +Created on Mon Dec 11 10:15:29 2017 + +@author: giulio +""" + +############################################################################### +# # +# Libraries # +# # +############################################################################### + +import os #used for reading files and directories +import datetime #Check if dates are correct +import requests +import warnings + +############################################################################### +# # +# FUNCTIONS # +# # +############################################################################### +""" +Here you can change the paramaters used in this script + +""" + +def checkDate(myDate): + """ This function checks if an input date is real or not + Input = date as string YYYY-MM-DD + Output = True or False + """ + + y,m,d = myDate.split("-") + try: + datetime.datetime(int(y), int(m), int(d)) + return(True) + except: + return(False) + +def checkLanguage(language): + """ This function checks if selected language is supported or not + Input = language code + Output = True or False + """ + languages = ["ar","en","cn","de","es","fr","he","it","nl","no","pt","ru","sv","ud"] + if language in languages: + return(True) + else: + warnings.warn("Unsupported language, skipped") + return(False) + +def checkCountry(country): + """ This function checks if selected country is supported or not + Input = country code + Output = True or False + """ + countries = ["ar","au","br","ca","cn","de","es","fr","gb","hk","ie","in","is","it","nl","no","pk","ru","sa","sv","us","z"] + if country in countries: + return(True) + else: + warnings.warn("Unsupported country, skipped") + return(False) + +def checkCategory(category): + """ This function checks if selected category is supported or not + Input = category label + Output = True or False + """ + categories = ["business","entertainment","gaming","general","health-and-medical","music","politics","science-and-nature","sport","technology"] + if category in categories: + return(True) + else: + warnings.warn("Unsupported category, skipped") + return(False) + +def checkSorting(sortBy): + """ This function checks if selected sorting is supported or not + Input = sorting label + Output = True or False + """ + sortBypossibilities = ["relevancy", "popularity", "publishedAt"] + if(sortBy in sortBypossibilities): + return(True) + else: + warnings.warn("Unsupported sorting, skipped") + return(False) + +def getTopHeadlines(apiKey, q="", source="",language="",country="",category=""): + """ This functions gets the top headlines + Input: + apiKey = your News API key + q = query to search for + source = the source to scrap the data from, if not specified all sources are reported. This can be a string with one or more comma-separated sources + language = add a filter by language. Suported languages are ["ar","en","cn","de","es","fr","he","it","nl","no","pt","ru","sv","ud"] + country = add a filter by country. Supported countries are ["ar","au","br","ca","cn","de","es","fr","gb","hk","ie","in","is","it","nl","no","pk","ru","sa","sv","us","z"] + category = add a filter by category. Supported categories are ["busines","entertainment","gaming","general","health-and-medical","music","politics","science-and-nature","sport","technology"] + """ + + thisURL = "https://newsapi.org/v2/top-headlines?apiKey="+apiKey + if(q != ""): + thisURL += "&q="+q + if(source != ""): + #if a source have been specified + thisURL += "&sources="+source + if(country != "" and checkCountry(country)): #if a country have been specified + thisURL += "&country="+country + if(language != "" and checkLanguage(language)): #if a language have been specified + thisURL += "&language="+language + if(category != "" and checkCategory(category)): #if a category have been specified + thisURL += "&category="+category + + response = requests.get(thisURL) + response = response.json() + if(response["status"] == "ok"): #if ok + return(response["articles"]) + else: #else return the status code + return(response["status"]) + +def getEverything(apiKey,q="",sortBy="",source="",language="",dateFrom="",dateTo="",page=1): + thisURL = "https://newsapi.org/v2/everything?apiKey="+apiKey + if(q != ""): + thisURL += "&q="+q + if(source != ""):#if a source have been specified + thisURL += "&sources="+source + if(language != ""): #if a language have been specified + thisURL += "&language="+language + if(page != 1): #if a page number have been specified + thisURL += "&page="+page + if(sortBy != "" and checkSorting(sortBy)): + thisURL += "&sortBy="+sortBy + + if(dateFrom != ""): #if a dateFrom have been specified + if(checkDate(dateFrom)): #and is valid + thisURL += "&from="+dateFrom + else: + warnings.warn("Incorrect dateFrom format, skipped") + + if(dateTo != ""): #if a dateTo have been specified + if(checkDate(dateTo)): #and is valid + thisURL += "&to="+dateTo + else: + warnings.warn("Incorrect dateTo format, skipped") + + response = requests.get(thisURL) + response = response.json() + + if(response["status"] == "ok"): + return(response["articles"]) + else: + return(response["status"]) + +def getSources(apiKey,language="",country=""): + thisURL = "https://newsapi.org/v2/sources?apiKey="+apiKey + if(language != ""): #if a language have been specified + thisURL += "&language="+language + if(country != ""): #if a country have been specified + thisURL += "&country="+country + response = requests.get(thisURL) + response = response.json() + if(response["status"] == "ok"): + return(response) + +############################################################################### +# # +# DEBUG # +# # +############################################################################### +""" +FOR DEBUG PURPOSES +""" + +if(__name__ == "__main__"): + apiKey = "2809dbe4cfe044708cacd93879af483e" + print("Hello debugger") diff --git a/setup.py b/setup.py new file mode 100644 index 0000000..fa365e4 --- /dev/null +++ b/setup.py @@ -0,0 +1,17 @@ +from setuptools import setup + +setup(name='newsapi', + version='0.0.1', + description='News - API Python SDK ', + url='https://github.com/Gabrock94/News-API-python', + author='Giulio Gabrieli', + author_email='gack94@gmail.com', + license='MIT', + packages=['newsapi'], + install_requires=[ + 'os', + 'datetime', + 'requests' + ], + zip_safe=False) +