Compare commits

..

No commits in common. "master" and "v1.0" have entirely different histories.
master ... v1.0

8 changed files with 19 additions and 113 deletions

4
.gitignore vendored
View File

@ -1,9 +1,7 @@
.idea .idea
venv venv
twitter.json bearer.json
mail_credentials.json mail_credentials.json
test.* test.*
__pycache__/ __pycache__/
*.json *.json
!/etc/tw2hugo/twitter.json
!/etc/tw2hugo/mail_credentials.json

24
Pipfile.lock generated
View File

@ -18,17 +18,17 @@
"default": { "default": {
"certifi": { "certifi": {
"hashes": [ "hashes": [
"sha256:2bbf76fd432960138b3ef6dda3dde0544f27cbf8546c458e60baf371917ba9ee", "sha256:5930595817496dd21bb8dc35dad090f1c2cd0adfaf21204bf6732ca5d8ee34d3",
"sha256:50b1e4f8446b06f41be7dd6338db18e0990601dce795c2b1686458aa7e8fa7d8" "sha256:8fc0819f1f30ba15bdb34cceffb9ef04d99f420f68eb75d901e9560b8749fc41"
], ],
"version": "~> 2022.12.07" "version": "==2020.6.20"
}, },
"chardet": { "chardet": {
"hashes": [ "hashes": [
"sha256:0d6f53a15db4120f2b08c94f11e7d93d2c911ee118b6b30a04ec3ee8310179fa", "sha256:84ab92ed1c4d4f16916e05906b6b75a6c0fb5db821cc65e70cbd64a3e2a5eaae",
"sha256:f864054d66fd9118f2e67044ac8981a54775ec5b67aed0441892edb553d21da5" "sha256:fc323ffcaeaed0e0a02bf4d117757b98aed530d9ed4531e3e15460124c106691"
], ],
"version": "==4.0.0" "version": "==3.0.4"
}, },
"idna": { "idna": {
"hashes": [ "hashes": [
@ -39,18 +39,18 @@
}, },
"requests": { "requests": {
"hashes": [ "hashes": [
"sha256:27973dd4a904a4f13b263a19c866c13b92a39ed1c964655f025f3f8d3d75b804", "sha256:b3559a131db72c33ee969480840fff4bb6dd111de7dd27c8ee1f820f4f00231b",
"sha256:c210084e36a42ae6b9219e00e48287def368a26d03a048ddad7bfee44f75871e" "sha256:fe75cc94a9443b9246fc7049224f75604b113c36acb93f87b80ed42c44cbb898"
], ],
"index": "pypi", "index": "pypi",
"version": "==2.25.1" "version": "==2.24.0"
}, },
"urllib3": { "urllib3": {
"hashes": [ "hashes": [
"sha256:753a0374df26658f99d826cfe40394a686d05985786d946fbe4165b5148f5a7c", "sha256:8d7eaa5a82a1cac232164990f04874c594c9453ec55eef02eab885aa02fc17a2",
"sha256:a7acd0977125325f516bda9735fa7142b909a8d01e8b2e4c8108d0984e6e0098" "sha256:f5321fbe4bf3fefa0efd0bfe7fb14e90909eb62a48ccda331726b4319897dd5e"
], ],
"version": "==1.26.5" "version": "==1.25.11"
} }
}, },
"develop": {} "develop": {}

View File

@ -4,20 +4,11 @@ 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 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`. 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 ## Needed configuration files
/etc/tw2hugo/mail_credentials.json /etc/tw2hugo/mail_credentials.json
``` ```
{ {
"enable": true,
"smtp-server": "mail server", "smtp-server": "mail server",
"smtp-port": 465, "smtp-port": 465,
"email-user": "username for smtp", "email-user": "username for smtp",
@ -32,20 +23,14 @@ Next, you will need to install the configuration files below:
{ {
"bearer": "bearer token of your twitter app", "bearer": "bearer token of your twitter app",
"twitter-handle": "twitter handle", "twitter-handle": "twitter handle",
"output-location": "/hugo/project/directory/data/latest_tweet.json", "output-location": "hugo base dir/data/latest_tweet.json"
"is_blocklist": true,
"hashtags": [
"dontincludethishastag",
"politics"
],
"omit_replies": true
} }
``` ```
## Why does it need email credentials? ## 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. 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, replace `enable: true` in the `mail_credentials.json` with `false`. If you don't want that, simply open `mail/mail.py` and replace everyting in the init function with a simple `pass`.
## How does it integrate into Hugo? ## How does it integrate into Hugo?
@ -82,10 +67,7 @@ I build the template using a [partial template](https://gohugo.io/templates/part
<div class="level mb-0"> <div class="level mb-0">
<span class="level-left"> <span class="level-left">
<a target="_blank" <a target="_blank" rel="nofollow noreferrer noopener" href="https://twitter.com/{{.user.name}}/status/{{.id_str}}">{{ slicestr .created_at 0 20 }}</a>
rel="nofollow noreferrer noopener"
href="https://twitter.com/{{.user.name}}/status/{{.id_str}}">{{ slicestr .created_at 0 20 }}
</a>
</span> </span>
<span class="level-right"> <span class="level-right">
via {{ .source | safeHTML }} via {{ .source | safeHTML }}
@ -121,27 +103,10 @@ The important bit is this go template instruction:
{{ end }} {{ end }}
``` ```
Between these you can call the keys from the json, so `name` in dict `user` becomes simply `{{ .user.name }}`. Between these you can simple call the keys from the json, so `name` in dict `user` becomes simple `{{ .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 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. 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 --> <!--suppress HtmlDeprecatedAttribute -->
<p align="center"> <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> <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>

View File

@ -1,9 +0,0 @@
{
"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"
}

View File

@ -1,11 +0,0 @@
{
"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
}

View File

@ -26,9 +26,6 @@ class mail:
with path.open("r") as fp: with path.open("r") as fp:
credentials = j_load(fp) credentials = j_load(fp)
if not credentials['enable']:
return
message = MIMEText(what, _charset='utf8') message = MIMEText(what, _charset='utf8')
message['Subject'] = "Tweet2Hugo alarm report" message['Subject'] = "Tweet2Hugo alarm report"
message['From'] = credentials['email-sendfrom'] message['From'] = credentials['email-sendfrom']

View File

@ -19,7 +19,7 @@ if system().lower() == "windows":
with open('latest_tweet.json', 'w') as fp: with open('latest_tweet.json', 'w') as fp:
json.dump(tweet, fp, indent=4, sort_keys=True) json.dump(tweet, fp, indent=4, sort_keys=True)
if system().lower() == "linux": if system().lower() == "linux":
with open(twitter.output_location(), 'w') as fp: with open('/home/git/tuxstash.de/data/latest_tweet.json', 'w') as fp:
json.dump(tweet, fp, indent=4, sort_keys=True) json.dump(tweet, fp, indent=4, sort_keys=True)
exit(0) exit(0)

View File

@ -22,7 +22,7 @@ class twitter:
self.credentials = str() self.credentials = str()
if system().lower() == "windows": if system().lower() == "windows":
self.path = Path("twitter.json") self.path = Path("bearer.json")
else: else:
self.path = Path("/etc/tw2hugo/twitter.json") self.path = Path("/etc/tw2hugo/twitter.json")
@ -51,40 +51,6 @@ class twitter:
r = requests.get(url=get_url, headers=header) r = requests.get(url=get_url, headers=header)
r.raise_for_status() 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] return r.json()[0]
except Exception as e: except Exception as e: