This repository has been archived by the owner on Nov 7, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtwitter2facebook.py
executable file
·123 lines (89 loc) · 3.58 KB
/
twitter2facebook.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
112
113
114
115
116
117
118
119
120
121
122
123
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import configparser
import html
import os
import re
import requests
import selenium
import selenium.webdriver.firefox.options
import sentry_sdk
import sqlite3
import time
from lxml.html.clean import Cleaner
from selenium.webdriver.common.by import By
from selenium.webdriver.firefox.firefox_binary import FirefoxBinary
class Twitter2Facebook(object):
b = None
def init_browser(self):
if self.b is not None:
return
home = os.environ['HOME']
firefox_binary = '/usr/bin/firefox-esr'
profile_dir = home + '/.mozilla/firefox-esr/selenium'
options = selenium.webdriver.firefox.options.Options()
options.headless = True
options.profile = profile_dir
self.b = selenium.webdriver.Firefox(firefox_binary=FirefoxBinary(firefox_binary), options=options)
def post(self, text):
self.init_browser()
b = self.b
url = 'https://mbasic.facebook.com/'
b.get(url)
t = b.find_element(by=By.CSS_SELECTOR, value='#mbasic_inline_feed_composer textarea')
t.send_keys(text)
btn = b.find_element(by=By.CSS_SELECTOR, value='#mbasic_inline_feed_composer input[value="Post"]')
btn.click()
def main(self):
home = os.environ['HOME']
f_conf = '{}/.config/twitter2facebook/config.ini'.format(home)
f_db = '{}/.config/twitter2facebook/entry.sqlite3'.format(home)
c = configparser.ConfigParser()
c.read(f_conf)
if 'sentry_sdk_url' in c['default'] and '' != c['default']['sentry_sdk_url']:
sentry_sdk_url = c['default']['sentry_sdk_url']
sentry_sdk.init(sentry_sdk_url)
jsonfeed_url = c['default']['twitter_rssbridge_jsonfeed_url']
items = requests.get(jsonfeed_url).json()['items']
s = sqlite3.connect(f_db)
sql_insert = 'INSERT INTO entry (twitter_id, created_at) VALUES (?, ?);'
sql_select = 'SELECT COUNT(*) FROM entry WHERE twitter_id = ?;'
cl = Cleaner(allow_tags=['a'])
for item in items:
# Skip if it's retweet.
if item['title'].startswith('@'):
continue
# Craft "text".
#
# First to remove all tags except "a" and root's "div".
text = cl.clean_html(item['content_html'])
# Remove root's "div".
text = text.replace('<div>', '').replace('</div>', '')
# Replace each "a" element with its href link and unescape.
tokens = re.split(r'<a href="(.*?)">(?:.*?)</a>', text, flags=re.DOTALL)
for i in range(1, len(tokens), 2):
tokens[i] = re.sub(r'<a href="(.*?)">.*?</a>', r'\1', tokens[i], flags=re.DOTALL)
tokens[i] = html.unescape(tokens[i])
text = ''.join(tokens)
# Skip Instagram.
if 'https://instagr.am/p/' in text:
continue
# Generate parameters.
id_str = item['url'].split('/')[-1]
url = item['url']
c = s.cursor()
c.execute(sql_select, (id_str, ))
if 0 == c.fetchone()[0]:
content = '{}\n\n{}'.format(text, url)
print('* content = {}'.format(content))
print(content)
self.post(content)
c.execute(sql_insert, (id_str, int(time.time())))
s.commit()
self.quit_browser()
def quit_browser(self):
if self.b is None:
return
self.b.quit()
if __name__ == '__main__':
Twitter2Facebook().main()