-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathfunctions.py
111 lines (86 loc) · 3.23 KB
/
functions.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
"This script contains useful functions for building the Twitter bot."
import json
import requests
from lxml.html import fromstring
from trouble_script import SHORTE_ST_TOKEN
from twitter import TwitterHTTPError
# Here 't' is account handler defined in the main script.
def reply(account_handler, tweet_id, user_name, msg):
"""
Sends msg as reply to the tweet whose id is passed.
user_name of the tweet's author is required as per Twitter API docs.
"""
account_handler.statuses.update(
status='@%s %s' % (user_name, msg),
in_reply_to_status_id=tweet_id
)
def print_tweet(tweet):
"""Displays the primary contents of a tweet, maybe except links."""
print(tweet["user"]["name"])
print(tweet["user"]["screen_name"])
print(tweet["created_at"])
print(tweet["text"])
hashtags_list = []
hashtags = tweet["entities"]["hashtags"]
for tag in hashtags:
hashtags_list.append(tag["text"])
print(hashtags_list)
def fav_tweet(account_handler, tweet):
"""Favorites a passed tweet and returns a success status - 0 if successful
otherwise 1.
"""
try:
account_handler.favorites.create(_id=tweet['id'])
return 0
except TwitterHTTPError:
return 1
def retweet(account_handler, tweet):
"""Retweets a passed tweet and returns a success status - 0 if successful
otherwise 1.
"""
try:
account_handler.statuses.retweet._id(_id=tweet["id"])
return 0
except TwitterHTTPError:
return 1
def quote_tweet(account_handler, tweet, text):
"""Quotes a passed tweet with a passed text and then tweets it."""
# May not work for long links because of 140-limit. Can be improved.
tweet_id = tweet["id"]
screen_name = tweet["user"]["screen_name"]
link = "https://twitter.com/%s/status/%s" % (screen_name, tweet_id)
try:
string = text + " " + link
account_handler.statuses.update(status=string)
return 0
except TwitterHTTPError:
return 1
def unfollow(account_handler, iden):
"""Unfollows the person identified by 'iden', returns 0 if successful,
otherwise 0."""
try:
account_handler.friendships.destroy(_id=iden)
return 0
except TwitterHTTPError:
return 1
def shorten_url(url):
"""Shortens the passed url using shorte.st's API."""
response = requests.put(
"https://api.shorte.st/v1/data/url",
{"urlToShorten": url}, headers={"public-api-token": SHORTE_ST_TOKEN}
)
info = json.loads(response.content.decode())
if info["status"] == "ok":
return info["shortenedUrl"]
return url # If shortening fails, the original url is returned.
def get_top_headline(query):
"""Gets top headline for a query from Google News.
Returns in format (headline, link)."""
# Following assumes that query doesn't contain special characters.
url_encoded_query = '%20'.join(query.split())
req = "https://news.google.com/news/search/section/q/" + url_encoded_query
tree = fromstring(requests.get(req).content)
news_item = tree.find(".//main/div/c-wiz/div/c-wiz")
headline = news_item.xpath('.//a/text()')[0].strip()
link = news_item.xpath('.//a/@href')[0].strip()
return (headline, link)