Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
T
tootbot-py
Manage
Activity
Members
Labels
Plan
Issues
0
Issue boards
Milestones
Wiki
Requirements
Code
Merge requests
0
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package Registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
M33
tootbot-py
Commits
025c53b9
Commit
025c53b9
authored
4 years ago
by
M33
Browse files
Options
Downloads
Patches
Plain Diff
Make it work with RSS feeds
parent
3796464e
Loading
Loading
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
tootbot.py
+36
-28
36 additions, 28 deletions
tootbot.py
with
36 additions
and
28 deletions
tootbot.py
+
36
−
28
View file @
025c53b9
...
@@ -8,8 +8,8 @@ import feedparser
...
@@ -8,8 +8,8 @@ import feedparser
from
mastodon
import
Mastodon
from
mastodon
import
Mastodon
import
requests
import
requests
if
len
(
sys
.
argv
)
<
4
:
if
len
(
sys
.
argv
)
<
8
:
print
(
"
Usage: python3 tootbot.py twitter_account mastodon_login mastodon_passwd mastodon_instance
[
max_days
[
footer_tags
[
delay
]]]
"
)
# noqa
print
(
"
Usage: python3 tootbot.py twitter_account mastodon_login mastodon_passwd mastodon_instance max_days footer_tags delay
mastodon_account_email
"
)
# noqa
sys
.
exit
(
1
)
sys
.
exit
(
1
)
# sqlite db to store processed tweets (and corresponding toots ids)
# sqlite db to store processed tweets (and corresponding toots ids)
...
@@ -20,38 +20,36 @@ db.execute('''CREATE TABLE IF NOT EXISTS tweets (tweet text, toot text,
...
@@ -20,38 +20,36 @@ db.execute('''CREATE TABLE IF NOT EXISTS tweets (tweet text, toot text,
if
len
(
sys
.
argv
)
>
4
:
if
len
(
sys
.
argv
)
>
4
:
instance
=
sys
.
argv
[
4
]
instance
=
sys
.
argv
[
4
]
else
:
instance
=
'
amicale.net
'
if
len
(
sys
.
argv
)
>
5
:
if
len
(
sys
.
argv
)
>
5
:
days
=
int
(
sys
.
argv
[
5
])
days
=
int
(
sys
.
argv
[
5
])
else
:
days
=
1
if
len
(
sys
.
argv
)
>
6
:
if
len
(
sys
.
argv
)
>
6
:
tags
=
sys
.
argv
[
6
]
tags
=
sys
.
argv
[
6
]
else
:
tags
=
None
if
len
(
sys
.
argv
)
>
7
:
if
len
(
sys
.
argv
)
>
7
:
delay
=
int
(
sys
.
argv
[
7
])
delay
=
int
(
sys
.
argv
[
7
])
else
:
delay
=
0
if
len
(
sys
.
argv
)
>
8
:
mastodon_account_email
=
sys
.
argv
[
8
]
source
=
sys
.
argv
[
1
]
source
=
sys
.
argv
[
1
]
mastodon
=
sys
.
argv
[
2
]
mastodon
=
sys
.
argv
[
2
]
passwd
=
sys
.
argv
[
3
]
passwd
=
sys
.
argv
[
3
]
mastodon_api
=
None
mastodon_api
=
None
if
source
[:
4
]
==
'
http
'
:
if
source
[:
4
]
==
'
http
'
:
print
(
"
Parsing source
"
,
source
,
"
...
"
,
end
=
''
)
d
=
feedparser
.
parse
(
source
)
d
=
feedparser
.
parse
(
source
)
twitter
=
None
twitter
=
None
print
(
"
ok.
"
)
else
:
else
:
d
=
feedparser
.
parse
(
'
http://twitrss.me/twitter_user_to_rss/?user=
'
+
source
)
d
=
feedparser
.
parse
(
'
http://twitrss.me/twitter_user_to_rss/?user=
'
+
source
)
twitter
=
source
twitter
=
source
print
(
"
Processing
"
,
end
=
''
)
for
t
in
reversed
(
d
.
entries
):
for
t
in
reversed
(
d
.
entries
):
# check if this tweet has been processed
# check if this tweet has been processed
db
.
execute
(
'
SELECT * FROM tweets WHERE tweet = ? AND twitter = ? and mastodon = ? and instance = ?
'
,
(
t
.
id
,
source
,
mastodon
,
instance
))
# noqa
db
.
execute
(
'
SELECT * FROM tweets WHERE tweet = ? AND twitter = ? and mastodon = ? and instance = ?
'
,
(
t
.
id
,
source
,
mastodon
,
instance
))
# noqa
...
@@ -63,33 +61,38 @@ for t in reversed(d.entries):
...
@@ -63,33 +61,38 @@ for t in reversed(d.entries):
if
last
is
None
and
age
<
timedelta
(
days
=
days
)
and
age
>
timedelta
(
days
=
delay
):
if
last
is
None
and
age
<
timedelta
(
days
=
days
)
and
age
>
timedelta
(
days
=
delay
):
if
mastodon_api
is
None
:
if
mastodon_api
is
None
:
# Create application if it does not exist
# Create application if it does not exist
if
not
os
.
path
.
isfile
(
instance
+
'
.secret
'
):
try
:
if
Mastodon
.
create_app
(
Mastodon
.
create_app
(
'
tootbot
'
,
api_base_url
=
'
https://
'
+
instance
,
to_file
=
'
/var/run/lock/
'
+
instance
+
'
.secret
'
)
'
tootbot
'
,
except
:
api_base_url
=
'
https://
'
+
instance
,
print
(
'
ERROR: failed to create app on instance
'
+
instance
)
to_file
=
instance
+
'
.secret
'
sys
.
exit
(
1
)
):
print
(
'
tootbot app created on instance
'
+
instance
)
else
:
print
(
'
failed to create app on instance
'
+
instance
)
sys
.
exit
(
1
)
try
:
try
:
print
(
"
Trying to connect with
"
,
instance
+
'
.secret
'
,
"
to
"
,
'
https://
'
+
instance
,
"
...
"
,
end
=
''
)
mastodon_api
=
Mastodon
(
mastodon_api
=
Mastodon
(
client_id
=
instance
+
'
.secret
'
,
client_id
=
'
/var/run/lock/
'
+
instance
+
'
.secret
'
,
api_base_url
=
'
https://
'
+
instance
api_base_url
=
'
https://
'
+
instance
)
)
print
(
"
ok.
"
)
except
:
print
(
"
ERROR: can
'
t connect
"
)
sys
.
exit
(
1
)
print
(
"
Login with email
"
,
mastodon_account_email
,
"
...
"
,
end
=
''
)
try
:
mastodon_api
.
log_in
(
mastodon_api
.
log_in
(
username
=
mastodon
,
mastodon_account_email
,
password
=
passwd
,
passwd
,
scopes
=
[
'
read
'
,
'
write
'
],
to_file
=
'
/var/run/lock/
'
+
instance
+
'
.secret
'
to_file
=
mastodon
+
"
.secret
"
)
)
print
(
"
ok.
"
)
except
:
except
:
print
(
"
ERROR: First Login Failed!
"
)
print
(
"
ERROR: First Login Failed!
"
,
)
sys
.
exit
(
1
)
sys
.
exit
(
1
)
c
=
t
.
title
c
=
t
.
title
print
(
"
============================================================
"
)
print
(
"
[Posting]:
"
,
c
)
print
(
"
"
)
if
twitter
and
t
.
author
.
lower
()
!=
(
'
(@%s)
'
%
twitter
).
lower
():
if
twitter
and
t
.
author
.
lower
()
!=
(
'
(@%s)
'
%
twitter
).
lower
():
c
=
(
"
RT https://twitter.com/%s
\n
"
%
t
.
author
[
2
:
-
1
])
+
c
c
=
(
"
RT https://twitter.com/%s
\n
"
%
t
.
author
[
2
:
-
1
])
+
c
toot_media
=
[]
toot_media
=
[]
...
@@ -98,6 +101,7 @@ for t in reversed(d.entries):
...
@@ -98,6 +101,7 @@ for t in reversed(d.entries):
media
=
requests
.
get
(
p
.
group
(
0
))
media
=
requests
.
get
(
p
.
group
(
0
))
media_posted
=
mastodon_api
.
media_post
(
media
.
content
,
mime_type
=
media
.
headers
.
get
(
'
content-type
'
))
media_posted
=
mastodon_api
.
media_post
(
media
.
content
,
mime_type
=
media
.
headers
.
get
(
'
content-type
'
))
toot_media
.
append
(
media_posted
[
'
id
'
])
toot_media
.
append
(
media_posted
[
'
id
'
])
print
(
"
Added media
"
,
id
)
# replace short links by original URL
# replace short links by original URL
m
=
re
.
search
(
r
"
http[^ \xa0]*
"
,
c
)
m
=
re
.
search
(
r
"
http[^ \xa0]*
"
,
c
)
...
@@ -132,3 +136,7 @@ for t in reversed(d.entries):
...
@@ -132,3 +136,7 @@ for t in reversed(d.entries):
db
.
execute
(
"
INSERT INTO tweets VALUES ( ? , ? , ? , ? , ? )
"
,
db
.
execute
(
"
INSERT INTO tweets VALUES ( ? , ? , ? , ? , ? )
"
,
(
t
.
id
,
toot
[
"
id
"
],
source
,
mastodon
,
instance
))
(
t
.
id
,
toot
[
"
id
"
],
source
,
mastodon
,
instance
))
sql
.
commit
()
sql
.
commit
()
else
:
print
(
"
.
"
,
end
=
''
)
print
(
"
Bye!
"
)
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment