-
Notifications
You must be signed in to change notification settings - Fork 0
/
cron_twitter.py
66 lines (56 loc) · 3.46 KB
/
cron_twitter.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
# -*- coding: utf-8 -*-
import MySQLdb
import twitter
import json
from HTMLParser import HTMLParser
import configuration
if configuration.TWITTER['ACTIVE'] is 'true':
# Setup DB connection
SQLcon = MySQLdb.connect(configuration.DATABASE['HOST'], configuration.DATABASE['USER'], configuration.DATABASE['PASSWORD'], configuration.DATABASE['NAME'])
SQLcon.set_character_set('utf8')
SQLcur = SQLcon.cursor()
SQLcur.execute('SET NAMES utf8;')
SQLcur.execute('SET CHARACTER SET utf8;')
HTMLParser = HTMLParser()
# Setup TwitterAPI
TWITTERapi = twitter.Api(consumer_key=configuration.TWITTER['C_KEY'],
consumer_secret=configuration.TWITTER['C_SECRET'],
access_token_key=configuration.TWITTER['A_TOKEN'],
access_token_secret=configuration.TWITTER['A_SECRET'],
sleep_on_rate_limit=True)
if configuration.TWITTER['MENTIONS'] is 'true':
# Fetch Mentions
TwitterMetions = TWITTERapi.GetMentions(count=5)
# Check if Mentions already shown and insert into db
for Status in TwitterMetions:
SQLcur.execute("SELECT COUNT(*) FROM `message` WHERE `provider_id` = 2 AND `message_id_external` = '" + str(Status.id_str) + "';")
if SQLcur.fetchone()[0] == 0:
# Insert message to database
SQLcur.execute("INSERT INTO `message` (`message_text`, `message_from`, `message_id_external`, `provider_id`) VALUES (%s, %s, %s, '2');", (HTMLParser.unescape(Status.text), Status.user.screen_name, str(Status.id_str)))
# Like & retweet
TWITTERapi.CreateFavorite(status=Status)
TWITTERapi.PostRetweet(status_id=Status.id_str)
if configuration.TWITTER['USERS'] is 'true':
# Loop through all Users
for User in configuration.TWITTER['TWEETS_USERS']:
# Fetch User Timeline
TwitterTimeline = TWITTERapi.GetUserTimeline(screen_name=User, count=5)
# Check if Statuses already shown and insert into db
for Status in TwitterTimeline:
SQLcur.execute("SELECT COUNT(*) FROM `message` WHERE `provider_id` = 2 AND `message_id_external` = '" + str(Status.id_str) + "';")
if SQLcur.fetchone()[0] == 0:
# Insert message to database
SQLcur.execute("INSERT INTO `message` (`message_text`, `message_from`, `message_id_external`, `provider_id`) VALUES (%s, %s, %s, '2');", (HTMLParser.unescape(Status.text), Status.user.screen_name, str(Status.id_str)))
if configuration.TWITTER['HASHTAGS'] is 'true':
# Loop through all Users
for Hashtag in configuration.TWITTER['TWEETS_HASHTAGS']:
# Fetch Tweets by Hashtag
Tweets = TWITTERapi.GetSearch(term=Hashtag, result_type="recent", count=5)
# Check if Statuses already shown and insert into db
for Status in Tweets:
SQLcur.execute("SELECT COUNT(*) FROM `message` WHERE `provider_id` = 2 AND `message_id_external` = '" + str(Status.id_str) + "';")
if SQLcur.fetchone()[0] == 0:
# Insert message to database
SQLcur.execute("INSERT INTO `message` (`message_text`, `message_from`, `message_id_external`, `provider_id`) VALUES (%s, %s, %s, '2');", (HTMLParser.unescape(Status.text), Status.user.screen_name, str(Status.id_str)))
# Commit SQL transaction
SQLcon.commit()