Compare commits
14 Commits
Author | SHA1 | Date | |
---|---|---|---|
bbcc69cf97 | |||
a562aff2c1 | |||
85aa960e66 | |||
89cc933427 | |||
168d0ff42f | |||
f1bbd262e7 | |||
b252f8855b | |||
ee6a28f17c | |||
ce92f432e2 | |||
0ba324a373 | |||
2602a91a9b | |||
5818ac080a | |||
bd655cb883 | |||
a4c463c12d |
4
.gitignore
vendored
4
.gitignore
vendored
@ -1,7 +1,9 @@
|
||||
.idea
|
||||
venv
|
||||
bearer.json
|
||||
twitter.json
|
||||
mail_credentials.json
|
||||
test.*
|
||||
__pycache__/
|
||||
*.json
|
||||
!/etc/tw2hugo/twitter.json
|
||||
!/etc/tw2hugo/mail_credentials.json
|
24
Pipfile.lock
generated
24
Pipfile.lock
generated
@ -18,17 +18,17 @@
|
||||
"default": {
|
||||
"certifi": {
|
||||
"hashes": [
|
||||
"sha256:5930595817496dd21bb8dc35dad090f1c2cd0adfaf21204bf6732ca5d8ee34d3",
|
||||
"sha256:8fc0819f1f30ba15bdb34cceffb9ef04d99f420f68eb75d901e9560b8749fc41"
|
||||
"sha256:2bbf76fd432960138b3ef6dda3dde0544f27cbf8546c458e60baf371917ba9ee",
|
||||
"sha256:50b1e4f8446b06f41be7dd6338db18e0990601dce795c2b1686458aa7e8fa7d8"
|
||||
],
|
||||
"version": "==2020.6.20"
|
||||
"version": "~> 2022.12.07"
|
||||
},
|
||||
"chardet": {
|
||||
"hashes": [
|
||||
"sha256:84ab92ed1c4d4f16916e05906b6b75a6c0fb5db821cc65e70cbd64a3e2a5eaae",
|
||||
"sha256:fc323ffcaeaed0e0a02bf4d117757b98aed530d9ed4531e3e15460124c106691"
|
||||
"sha256:0d6f53a15db4120f2b08c94f11e7d93d2c911ee118b6b30a04ec3ee8310179fa",
|
||||
"sha256:f864054d66fd9118f2e67044ac8981a54775ec5b67aed0441892edb553d21da5"
|
||||
],
|
||||
"version": "==3.0.4"
|
||||
"version": "==4.0.0"
|
||||
},
|
||||
"idna": {
|
||||
"hashes": [
|
||||
@ -39,18 +39,18 @@
|
||||
},
|
||||
"requests": {
|
||||
"hashes": [
|
||||
"sha256:b3559a131db72c33ee969480840fff4bb6dd111de7dd27c8ee1f820f4f00231b",
|
||||
"sha256:fe75cc94a9443b9246fc7049224f75604b113c36acb93f87b80ed42c44cbb898"
|
||||
"sha256:27973dd4a904a4f13b263a19c866c13b92a39ed1c964655f025f3f8d3d75b804",
|
||||
"sha256:c210084e36a42ae6b9219e00e48287def368a26d03a048ddad7bfee44f75871e"
|
||||
],
|
||||
"index": "pypi",
|
||||
"version": "==2.24.0"
|
||||
"version": "==2.25.1"
|
||||
},
|
||||
"urllib3": {
|
||||
"hashes": [
|
||||
"sha256:8d7eaa5a82a1cac232164990f04874c594c9453ec55eef02eab885aa02fc17a2",
|
||||
"sha256:f5321fbe4bf3fefa0efd0bfe7fb14e90909eb62a48ccda331726b4319897dd5e"
|
||||
"sha256:753a0374df26658f99d826cfe40394a686d05985786d946fbe4165b5148f5a7c",
|
||||
"sha256:a7acd0977125325f516bda9735fa7142b909a8d01e8b2e4c8108d0984e6e0098"
|
||||
],
|
||||
"version": "==1.25.11"
|
||||
"version": "==1.26.5"
|
||||
}
|
||||
},
|
||||
"develop": {}
|
||||
|
43
README.md
43
README.md
@ -4,11 +4,20 @@ This python script fetches the latest tweet of a public user from twitter and ou
|
||||
directory. This script expects two json files in `/etc/tw2hugo`, which should have `chmod 700` for a non-privileged
|
||||
user and no rights for the group. All files should have `chmod 400`.
|
||||
|
||||
## Setup
|
||||
|
||||
This package depends only on the pip package "requests", which I recommend to simply install globally with
|
||||
`pip3 install requests`. You can also use the provided pipfile and install all dependencies into a virtual environment
|
||||
with `pipenv lock`, however, you will need to activate that environment in your automation script.
|
||||
|
||||
Next, you will need to install the configuration files below:
|
||||
|
||||
## Needed configuration files
|
||||
|
||||
/etc/tw2hugo/mail_credentials.json
|
||||
```
|
||||
{
|
||||
"enable": true,
|
||||
"smtp-server": "mail server",
|
||||
"smtp-port": 465,
|
||||
"email-user": "username for smtp",
|
||||
@ -23,14 +32,20 @@ user and no rights for the group. All files should have `chmod 400`.
|
||||
{
|
||||
"bearer": "bearer token of your twitter app",
|
||||
"twitter-handle": "twitter handle",
|
||||
"output-location": "hugo base dir/data/latest_tweet.json"
|
||||
"output-location": "/hugo/project/directory/data/latest_tweet.json",
|
||||
"is_blocklist": true,
|
||||
"hashtags": [
|
||||
"dontincludethishastag",
|
||||
"politics"
|
||||
],
|
||||
"omit_replies": true
|
||||
}
|
||||
```
|
||||
|
||||
## Why does it need email credentials?
|
||||
|
||||
The script has the specialty that sends an email when something goes wrong, so I am notifed and can fix the issue.
|
||||
If you don't want that, simply open `mail/mail.py` and replace everyting in the init function with a simple `pass`.
|
||||
If you don't want that, replace `enable: true` in the `mail_credentials.json` with `false`.
|
||||
|
||||
## How does it integrate into Hugo?
|
||||
|
||||
@ -67,7 +82,10 @@ I build the template using a [partial template](https://gohugo.io/templates/part
|
||||
|
||||
<div class="level mb-0">
|
||||
<span class="level-left">
|
||||
<a target="_blank" rel="nofollow noreferrer noopener" href="https://twitter.com/{{.user.name}}/status/{{.id_str}}">{{ slicestr .created_at 0 20 }}</a>
|
||||
<a target="_blank"
|
||||
rel="nofollow noreferrer noopener"
|
||||
href="https://twitter.com/{{.user.name}}/status/{{.id_str}}">{{ slicestr .created_at 0 20 }}
|
||||
</a>
|
||||
</span>
|
||||
<span class="level-right">
|
||||
via {{ .source | safeHTML }}
|
||||
@ -103,10 +121,27 @@ The important bit is this go template instruction:
|
||||
{{ end }}
|
||||
```
|
||||
|
||||
Between these you can simple call the keys from the json, so `name` in dict `user` becomes simple `{{ .user.name }}`.
|
||||
Between these you can call the keys from the json, so `name` in dict `user` becomes simply `{{ .user.name }}`.
|
||||
Neato, isn't it? If you want to know how it looks like, head over to my [website](https://tuxstash.de/) and scroll down
|
||||
to the footer.
|
||||
|
||||
## Excluding hashtags
|
||||
|
||||
Sometimes you do not want a tweet to decorate the hard work you call your web home.
|
||||
You can edit the following in `twitter.json` to exclude certain hashtags:
|
||||
|
||||
```
|
||||
"is_blocklist: true,
|
||||
"exclude": [
|
||||
"politics"
|
||||
]
|
||||
```
|
||||
|
||||
**Note**: If `is_blocklist` is false, then it acts as a list of allowed hashtags and will only output new json
|
||||
if the hashtag is found.
|
||||
|
||||
You find sample configuration files in the `/etc/` folder in the project root.
|
||||
|
||||
<!--suppress HtmlDeprecatedAttribute -->
|
||||
<p align="center">
|
||||
<a href='https://ko-fi.com/L3L31HXRQ' target='_blank'><img height='36' style='border:0;height:36px;' src='https://cdn.ko-fi.com/cdn/kofi2.png?v=2' border='0' alt='Buy Me a Coffee at ko-fi.com' /></a>
|
||||
|
9
etc/tw2hugo/mail_credentials.json
Normal file
9
etc/tw2hugo/mail_credentials.json
Normal file
@ -0,0 +1,9 @@
|
||||
{
|
||||
"enable": true,
|
||||
"smtp-server": "smtp.example.com",
|
||||
"smtp-port": 465,
|
||||
"email-user": "notify@example.com",
|
||||
"email-sendfrom": "notify@example.com",
|
||||
"email-password": "passw0rd",
|
||||
"email-sendto": "you@example.com"
|
||||
}
|
11
etc/tw2hugo/twitter.json
Normal file
11
etc/tw2hugo/twitter.json
Normal file
@ -0,0 +1,11 @@
|
||||
{
|
||||
"bearer": "bearer token of your twitter app",
|
||||
"twitter-handle": "twitter handle",
|
||||
"output-location": "/hugo/project/directory/data/latest_tweet.json",
|
||||
"is_blocklist": true,
|
||||
"hashtags": [
|
||||
"dontincludethishastag",
|
||||
"politics"
|
||||
],
|
||||
"omit_replies": true
|
||||
}
|
@ -26,6 +26,9 @@ class mail:
|
||||
with path.open("r") as fp:
|
||||
credentials = j_load(fp)
|
||||
|
||||
if not credentials['enable']:
|
||||
return
|
||||
|
||||
message = MIMEText(what, _charset='utf8')
|
||||
message['Subject'] = "Tweet2Hugo alarm report"
|
||||
message['From'] = credentials['email-sendfrom']
|
||||
|
@ -19,7 +19,7 @@ if system().lower() == "windows":
|
||||
with open('latest_tweet.json', 'w') as fp:
|
||||
json.dump(tweet, fp, indent=4, sort_keys=True)
|
||||
if system().lower() == "linux":
|
||||
with open('/home/git/tuxstash.de/data/latest_tweet.json', 'w') as fp:
|
||||
with open(twitter.output_location(), 'w') as fp:
|
||||
json.dump(tweet, fp, indent=4, sort_keys=True)
|
||||
|
||||
exit(0)
|
||||
|
@ -22,7 +22,7 @@ class twitter:
|
||||
self.credentials = str()
|
||||
|
||||
if system().lower() == "windows":
|
||||
self.path = Path("bearer.json")
|
||||
self.path = Path("twitter.json")
|
||||
else:
|
||||
self.path = Path("/etc/tw2hugo/twitter.json")
|
||||
|
||||
@ -51,6 +51,40 @@ class twitter:
|
||||
|
||||
r = requests.get(url=get_url, headers=header)
|
||||
r.raise_for_status()
|
||||
try:
|
||||
# Get hashtag list
|
||||
tweet_tags = r.json()[0]['entities']['hashtags']
|
||||
hashtags = self.credentials['hashtags']
|
||||
found_not_allowed_tag = True
|
||||
print(tweet_tags)
|
||||
|
||||
for each in tweet_tags:
|
||||
# It's a blocklist and the hashtag is found
|
||||
if self.credentials['is_blocklist'] and each['text'] in hashtags:
|
||||
found_not_allowed_tag = True
|
||||
continue
|
||||
|
||||
# It is a list of allowed hashtags and the hashtag was found
|
||||
if not self.credentials['is_blocklist'] and each['text'] in hashtags:
|
||||
found_not_allowed_tag = False
|
||||
break
|
||||
|
||||
# it is a list of allowed hashtags and the hash was not fonud
|
||||
if not self.credentials['is_blocklist'] and not each['text'] in hashtags:
|
||||
found_not_allowed_tag = True
|
||||
continue
|
||||
|
||||
# Is Tweet a reply?
|
||||
if self.credentials['omit_replies'] and r.json()[0]['in_reply_to_status_id']:
|
||||
found_not_allowed_tag = True
|
||||
|
||||
if found_not_allowed_tag:
|
||||
exit(0)
|
||||
|
||||
except IndexError:
|
||||
# No hashtags, that's fine
|
||||
pass
|
||||
|
||||
return r.json()[0]
|
||||
|
||||
except Exception as e:
|
||||
|
Loading…
x
Reference in New Issue
Block a user