Get data and send mail scriptPython compress and sendData cleansing and formatting scriptGet all followers...

Can I sign legal documents with a smiley face?

MAXDOP Settings for SQL Server 2014

Is '大勢の人' redundant?

Can not upgrade Kali,not enough space in /var/cache/apt/archives

What is the difference between "Do you interest" and "...interested in" something?

Is it better practice to read straight from sheet music rather than memorize it?

On a tidally locked planet, would time be quantized?

Have I saved too much for retirement so far?

Can somebody explain Brexit in a few child-proof sentences?

Melting point of aspirin, contradicting sources

Python script not running correctly when launched with crontab

Interest Rate Futures Question from Hull, 8e

Question about alert, surprise, and crit failing

Can a Necromancer Reuse the corpses left behind from slain Undead?

Why did the HMS Bounty go back to a time when whales are already rare?

Why does the Sun have different day lengths, but not the gas giants?

Biological Blimps: Propulsion

Why did the EU agree to delay the Brexit deadline?

Difference between -| and |- in TikZ

Is it improper etiquette to ask your opponent what his/her rating is before the game?

Count the occurrence of each unique word in the file

How do I nest cases?

Could the E-bike drivetrain wear down till needing replacement after 400 km?

How can "mimic phobia" be cured or prevented?



Get data and send mail script


Python compress and sendData cleansing and formatting scriptGet all followers and friends of a Twitter userSend emails with data from spreadsheet filesTwitter streamer that stores data in mongodb and emails errorsGet nearest driver from 2.5 millions of data using mongodbPHP script to generate invoice and send notificationGet 10-day forecast scriptConstruct and send an HTTP get request from scratch and print all the received data to the screenNode.js Data-Completion script













4












$begingroup$


I wrote a script using Python that gets data from several sources (news sites, Twitter, Yahoo), puts it into a dict and then formats it as a string to be sent through email.



I wonder if there is possibility to write the code more neatly in a shorter way. Maybe I'm doing some steps that are a bit unnecessary and maybe faster and I could do it differently, but not sure how.



import json
import urllib.request
from twitter import Twitter, OAuth, TwitterHTTPError, TwitterStream
from bs4 import BeautifulSoup
import requests
import datetime
from pymongo import MongoClient
from email.mime.text import MIMEText
import smtplib
import os
#from config import *

now = datetime.datetime.now()
api_news= os.environ["api_news"]

#twitter setup
ACCESS_TOKEN = os.environ["ACCESS_TOKEN"]
ACCESS_SECRET = os.environ["ACCESS_SECRET"]
CONSUMER_KEY = os.environ["CONSUMER_KEY"]
CONSUMER_SECRET = os.environ["CONSUMER_SECRET"]
oauth = OAuth(ACCESS_TOKEN, ACCESS_SECRET, CONSUMER_KEY, CONSUMER_SECRET)
twitter = Twitter(auth=oauth)

#polish
pol_trends = twitter.trends.place(_id = 23424923)
twittrendlistPL=[]
for i in pol_trends[0]['trends']:
twittrendlistPL.append(i['name'])
strPLT="<br>".join(str(x) for x in twittrendlistPL[0:15])

#global trends
globaltrends=twitter.trends.place(_id = 1)
twittrendlist=[]
for i in globaltrends[0]['trends']:
twittrendlist.append(i['name'])
def isEnglish(s):
try:
s.encode(encoding='utf-8').decode('ascii')
except UnicodeDecodeError:
return False
else:
return True
G=[i for i in twittrendlist if isEnglish(i)]
strGT="<br>".join(str(x) for x in G[0:15])

#us headlines
url = ('https://newsapi.org/v2/top-headlines?'
'country=us&'+api_news)
response = requests.get(url)
listus=[]
for i in range(len(response.json()['articles'])):
listus.append(response.json()['articles'][i]['title'])
listus.append(response.json()['articles'][i]['url'])
strNUS="<br>".join(str(x) for x in listus[0:10])


#uk headlines
url = ('https://newsapi.org/v2/top-headlines?country=gb&'+api_news)
response = requests.get(url)
listGB=[]
for i in range(len(response.json()['articles'])):
listGB.append(response.json()['articles'][i]['title'])
listGB.append(response.json()['articles'][i]['url'])
strGB="<br>".join(str(x) for x in listGB[0:10])


#google news(global) headlines
url = ("https://newsapi.org/v2/top-headlines?sources=google-news&"+api_news)
response = requests.get(url)
listg=[]
for i in range(len(response.json()['articles'])):
listg.append(response.json()['articles'][i]['title'])
listg.append(response.json()['articles'][i]['url'])
strg="<br>".join(str(x) for x in listg[0:10])


#most popular from technology
url = ("https://newsapi.org/v2/top-headlines?category=technology&country=us&sortBy=popularity&"+api_news)

response = requests.get(url)
listt=[]
for i in range(len(response.json()['articles'])):
listt.append(response.json()['articles'][i]['title'])
listt.append(response.json()['articles'][i]['url'])
strt="<br>".join(str(x) for x in listt[0:10])

#yahoo trending charts
page = requests.get("https://finance.yahoo.com/trending-tickers/")
soup = BeautifulSoup(page.content, 'html.parser')
base=soup.findAll('td', {'class':'data-col1 Ta(start) Pstart(10px) Miw(180px)'})
yhoo=[]
for i in base:
yhoo.append(i.get_text())
strYHOO='<br>'.join(str(x) for x in yhoo[0:15])

#crypto trends to find
with urllib.request.urlopen("https://api.coinmarketcap.com/v2/ticker/") as url:
cmc = json.loads(url.read().decode())
names=[]
change=[]
for i in cmc['data']:
names.append(cmc['data'][i]['symbol'])
change.append(cmc['data'][i]['quotes']['USD']['percent_change_24h'])
change, names = zip(*sorted(zip(change, names)))
cmcstr='<br>'.join([str(a) + ': '+ str(b) + '%' for a,b in zip(names[-5:],change[-5:])])

#create a dict to upload for db
maind={
"Global Twitter trends": strGT,
"Polish Twitter trends" : strPLT,
"Top US headlines": strNUS,
"Top UK headlines": strGB,
"Top Google News headlines": strg,
"Top tech headlines": strt,
"Trending yahoo stocks": strYHOO,
"CMC trending": cmcstr,
"Date": str(datetime.date.today())
}

#create and connect to mongo database
mongo=os.environ["mongodb"]
try:
#local test
#conn = MongoClient()
#production
conn = MongoClient(mongo)
print("Connected successfully!!!")
except:
print("Could not connect to MongoDB")

#Create/conn database
db = conn.database

# Created or Switched to collection names: trends
collection = db.trends

# Insert Data
rec_id1 = collection.insert_one(maind)
print("Data inserted with record ids",rec_id1)

mpass=os.environ["mpass"]

record = collection.find_one({'Date': str(datetime.date.today())}) #create record that is from today
#convert all from database so that its easier to put into mail
gtt=record["Global Twitter trends"]
ptt=record["Polish Twitter trends"]
tus=record["Top US headlines"]
tuk=record["Top UK headlines"]
tgn=record["Top Google News headlines"]
tech=record["Top tech headlines"]
cmc=record["CMC trending"]
yahoo=record["Trending yahoo stocks"]
date=record["Date"]

#crate function to send mail
def send_email(date, *args):
#login data
from_email="" #sending mail
from_password=mpass
to_email="" #recipient

subject="Daily trends {0}".format(date)
message="Today's dose of news and trends starting with global twitter trends:<br> <strong>{0}</strong>. <br> <br> Polish twitter:<br> <strong>{1}</strong> <br> <br> Top us headlines:<br> <strong>{2}</strong> <br> <br> Top uk headlines:<br> <strong>{3}</strong> <br> <br> Top news headlines:<br> <strong>{4}</strong> <br> <br> Tech news:<br> <strong>{5}</strong> <br> <br> CMC trending:<br> <strong>{6}</strong> <br> <br> Yahoo trending:<br> <strong>{7}</strong> <br>".format(*args)
msg=MIMEText(message, 'html') #msg setup
msg['Subject']=subject
msg['To']=to_email
msg['From']=from_email
gmail=smtplib.SMTP('smtp.gmail.com', 587) #mail setup
gmail.ehlo()
gmail.starttls()
gmail.login(from_email, from_password)
gmail.send_message(msg)

send_email(date, gtt, ptt, tus, tuk, tgn, tech, cmc, yahoo)









share|improve this question











$endgroup$

















    4












    $begingroup$


    I wrote a script using Python that gets data from several sources (news sites, Twitter, Yahoo), puts it into a dict and then formats it as a string to be sent through email.



    I wonder if there is possibility to write the code more neatly in a shorter way. Maybe I'm doing some steps that are a bit unnecessary and maybe faster and I could do it differently, but not sure how.



    import json
    import urllib.request
    from twitter import Twitter, OAuth, TwitterHTTPError, TwitterStream
    from bs4 import BeautifulSoup
    import requests
    import datetime
    from pymongo import MongoClient
    from email.mime.text import MIMEText
    import smtplib
    import os
    #from config import *

    now = datetime.datetime.now()
    api_news= os.environ["api_news"]

    #twitter setup
    ACCESS_TOKEN = os.environ["ACCESS_TOKEN"]
    ACCESS_SECRET = os.environ["ACCESS_SECRET"]
    CONSUMER_KEY = os.environ["CONSUMER_KEY"]
    CONSUMER_SECRET = os.environ["CONSUMER_SECRET"]
    oauth = OAuth(ACCESS_TOKEN, ACCESS_SECRET, CONSUMER_KEY, CONSUMER_SECRET)
    twitter = Twitter(auth=oauth)

    #polish
    pol_trends = twitter.trends.place(_id = 23424923)
    twittrendlistPL=[]
    for i in pol_trends[0]['trends']:
    twittrendlistPL.append(i['name'])
    strPLT="<br>".join(str(x) for x in twittrendlistPL[0:15])

    #global trends
    globaltrends=twitter.trends.place(_id = 1)
    twittrendlist=[]
    for i in globaltrends[0]['trends']:
    twittrendlist.append(i['name'])
    def isEnglish(s):
    try:
    s.encode(encoding='utf-8').decode('ascii')
    except UnicodeDecodeError:
    return False
    else:
    return True
    G=[i for i in twittrendlist if isEnglish(i)]
    strGT="<br>".join(str(x) for x in G[0:15])

    #us headlines
    url = ('https://newsapi.org/v2/top-headlines?'
    'country=us&'+api_news)
    response = requests.get(url)
    listus=[]
    for i in range(len(response.json()['articles'])):
    listus.append(response.json()['articles'][i]['title'])
    listus.append(response.json()['articles'][i]['url'])
    strNUS="<br>".join(str(x) for x in listus[0:10])


    #uk headlines
    url = ('https://newsapi.org/v2/top-headlines?country=gb&'+api_news)
    response = requests.get(url)
    listGB=[]
    for i in range(len(response.json()['articles'])):
    listGB.append(response.json()['articles'][i]['title'])
    listGB.append(response.json()['articles'][i]['url'])
    strGB="<br>".join(str(x) for x in listGB[0:10])


    #google news(global) headlines
    url = ("https://newsapi.org/v2/top-headlines?sources=google-news&"+api_news)
    response = requests.get(url)
    listg=[]
    for i in range(len(response.json()['articles'])):
    listg.append(response.json()['articles'][i]['title'])
    listg.append(response.json()['articles'][i]['url'])
    strg="<br>".join(str(x) for x in listg[0:10])


    #most popular from technology
    url = ("https://newsapi.org/v2/top-headlines?category=technology&country=us&sortBy=popularity&"+api_news)

    response = requests.get(url)
    listt=[]
    for i in range(len(response.json()['articles'])):
    listt.append(response.json()['articles'][i]['title'])
    listt.append(response.json()['articles'][i]['url'])
    strt="<br>".join(str(x) for x in listt[0:10])

    #yahoo trending charts
    page = requests.get("https://finance.yahoo.com/trending-tickers/")
    soup = BeautifulSoup(page.content, 'html.parser')
    base=soup.findAll('td', {'class':'data-col1 Ta(start) Pstart(10px) Miw(180px)'})
    yhoo=[]
    for i in base:
    yhoo.append(i.get_text())
    strYHOO='<br>'.join(str(x) for x in yhoo[0:15])

    #crypto trends to find
    with urllib.request.urlopen("https://api.coinmarketcap.com/v2/ticker/") as url:
    cmc = json.loads(url.read().decode())
    names=[]
    change=[]
    for i in cmc['data']:
    names.append(cmc['data'][i]['symbol'])
    change.append(cmc['data'][i]['quotes']['USD']['percent_change_24h'])
    change, names = zip(*sorted(zip(change, names)))
    cmcstr='<br>'.join([str(a) + ': '+ str(b) + '%' for a,b in zip(names[-5:],change[-5:])])

    #create a dict to upload for db
    maind={
    "Global Twitter trends": strGT,
    "Polish Twitter trends" : strPLT,
    "Top US headlines": strNUS,
    "Top UK headlines": strGB,
    "Top Google News headlines": strg,
    "Top tech headlines": strt,
    "Trending yahoo stocks": strYHOO,
    "CMC trending": cmcstr,
    "Date": str(datetime.date.today())
    }

    #create and connect to mongo database
    mongo=os.environ["mongodb"]
    try:
    #local test
    #conn = MongoClient()
    #production
    conn = MongoClient(mongo)
    print("Connected successfully!!!")
    except:
    print("Could not connect to MongoDB")

    #Create/conn database
    db = conn.database

    # Created or Switched to collection names: trends
    collection = db.trends

    # Insert Data
    rec_id1 = collection.insert_one(maind)
    print("Data inserted with record ids",rec_id1)

    mpass=os.environ["mpass"]

    record = collection.find_one({'Date': str(datetime.date.today())}) #create record that is from today
    #convert all from database so that its easier to put into mail
    gtt=record["Global Twitter trends"]
    ptt=record["Polish Twitter trends"]
    tus=record["Top US headlines"]
    tuk=record["Top UK headlines"]
    tgn=record["Top Google News headlines"]
    tech=record["Top tech headlines"]
    cmc=record["CMC trending"]
    yahoo=record["Trending yahoo stocks"]
    date=record["Date"]

    #crate function to send mail
    def send_email(date, *args):
    #login data
    from_email="" #sending mail
    from_password=mpass
    to_email="" #recipient

    subject="Daily trends {0}".format(date)
    message="Today's dose of news and trends starting with global twitter trends:<br> <strong>{0}</strong>. <br> <br> Polish twitter:<br> <strong>{1}</strong> <br> <br> Top us headlines:<br> <strong>{2}</strong> <br> <br> Top uk headlines:<br> <strong>{3}</strong> <br> <br> Top news headlines:<br> <strong>{4}</strong> <br> <br> Tech news:<br> <strong>{5}</strong> <br> <br> CMC trending:<br> <strong>{6}</strong> <br> <br> Yahoo trending:<br> <strong>{7}</strong> <br>".format(*args)
    msg=MIMEText(message, 'html') #msg setup
    msg['Subject']=subject
    msg['To']=to_email
    msg['From']=from_email
    gmail=smtplib.SMTP('smtp.gmail.com', 587) #mail setup
    gmail.ehlo()
    gmail.starttls()
    gmail.login(from_email, from_password)
    gmail.send_message(msg)

    send_email(date, gtt, ptt, tus, tuk, tgn, tech, cmc, yahoo)









    share|improve this question











    $endgroup$















      4












      4








      4





      $begingroup$


      I wrote a script using Python that gets data from several sources (news sites, Twitter, Yahoo), puts it into a dict and then formats it as a string to be sent through email.



      I wonder if there is possibility to write the code more neatly in a shorter way. Maybe I'm doing some steps that are a bit unnecessary and maybe faster and I could do it differently, but not sure how.



      import json
      import urllib.request
      from twitter import Twitter, OAuth, TwitterHTTPError, TwitterStream
      from bs4 import BeautifulSoup
      import requests
      import datetime
      from pymongo import MongoClient
      from email.mime.text import MIMEText
      import smtplib
      import os
      #from config import *

      now = datetime.datetime.now()
      api_news= os.environ["api_news"]

      #twitter setup
      ACCESS_TOKEN = os.environ["ACCESS_TOKEN"]
      ACCESS_SECRET = os.environ["ACCESS_SECRET"]
      CONSUMER_KEY = os.environ["CONSUMER_KEY"]
      CONSUMER_SECRET = os.environ["CONSUMER_SECRET"]
      oauth = OAuth(ACCESS_TOKEN, ACCESS_SECRET, CONSUMER_KEY, CONSUMER_SECRET)
      twitter = Twitter(auth=oauth)

      #polish
      pol_trends = twitter.trends.place(_id = 23424923)
      twittrendlistPL=[]
      for i in pol_trends[0]['trends']:
      twittrendlistPL.append(i['name'])
      strPLT="<br>".join(str(x) for x in twittrendlistPL[0:15])

      #global trends
      globaltrends=twitter.trends.place(_id = 1)
      twittrendlist=[]
      for i in globaltrends[0]['trends']:
      twittrendlist.append(i['name'])
      def isEnglish(s):
      try:
      s.encode(encoding='utf-8').decode('ascii')
      except UnicodeDecodeError:
      return False
      else:
      return True
      G=[i for i in twittrendlist if isEnglish(i)]
      strGT="<br>".join(str(x) for x in G[0:15])

      #us headlines
      url = ('https://newsapi.org/v2/top-headlines?'
      'country=us&'+api_news)
      response = requests.get(url)
      listus=[]
      for i in range(len(response.json()['articles'])):
      listus.append(response.json()['articles'][i]['title'])
      listus.append(response.json()['articles'][i]['url'])
      strNUS="<br>".join(str(x) for x in listus[0:10])


      #uk headlines
      url = ('https://newsapi.org/v2/top-headlines?country=gb&'+api_news)
      response = requests.get(url)
      listGB=[]
      for i in range(len(response.json()['articles'])):
      listGB.append(response.json()['articles'][i]['title'])
      listGB.append(response.json()['articles'][i]['url'])
      strGB="<br>".join(str(x) for x in listGB[0:10])


      #google news(global) headlines
      url = ("https://newsapi.org/v2/top-headlines?sources=google-news&"+api_news)
      response = requests.get(url)
      listg=[]
      for i in range(len(response.json()['articles'])):
      listg.append(response.json()['articles'][i]['title'])
      listg.append(response.json()['articles'][i]['url'])
      strg="<br>".join(str(x) for x in listg[0:10])


      #most popular from technology
      url = ("https://newsapi.org/v2/top-headlines?category=technology&country=us&sortBy=popularity&"+api_news)

      response = requests.get(url)
      listt=[]
      for i in range(len(response.json()['articles'])):
      listt.append(response.json()['articles'][i]['title'])
      listt.append(response.json()['articles'][i]['url'])
      strt="<br>".join(str(x) for x in listt[0:10])

      #yahoo trending charts
      page = requests.get("https://finance.yahoo.com/trending-tickers/")
      soup = BeautifulSoup(page.content, 'html.parser')
      base=soup.findAll('td', {'class':'data-col1 Ta(start) Pstart(10px) Miw(180px)'})
      yhoo=[]
      for i in base:
      yhoo.append(i.get_text())
      strYHOO='<br>'.join(str(x) for x in yhoo[0:15])

      #crypto trends to find
      with urllib.request.urlopen("https://api.coinmarketcap.com/v2/ticker/") as url:
      cmc = json.loads(url.read().decode())
      names=[]
      change=[]
      for i in cmc['data']:
      names.append(cmc['data'][i]['symbol'])
      change.append(cmc['data'][i]['quotes']['USD']['percent_change_24h'])
      change, names = zip(*sorted(zip(change, names)))
      cmcstr='<br>'.join([str(a) + ': '+ str(b) + '%' for a,b in zip(names[-5:],change[-5:])])

      #create a dict to upload for db
      maind={
      "Global Twitter trends": strGT,
      "Polish Twitter trends" : strPLT,
      "Top US headlines": strNUS,
      "Top UK headlines": strGB,
      "Top Google News headlines": strg,
      "Top tech headlines": strt,
      "Trending yahoo stocks": strYHOO,
      "CMC trending": cmcstr,
      "Date": str(datetime.date.today())
      }

      #create and connect to mongo database
      mongo=os.environ["mongodb"]
      try:
      #local test
      #conn = MongoClient()
      #production
      conn = MongoClient(mongo)
      print("Connected successfully!!!")
      except:
      print("Could not connect to MongoDB")

      #Create/conn database
      db = conn.database

      # Created or Switched to collection names: trends
      collection = db.trends

      # Insert Data
      rec_id1 = collection.insert_one(maind)
      print("Data inserted with record ids",rec_id1)

      mpass=os.environ["mpass"]

      record = collection.find_one({'Date': str(datetime.date.today())}) #create record that is from today
      #convert all from database so that its easier to put into mail
      gtt=record["Global Twitter trends"]
      ptt=record["Polish Twitter trends"]
      tus=record["Top US headlines"]
      tuk=record["Top UK headlines"]
      tgn=record["Top Google News headlines"]
      tech=record["Top tech headlines"]
      cmc=record["CMC trending"]
      yahoo=record["Trending yahoo stocks"]
      date=record["Date"]

      #crate function to send mail
      def send_email(date, *args):
      #login data
      from_email="" #sending mail
      from_password=mpass
      to_email="" #recipient

      subject="Daily trends {0}".format(date)
      message="Today's dose of news and trends starting with global twitter trends:<br> <strong>{0}</strong>. <br> <br> Polish twitter:<br> <strong>{1}</strong> <br> <br> Top us headlines:<br> <strong>{2}</strong> <br> <br> Top uk headlines:<br> <strong>{3}</strong> <br> <br> Top news headlines:<br> <strong>{4}</strong> <br> <br> Tech news:<br> <strong>{5}</strong> <br> <br> CMC trending:<br> <strong>{6}</strong> <br> <br> Yahoo trending:<br> <strong>{7}</strong> <br>".format(*args)
      msg=MIMEText(message, 'html') #msg setup
      msg['Subject']=subject
      msg['To']=to_email
      msg['From']=from_email
      gmail=smtplib.SMTP('smtp.gmail.com', 587) #mail setup
      gmail.ehlo()
      gmail.starttls()
      gmail.login(from_email, from_password)
      gmail.send_message(msg)

      send_email(date, gtt, ptt, tus, tuk, tgn, tech, cmc, yahoo)









      share|improve this question











      $endgroup$




      I wrote a script using Python that gets data from several sources (news sites, Twitter, Yahoo), puts it into a dict and then formats it as a string to be sent through email.



      I wonder if there is possibility to write the code more neatly in a shorter way. Maybe I'm doing some steps that are a bit unnecessary and maybe faster and I could do it differently, but not sure how.



      import json
      import urllib.request
      from twitter import Twitter, OAuth, TwitterHTTPError, TwitterStream
      from bs4 import BeautifulSoup
      import requests
      import datetime
      from pymongo import MongoClient
      from email.mime.text import MIMEText
      import smtplib
      import os
      #from config import *

      now = datetime.datetime.now()
      api_news= os.environ["api_news"]

      #twitter setup
      ACCESS_TOKEN = os.environ["ACCESS_TOKEN"]
      ACCESS_SECRET = os.environ["ACCESS_SECRET"]
      CONSUMER_KEY = os.environ["CONSUMER_KEY"]
      CONSUMER_SECRET = os.environ["CONSUMER_SECRET"]
      oauth = OAuth(ACCESS_TOKEN, ACCESS_SECRET, CONSUMER_KEY, CONSUMER_SECRET)
      twitter = Twitter(auth=oauth)

      #polish
      pol_trends = twitter.trends.place(_id = 23424923)
      twittrendlistPL=[]
      for i in pol_trends[0]['trends']:
      twittrendlistPL.append(i['name'])
      strPLT="<br>".join(str(x) for x in twittrendlistPL[0:15])

      #global trends
      globaltrends=twitter.trends.place(_id = 1)
      twittrendlist=[]
      for i in globaltrends[0]['trends']:
      twittrendlist.append(i['name'])
      def isEnglish(s):
      try:
      s.encode(encoding='utf-8').decode('ascii')
      except UnicodeDecodeError:
      return False
      else:
      return True
      G=[i for i in twittrendlist if isEnglish(i)]
      strGT="<br>".join(str(x) for x in G[0:15])

      #us headlines
      url = ('https://newsapi.org/v2/top-headlines?'
      'country=us&'+api_news)
      response = requests.get(url)
      listus=[]
      for i in range(len(response.json()['articles'])):
      listus.append(response.json()['articles'][i]['title'])
      listus.append(response.json()['articles'][i]['url'])
      strNUS="<br>".join(str(x) for x in listus[0:10])


      #uk headlines
      url = ('https://newsapi.org/v2/top-headlines?country=gb&'+api_news)
      response = requests.get(url)
      listGB=[]
      for i in range(len(response.json()['articles'])):
      listGB.append(response.json()['articles'][i]['title'])
      listGB.append(response.json()['articles'][i]['url'])
      strGB="<br>".join(str(x) for x in listGB[0:10])


      #google news(global) headlines
      url = ("https://newsapi.org/v2/top-headlines?sources=google-news&"+api_news)
      response = requests.get(url)
      listg=[]
      for i in range(len(response.json()['articles'])):
      listg.append(response.json()['articles'][i]['title'])
      listg.append(response.json()['articles'][i]['url'])
      strg="<br>".join(str(x) for x in listg[0:10])


      #most popular from technology
      url = ("https://newsapi.org/v2/top-headlines?category=technology&country=us&sortBy=popularity&"+api_news)

      response = requests.get(url)
      listt=[]
      for i in range(len(response.json()['articles'])):
      listt.append(response.json()['articles'][i]['title'])
      listt.append(response.json()['articles'][i]['url'])
      strt="<br>".join(str(x) for x in listt[0:10])

      #yahoo trending charts
      page = requests.get("https://finance.yahoo.com/trending-tickers/")
      soup = BeautifulSoup(page.content, 'html.parser')
      base=soup.findAll('td', {'class':'data-col1 Ta(start) Pstart(10px) Miw(180px)'})
      yhoo=[]
      for i in base:
      yhoo.append(i.get_text())
      strYHOO='<br>'.join(str(x) for x in yhoo[0:15])

      #crypto trends to find
      with urllib.request.urlopen("https://api.coinmarketcap.com/v2/ticker/") as url:
      cmc = json.loads(url.read().decode())
      names=[]
      change=[]
      for i in cmc['data']:
      names.append(cmc['data'][i]['symbol'])
      change.append(cmc['data'][i]['quotes']['USD']['percent_change_24h'])
      change, names = zip(*sorted(zip(change, names)))
      cmcstr='<br>'.join([str(a) + ': '+ str(b) + '%' for a,b in zip(names[-5:],change[-5:])])

      #create a dict to upload for db
      maind={
      "Global Twitter trends": strGT,
      "Polish Twitter trends" : strPLT,
      "Top US headlines": strNUS,
      "Top UK headlines": strGB,
      "Top Google News headlines": strg,
      "Top tech headlines": strt,
      "Trending yahoo stocks": strYHOO,
      "CMC trending": cmcstr,
      "Date": str(datetime.date.today())
      }

      #create and connect to mongo database
      mongo=os.environ["mongodb"]
      try:
      #local test
      #conn = MongoClient()
      #production
      conn = MongoClient(mongo)
      print("Connected successfully!!!")
      except:
      print("Could not connect to MongoDB")

      #Create/conn database
      db = conn.database

      # Created or Switched to collection names: trends
      collection = db.trends

      # Insert Data
      rec_id1 = collection.insert_one(maind)
      print("Data inserted with record ids",rec_id1)

      mpass=os.environ["mpass"]

      record = collection.find_one({'Date': str(datetime.date.today())}) #create record that is from today
      #convert all from database so that its easier to put into mail
      gtt=record["Global Twitter trends"]
      ptt=record["Polish Twitter trends"]
      tus=record["Top US headlines"]
      tuk=record["Top UK headlines"]
      tgn=record["Top Google News headlines"]
      tech=record["Top tech headlines"]
      cmc=record["CMC trending"]
      yahoo=record["Trending yahoo stocks"]
      date=record["Date"]

      #crate function to send mail
      def send_email(date, *args):
      #login data
      from_email="" #sending mail
      from_password=mpass
      to_email="" #recipient

      subject="Daily trends {0}".format(date)
      message="Today's dose of news and trends starting with global twitter trends:<br> <strong>{0}</strong>. <br> <br> Polish twitter:<br> <strong>{1}</strong> <br> <br> Top us headlines:<br> <strong>{2}</strong> <br> <br> Top uk headlines:<br> <strong>{3}</strong> <br> <br> Top news headlines:<br> <strong>{4}</strong> <br> <br> Tech news:<br> <strong>{5}</strong> <br> <br> CMC trending:<br> <strong>{6}</strong> <br> <br> Yahoo trending:<br> <strong>{7}</strong> <br>".format(*args)
      msg=MIMEText(message, 'html') #msg setup
      msg['Subject']=subject
      msg['To']=to_email
      msg['From']=from_email
      gmail=smtplib.SMTP('smtp.gmail.com', 587) #mail setup
      gmail.ehlo()
      gmail.starttls()
      gmail.login(from_email, from_password)
      gmail.send_message(msg)

      send_email(date, gtt, ptt, tus, tuk, tgn, tech, cmc, yahoo)






      python performance mongodb






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Mar 17 at 5:44









      Jamal

      30.4k11121227




      30.4k11121227










      asked Mar 17 at 1:29









      Alex TAlex T

      1211




      1211






















          1 Answer
          1






          active

          oldest

          votes


















          4












          $begingroup$

          Now isn't now



          You call now at the top of the program. First of all, it's never used, so it should be deleted. Even if it were used, this call should be moved next to the usage so that any delay between program start and the usage of this variable won't introduce error.



          Use what Python gives you



          In this case, os.environ["api_news"] is more easily expressed as:



          from os import getenv
          # ...
          api_news = getenv('api_news')


          Configuration



          This isn't Python-specific, but for the kind of configuration you're pulling from the environment (access token, secret, etc.), the environment isn't really an appropriate place to keep it. Keep it in a permissions-protected file, perhaps JSON for ease of use.



          Make some functions



          Resist the urge to dump all of your code into global scope. Make a main method and a handful of subroutines. This makes the stack trace meaningful if something goes wrong, and helps with maintainability.



          Use snake case



          Python promotes is_english instead of isEnglish, in general.



          Use requests features



          You're using requests, which buys you a lot of power. Strip the query params off of your url and pass them into get as a dictionary on the params kwarg.



          Only call json() once



          You call json() a bunch of times on the response. Instead, you should probably save a temporary variable:



          articles = response.json()['articles']


          and work with that.



          Check for failure



          Call response.raise_for_status().



          Form strg as a text stream



          Rather than populating listg in a loop and then joining it to strg, you should make strg a StringIO and write it out that way, so that you don't need to hold onto a list object at all.



          Early-bail out of your loops



          You're iterating over all of your content in several places but then only using the first ten items. Instead, slice the content before iterating. Also, don't use an index; iterate through the list itself. In other words:



          for article in articles[:10]:


          Use f-strings



          This:



          str(a) + ': '+ str(b) + '%'


          can be expressed as



          f'{a}: {b}%'


          Don't except



          There are a few problems with your bare except:. First of all, it's catching things you won't expect to catch, like a user's Ctrl+C. At the least, you should be doing except Exception:. Also, when you fail, you don't do anything useful. You should probably print the exception along with your failure message and re-raise rather than continuing on with your database code.






          share|improve this answer









          $endgroup$













            Your Answer





            StackExchange.ifUsing("editor", function () {
            return StackExchange.using("mathjaxEditing", function () {
            StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
            StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
            });
            });
            }, "mathjax-editing");

            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "196"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: false,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: null,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f215586%2fget-data-and-send-mail-script%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            4












            $begingroup$

            Now isn't now



            You call now at the top of the program. First of all, it's never used, so it should be deleted. Even if it were used, this call should be moved next to the usage so that any delay between program start and the usage of this variable won't introduce error.



            Use what Python gives you



            In this case, os.environ["api_news"] is more easily expressed as:



            from os import getenv
            # ...
            api_news = getenv('api_news')


            Configuration



            This isn't Python-specific, but for the kind of configuration you're pulling from the environment (access token, secret, etc.), the environment isn't really an appropriate place to keep it. Keep it in a permissions-protected file, perhaps JSON for ease of use.



            Make some functions



            Resist the urge to dump all of your code into global scope. Make a main method and a handful of subroutines. This makes the stack trace meaningful if something goes wrong, and helps with maintainability.



            Use snake case



            Python promotes is_english instead of isEnglish, in general.



            Use requests features



            You're using requests, which buys you a lot of power. Strip the query params off of your url and pass them into get as a dictionary on the params kwarg.



            Only call json() once



            You call json() a bunch of times on the response. Instead, you should probably save a temporary variable:



            articles = response.json()['articles']


            and work with that.



            Check for failure



            Call response.raise_for_status().



            Form strg as a text stream



            Rather than populating listg in a loop and then joining it to strg, you should make strg a StringIO and write it out that way, so that you don't need to hold onto a list object at all.



            Early-bail out of your loops



            You're iterating over all of your content in several places but then only using the first ten items. Instead, slice the content before iterating. Also, don't use an index; iterate through the list itself. In other words:



            for article in articles[:10]:


            Use f-strings



            This:



            str(a) + ': '+ str(b) + '%'


            can be expressed as



            f'{a}: {b}%'


            Don't except



            There are a few problems with your bare except:. First of all, it's catching things you won't expect to catch, like a user's Ctrl+C. At the least, you should be doing except Exception:. Also, when you fail, you don't do anything useful. You should probably print the exception along with your failure message and re-raise rather than continuing on with your database code.






            share|improve this answer









            $endgroup$


















              4












              $begingroup$

              Now isn't now



              You call now at the top of the program. First of all, it's never used, so it should be deleted. Even if it were used, this call should be moved next to the usage so that any delay between program start and the usage of this variable won't introduce error.



              Use what Python gives you



              In this case, os.environ["api_news"] is more easily expressed as:



              from os import getenv
              # ...
              api_news = getenv('api_news')


              Configuration



              This isn't Python-specific, but for the kind of configuration you're pulling from the environment (access token, secret, etc.), the environment isn't really an appropriate place to keep it. Keep it in a permissions-protected file, perhaps JSON for ease of use.



              Make some functions



              Resist the urge to dump all of your code into global scope. Make a main method and a handful of subroutines. This makes the stack trace meaningful if something goes wrong, and helps with maintainability.



              Use snake case



              Python promotes is_english instead of isEnglish, in general.



              Use requests features



              You're using requests, which buys you a lot of power. Strip the query params off of your url and pass them into get as a dictionary on the params kwarg.



              Only call json() once



              You call json() a bunch of times on the response. Instead, you should probably save a temporary variable:



              articles = response.json()['articles']


              and work with that.



              Check for failure



              Call response.raise_for_status().



              Form strg as a text stream



              Rather than populating listg in a loop and then joining it to strg, you should make strg a StringIO and write it out that way, so that you don't need to hold onto a list object at all.



              Early-bail out of your loops



              You're iterating over all of your content in several places but then only using the first ten items. Instead, slice the content before iterating. Also, don't use an index; iterate through the list itself. In other words:



              for article in articles[:10]:


              Use f-strings



              This:



              str(a) + ': '+ str(b) + '%'


              can be expressed as



              f'{a}: {b}%'


              Don't except



              There are a few problems with your bare except:. First of all, it's catching things you won't expect to catch, like a user's Ctrl+C. At the least, you should be doing except Exception:. Also, when you fail, you don't do anything useful. You should probably print the exception along with your failure message and re-raise rather than continuing on with your database code.






              share|improve this answer









              $endgroup$
















                4












                4








                4





                $begingroup$

                Now isn't now



                You call now at the top of the program. First of all, it's never used, so it should be deleted. Even if it were used, this call should be moved next to the usage so that any delay between program start and the usage of this variable won't introduce error.



                Use what Python gives you



                In this case, os.environ["api_news"] is more easily expressed as:



                from os import getenv
                # ...
                api_news = getenv('api_news')


                Configuration



                This isn't Python-specific, but for the kind of configuration you're pulling from the environment (access token, secret, etc.), the environment isn't really an appropriate place to keep it. Keep it in a permissions-protected file, perhaps JSON for ease of use.



                Make some functions



                Resist the urge to dump all of your code into global scope. Make a main method and a handful of subroutines. This makes the stack trace meaningful if something goes wrong, and helps with maintainability.



                Use snake case



                Python promotes is_english instead of isEnglish, in general.



                Use requests features



                You're using requests, which buys you a lot of power. Strip the query params off of your url and pass them into get as a dictionary on the params kwarg.



                Only call json() once



                You call json() a bunch of times on the response. Instead, you should probably save a temporary variable:



                articles = response.json()['articles']


                and work with that.



                Check for failure



                Call response.raise_for_status().



                Form strg as a text stream



                Rather than populating listg in a loop and then joining it to strg, you should make strg a StringIO and write it out that way, so that you don't need to hold onto a list object at all.



                Early-bail out of your loops



                You're iterating over all of your content in several places but then only using the first ten items. Instead, slice the content before iterating. Also, don't use an index; iterate through the list itself. In other words:



                for article in articles[:10]:


                Use f-strings



                This:



                str(a) + ': '+ str(b) + '%'


                can be expressed as



                f'{a}: {b}%'


                Don't except



                There are a few problems with your bare except:. First of all, it's catching things you won't expect to catch, like a user's Ctrl+C. At the least, you should be doing except Exception:. Also, when you fail, you don't do anything useful. You should probably print the exception along with your failure message and re-raise rather than continuing on with your database code.






                share|improve this answer









                $endgroup$



                Now isn't now



                You call now at the top of the program. First of all, it's never used, so it should be deleted. Even if it were used, this call should be moved next to the usage so that any delay between program start and the usage of this variable won't introduce error.



                Use what Python gives you



                In this case, os.environ["api_news"] is more easily expressed as:



                from os import getenv
                # ...
                api_news = getenv('api_news')


                Configuration



                This isn't Python-specific, but for the kind of configuration you're pulling from the environment (access token, secret, etc.), the environment isn't really an appropriate place to keep it. Keep it in a permissions-protected file, perhaps JSON for ease of use.



                Make some functions



                Resist the urge to dump all of your code into global scope. Make a main method and a handful of subroutines. This makes the stack trace meaningful if something goes wrong, and helps with maintainability.



                Use snake case



                Python promotes is_english instead of isEnglish, in general.



                Use requests features



                You're using requests, which buys you a lot of power. Strip the query params off of your url and pass them into get as a dictionary on the params kwarg.



                Only call json() once



                You call json() a bunch of times on the response. Instead, you should probably save a temporary variable:



                articles = response.json()['articles']


                and work with that.



                Check for failure



                Call response.raise_for_status().



                Form strg as a text stream



                Rather than populating listg in a loop and then joining it to strg, you should make strg a StringIO and write it out that way, so that you don't need to hold onto a list object at all.



                Early-bail out of your loops



                You're iterating over all of your content in several places but then only using the first ten items. Instead, slice the content before iterating. Also, don't use an index; iterate through the list itself. In other words:



                for article in articles[:10]:


                Use f-strings



                This:



                str(a) + ': '+ str(b) + '%'


                can be expressed as



                f'{a}: {b}%'


                Don't except



                There are a few problems with your bare except:. First of all, it's catching things you won't expect to catch, like a user's Ctrl+C. At the least, you should be doing except Exception:. Also, when you fail, you don't do anything useful. You should probably print the exception along with your failure message and re-raise rather than continuing on with your database code.







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Mar 17 at 18:00









                ReinderienReinderien

                4,365822




                4,365822






























                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Code Review Stack Exchange!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    Use MathJax to format equations. MathJax reference.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f215586%2fget-data-and-send-mail-script%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    Fairchild Swearingen Metro Inhaltsverzeichnis Geschichte | Innenausstattung | Nutzung | Zwischenfälle...

                    Pilgersdorf Inhaltsverzeichnis Geografie | Geschichte | Bevölkerungsentwicklung | Politik | Kultur...

                    Marineschifffahrtleitung Inhaltsverzeichnis Geschichte | Heutige Organisation der NATO | Nationale und...