forked from o20n3/Facebook-to-Telegram-Bot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFB_to_TG.py
53 lines (47 loc) · 2.22 KB
/
FB_to_TG.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
#!/usr/bin/env python3
#Get all the necessary tools
import telegram, threading
from facebook_scraper import get_posts
from datetime import datetime
import csv
from tempfile import NamedTemporaryFile
import shutil
import bot_conf
#Pull the bot 'TOKEN' and Telegram 'chat_id' added during setup
TOKEN = bot_conf.TOKEN
bot = telegram.Bot(TOKEN)
chat_id = bot_conf.chat_id
#Pull data from csv input from setup
date_format = '%Y-%m-%d %H:%M:%S'
fields = ['telegram_name', 'fbpage_tag', 'last_post_date']
#Define value for time between checks
WAIT_SECONDS = 120
#Scraping and composing of posts sent to Telegram Bot
def check():
with open('fbpages.csv', mode='r+') as csv_file, NamedTemporaryFile(mode='w', delete=False) as tempfile:
csv_reader = csv.DictReader(csv_file)
csv_writer = csv.DictWriter(tempfile, fields)
line_count = 0
csv_writer.writeheader()
for page in csv_reader:
temp = None
posts = list(get_posts(page['fbpage_tag'], pages=2))
posts.sort(key = lambda x: x['time'])
for post in posts:
if post['time'] <= datetime.strptime(page['last_post_date'], date_format): # post already sent to channel
break
if post['image'] is not None:
bot.send_photo(chat_id, post['image'], (post['text'] if post['text'] else '')+ '\n[' + page['telegram_name'] + ']')
if (post['time'] > datetime.strptime(page['last_post_date'], date_format) and temp is None):
temp = post['time']
elif post['text'] is not None:
bot.send_message(chat_id, (post['text'] if post['text'] else '')+ '\n[' + page['telegram_name'] + ']')
if (post['time'] > datetime.strptime(page['last_post_date'], date_format) and temp is None):
temp = post['time']
if temp is not None:
page['last_post_date'] = temp
row = {'telegram_name': page['telegram_name'], 'fbpage_tag': page['fbpage_tag'], 'last_post_date': page['last_post_date']}
csv_writer.writerow(row)
shutil.move(tempfile.name, 'fbpages.csv')
threading.Timer(WAIT_SECONDS, check).start()
check()