Update 06/12/2020

* Added new configuration options, please update your twitter.json after updating! Please see ./etc/twitter.json for changes.
* The hashtag list can now act as block list or include list, depending on `is_blocklist`
* Replies can now be ignored by setting `omit_replies` to true.
master
Domeniko Gentner 3 years ago
parent ee6a28f17c
commit b252f8855b
  1. 13
      README.md
  2. 14
      etc/tw2hugo/twitter.json
  3. 16
      twitter/twitter.py

@ -33,10 +33,12 @@ Next, you will need to install the configuration files below:
"bearer": "bearer token of your twitter app",
"twitter-handle": "twitter handle",
"output-location": "/hugo/project/directory/data/latest_tweet.json",
"exclude": [
"is_blocklist": true,
"hashtags": [
"dontincludethishastag",
"politics"
]
],
"omit_replies": true
}
```
@ -125,14 +127,19 @@ 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:
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 -->

@ -1,7 +1,11 @@
{
"bearer": "bearer token from twitter",
"twitter-handle": "GothSeiDank",
"output-location": "/path/to/hugo/data/latest_tweet.json",
"exclude": ["1", "2"]
"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
}

@ -52,9 +52,21 @@ class twitter:
r = requests.get(url=get_url, headers=header)
r.raise_for_status()
try:
excluded = r.json()[0]['entities']['hashtags'][0]['text']
if excluded in self.credentials['exclude']:
# Get hashtag list
hashtag = r.json()[0]['entities']['hashtags'][0]['text']
# It's a blocklist and the hashtag is found
if self.credentials['is_blocklist'] and hashtag in self.credentials['hashtags']:
exit(0)
# It is a list of allowed hashtags and the hashtag was not found
if not self.credentials['is_blocklist'] and hashtag not in self.credentials['hashtags']:
exit(0)
if self.credentials['omit_replies'] and r.json()[0]['in_reply_to_status_id']:
# Tweet is a reply
exit(0)
except IndexError:
# No hashtags, that's fine
pass

Loading…
Cancel
Save