What are the interesting Python scripts you've written?

What are the interesting python scripts youve written
One interesting script I’m proud (I’m working as a python developer for 2 years - so may be that script not really difficult for you - really senior 50 years experience python developer ahahaha) was to send a common message to all your friends on Facebook (for example - invitation to send $1 on my credit card ahahahaahah). (the message will reach the inbox of everyone if you are using your gmail id is updated in Facebook profile, else it will reach Facebook's spam msg box).
import requests 
import urllib 
import smtplib 
import json 
 
#Variable declaration 
TOKEN = ''  # your Access token here. You can get it @ 'https://developers.facebook.com/tools/explorer/' 
USERNAME = '' # your FB username 
SMTP_SERVER = 'Page on Gmail' 
SMTP_PORT = 587 
sender = '' #your gmail id 
message = 'Hi, This is a test message.'#Message here 
password = '' #Your Gmail password 
 
def sendMessage(): 
	payload = {'access_token': TOKEN} 
	response = requests.get('https://graph.facebook.com/'+USERNAME+'/friends?fields=username',params = payload) 
	result = json.loads(response.text) 
	print result 
	email_lists = [] 
	for user in result'data': 
		try: 
			email_lists.append(user'username'+"@facebook.com") 
		except Exception,e: 
			print "Error :"+ str(e) 
	print email_lists 
	print len(email_lists) 
	for email_list in email_lists: 
		session = smtplib.SMTP('Page on Gmail',587) 
		session.ehlo() 
		session.starttls() 
		session.ehlo() 
		session.login(sender, password) 
		session.sendmail(sender,str(email_list),message) 
		print 'Message sent to'+str(email_list) 
		session.quit() 
 
if name == 'main': 
   sendMessage()
And I want to share with you the best decision to archive the articles. The page thebrowser.com recommends a list of 4-5 curated articles from the web. You can see the daily recommendations for free but you've to pay to get access to their archive (or for example free slack account with limitation ahahahah).
Using Kimono I created an API which will give me details about the recommended articles such as the author, the title, the date of publication and most importantly - link to the article. Then I wrote a quick script in Python that will call this API and update a SQLite database using sqlite3 library. Here is the dirty code which does the work.
import json 
import urllib.request 
import sqlite3 
import re 
 
 
def api_call(address): 
   results = json.loads(urllib.request.urlopen(address).read().decode("utf-8")) 
   for item in (results'results''collection1'): 
       try: 
           article_title = item'Links''text' 
           article_details = item'Deatils''text' 
           article_author = article_details.split('|')0.strip() 
           article_source = article_details.split('|')1.strip() 
           published_date = article_details.split('|')2.strip() 
           published_day = int(re.findall(r'\d+', published_date.split(' ')0)0) 
           published_month = published_date.split(' ')1 
           published_year = published_date.split(' ')2 
           article_link = item'Links''href' 
           article_excerpt = item'Excerpt' 
           if db_query(link = (article_link,)) is None: 
               db_update({'title': article_title, 'author': article_author, 'source': article_source, 'day': published_day, 
                          'month': published_month, 'year': published_year, 'link': article_link, 'excerpt': article_excerpt}) 
       except Exception: 
           continue 
 
 
def db_update(new_article): 
   conn = sqlite3.connect('thebrowser_database.db') 
   c = conn.cursor() 
   data = (new_article'title', new_article'author', new_article'source', new_article'month', new_article'day', new_article'year', 
           new_article'link', new_article'excerpt') 
   c.execute('INSERT INTO articles_database VALUES (NULL,?,?,?,?,?,?,?,?)', data) 
   conn.commit() 
   conn.close() 
 
 
def db_query(**kwargs): 
   conn = sqlite3.connect('thebrowser_database.db') 
   c = conn.cursor() 
   for key, value in kwargs.items(): 
       if key == 'link': 
           c.execute('SELECT * FROM articles_database WHERE link=?', value) 
           return c.fetchone() 
       elif key == 'title': 
           c.execute('SELECT * FROM articles_database WHERE title=?', value) 
           return c.fetchone() 
       elif key == 'author': 
           c.execute('SELECT * FROM articles_database WHERE author=?', value) 
           return c.fetchall() 
       elif key == 'source': 
           c.execute('SELECT * FROM articles_database WHERE source=?', value) 
           return c.fetchall() 
       elif key == 'day': 
           c.execute('SELECT * FROM articles_database WHERE day=?', value) 
           return c.fetchall() 
       elif key == 'month': 
           c.execute('SELECT * FROM articles_database WHERE month=?', value) 
           return c.fetchall() 
       elif key == 'year': 
           c.execute('SELECT * FROM articles_database WHERE year=?', value) 
           return c.fetchall() 
       elif key == 'excerpt': 
           c.execute('SELECT * FROM articles_database WHERE excerpt=?', value) 
           return c.fetchone() 
       elif key == 'id': 
           c.execute('SELECT * FROM articles_database WHERE id=?', value) 
           return c.fetchone() 
       else: 
           print('Bad Key') 
 
 
def main(): 
   api_address = "My API"  
   api_call(api_address) 
   #print(db_query(id = (15,))) 
 
 
if name == "main": 
   main() 
Thanks for reading. Have a great day!
Popular articles