Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python3
- # -*- coding: utf-8 -*-
- import re
- import tweepy
- import requests
- from bs4 import BeautifulSoup
- CONSUMER_KEY = 'SOME_KEY'
- CONSUMER_SECRET = 'SOME_KEY'
- ACCESS_KEY = 'SOME_KEY'
- ACCESS_SECRET = 'SOME_KEY'
- auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
- auth.set_access_token(ACCESS_KEY, ACCESS_SECRET)
- api = tweepy.API(auth)
- packt_url = "http://bit.ly/Free-Tech-Learning"
- headers = {'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.95 Safari/537.36'}
- keywords = ["python", "python3", "django", "flask", "numpy", "ipython", "machinelearning", "datascience", "raspberry"]
- def get_book_name():
- try:
- site = requests.get(packt_url, headers=headers)
- data = site.text
- checksoup = BeautifulSoup(data, "html.parser")
- book = str(checksoup.select(".dotd-title")[0].getText())
- book_name = book.replace("\t", "").replace("\n", "")
- return book_name
- except:
- return 0
- def tweet_message(message_text):
- try :
- api.update_status(message_text)
- except tweepy.TweepError as e:
- e = eval(str(e))
- print(e[0]['message'], "| Code :", e[0]['code'])
- if __name__ == '__main__':
- book = get_book_name()
- print("Today Book :", book)
- message_en = "Today's free e-book on Packt: %s \nYou can download the book at http://bit.ly/Free-Tech-Learning\nhttp://bit.ly/Free-Tech-Learning" % book
- message_sk = "Dnešná eKnižka zdarma na Packt-e : %s \nVyužite šancu :)\nhttp://bit.ly/Free-Tech-Learning" % book
- if any(word in book.lower() for word in keywords):
- try:
- tweet_message(message_en)
- except:
- pass
- try:
- tweet_message(message_sk)
- except:
- pass
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement