Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
61 commits
Select commit Hold shift + click to select a range
b25d48f
Update Config.py - fix type hinting
Crinibus Oct 15, 2022
fe81f8c
Temporarily rename Config.py
Crinibus Oct 15, 2022
152325b
Rename config_1.py to config.py and rename class Config to ConfigManager
Crinibus Oct 15, 2022
aee599e
Change dest for argument --channel
Crinibus Oct 15, 2022
0fc805a
Rename class API to ApiManager
Crinibus Oct 15, 2022
81e0be0
Temporarily rename API.py to api_temp.py
Crinibus Oct 15, 2022
9c34317
Rename api_temp.py to api.py
Crinibus Oct 15, 2022
1cc4a44
Make api.py more readable and format with black
Crinibus Oct 15, 2022
de4d355
Update __init__.py
Crinibus Oct 15, 2022
2466bf5
Create tvguide.py
Crinibus Oct 17, 2022
bbe78b1
Fix
Crinibus Oct 17, 2022
78e6e2a
Use requests_cache to cache requests
Crinibus Oct 17, 2022
9cdbcd4
Use filesystem as backend to requests_cache
Crinibus Oct 17, 2022
f6fee07
Update Filemanager to have root_path and defaults_ini_path as properties
Crinibus Oct 17, 2022
2b60744
Add type hint in class Channel
Crinibus Oct 17, 2022
afb2928
Update class TvGuide
Crinibus Oct 17, 2022
870d8a8
Create main_v2.py to test with
Crinibus Oct 17, 2022
1c71b12
Fix method print_programs in class TvGuide
Crinibus Oct 17, 2022
e237891
Update values in CHANNEL_NUMBER_INDEX to replce "-" with spaces and d…
Crinibus Oct 17, 2022
8072b1e
Delete duplicate print statements in class Channel
Crinibus Oct 17, 2022
adacf08
Delete method add_program in class Channel and just add programs in m…
Crinibus Oct 17, 2022
ba006bd
Add type hints to properties in classes Channel and Program
Crinibus Oct 17, 2022
60c13eb
Update tvguide.py
Crinibus Oct 17, 2022
3cfcf1e
Update TvGuide.print_programs - Get all channels if show_all_channels…
Crinibus Oct 17, 2022
082d2af
Update TvGuide.print_programs - show all programs if show_all_program…
Crinibus Oct 17, 2022
92058f4
Rename method format_program_info to _parse_channel_info in class Cha…
Crinibus Oct 18, 2022
e584a9e
Update format.py
Crinibus Oct 18, 2022
9486e05
Rename method format_info to _format_info in class Program
Crinibus Oct 18, 2022
4e63846
Fix type hints
Crinibus Oct 18, 2022
4e4df9f
Rename a variable from time to print_time to avoid shadowing module time
Crinibus Oct 18, 2022
800ee5f
Update tv.py
Crinibus Oct 18, 2022
cf65530
Simplify method is_running in class Program
Crinibus Oct 18, 2022
dfc5294
Add static method read_defaults_config_file to class ConfigManager
Crinibus Oct 18, 2022
c577462
Add static method get_timezone to class ConfigManager
Crinibus Oct 18, 2022
401cbf6
Move class Channel under class Program in tv.py
Crinibus Oct 20, 2022
8f82920
Delete argument --default-space-seperator
Crinibus Oct 20, 2022
4cb5d66
Update class Channel - create "_get..." methods to simplify "print_..…
Crinibus Oct 20, 2022
aaf36ed
Simplify method "is_running_at" in class Program
Crinibus Oct 20, 2022
97d37a6
Update class TvGuide - add properties to keep track of what to print …
Crinibus Oct 20, 2022
e116880
Use cache when reading config file
Crinibus Oct 20, 2022
e0c0212
Update README - requires python 3.9+
Crinibus Oct 20, 2022
c99e5b3
Reorder imports and replace relative imports with absolute imports
Crinibus Oct 20, 2022
b620e84
Update LICENCE to 2022
Crinibus Oct 20, 2022
67e41d3
Fix import
Crinibus Oct 20, 2022
35d8b2f
Update tv.py
Crinibus Oct 20, 2022
669a894
Add print when currently running program is found
Crinibus Oct 22, 2022
410eff9
Split a line to two lines
Crinibus Oct 22, 2022
7de08ef
Format with black
Crinibus Oct 22, 2022
960bffb
Add config files for flake8 and black
Crinibus Oct 22, 2022
e50d968
Format with black
Crinibus Oct 22, 2022
b5f3ca2
Update Chrome version in REQUEST_HEADER
Crinibus Oct 22, 2022
6b7f0d4
Add print when using default channels
Crinibus Oct 23, 2022
11dac39
Change prints before showing the programs
Crinibus Oct 23, 2022
8b8ab24
Fix print statements in main.py - delete end=""
Crinibus Oct 23, 2022
834be88
Add try catch around main function call
Crinibus Oct 28, 2022
2c4f8c0
Delete main.py
Crinibus Oct 28, 2022
4d24a1a
Delete method format_data in ApiManager class
Crinibus Oct 28, 2022
839b3bd
Rename main_v2.py to main.py
Crinibus Oct 28, 2022
bcbefcc
Add type hints to all instance variables in TvGuide class
Crinibus Oct 28, 2022
1ec9630
Rename instance variable "channels" to "all_channels" in TvGuide class
Crinibus Oct 28, 2022
b226984
Simplify documentation of arguments in README
Crinibus Nov 2, 2022
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .flake8
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[flake8]
max-complexity = 10
max-line-length = 127
exclude = .git,__pycache__
per-file-ignores = __init__.py:F401
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@

__pycache__/
.vscode/
.vscode/
api_cache/
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) 2021 Crinibus
Copyright (c) 2022 Crinibus

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
35 changes: 14 additions & 21 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,23 @@ A program that gives the user a list over what comes in tv*.
<br/>


Requires ```python3.9+```

<br/>

# How to use <a name="how-to-use"></a>

An example on how to use this program:
```
python3 main.py -c dr1 -c tv2 --all
python3 main.py -c dr1 tv2 --all
```
This prints all the programs that run today for the channels DR1 and TV2.

<br/>

Another example:
```
python3 main.py -c dr1 -c tv2 -t 20:00 -d 1
python3 main.py -c dr1 tv2 -t 20:00 -d 1
```
This prints only the tv-shows that start or is running at 8 pm the next day on the channels DR1 and TV2.

Expand All @@ -45,29 +49,30 @@ This prints the programs (with categories) that is currently running on the defa
- ```-v``` or ```--verbose```
- ```-s [search-term]``` or ```--search [search-term]```
- ```--default-channels [channel]```
- ```--default-space-seperator [space-seperator]```
- ```--justify-length [int]```

<br/>


## --channel <a name="--channel"></a>

By using the flag ```-c``` or ```--channel``` you specify which channels you want the program to show tv-shows from. Replace "[channel]" with wanted channel, e.g. "dr1". <br/>
You can specify multiple channels just by using the ```-c``` flag again.<br/>
By using the flag ```-c``` or ```--channel``` you specify which channels you want the program to show tv-shows from. Replace "[channel]" with wanted channel(s), e.g. "dr1". <br/>
If no channel(s) is chosen, the default channels is used. You can change the default channels as described [here](#--default-channels)

For example:
```
python3 main.py -c [channel_1] -c [channel_2]
```
**OBS**: when specifing channels with a space such as "TV2 News", use a dash (-) instead of a space. E.g. "TV2 News" -> "TV2-News"
or
```
python3 main.py -c [channel_1] [channel_2]
```

You can also specify "all" as the first channel to get all channels.

Examples:<br/>
```
python3 main.py -c dr1 -c tv2 --all
python3 main.py -c dr1 tv2 --all
```
This shows all the programs that run on DR1 and TV2 for today.

Expand Down Expand Up @@ -135,7 +140,7 @@ This shows all the programs that have the category "film" on the default channel
<br/>

```
python3 main.py --category film --category drama
python3 main.py --category film drama
```
This shows all the programs that have either the category "film" or "drama" on the default channels for today.

Expand Down Expand Up @@ -178,7 +183,7 @@ This shows all the programs on the default channels for today that have the word
<br/>

```
python3 main.py --search avis --search vejr
python3 main.py --search avis vejr
```
This shows all the programs on the default channels for today that have either the words "avis" or "vejr" in the title.

Expand All @@ -197,18 +202,6 @@ This changes the default channels to "DR1", "TV2" and "CANAL 9".
<br/>


## --default-space-seperator <a name="--default-space-seperator"></a>


Example:
```
python3 main.py --default-space-seperator -
```
This changes the default space seperator to the sign "-".

<br/>


## --justify-length <a name="--justify-length"></a>


Expand Down
96 changes: 6 additions & 90 deletions main.py
Original file line number Diff line number Diff line change
@@ -1,106 +1,22 @@
# from format import Format
# from argument import argparse_setup
# from API import API
# from Config import Config
import tvguide as tv


def print_currently_running(data_source: dict, user_channels: list) -> None:
channels_string = ", ".join(user_channels).upper()

print(f"\n----- Showing currently running programs for: {channels_string} -----", end="")
for channel in user_channels:
data_source[channel].print_currently_running()


def print_program_times(data_source: dict, user_channels: list, user_times: list) -> None:
channels_string = ", ".join(user_channels).upper()
user_times_string = ", ".join(user_times)

print(f"\n----- Showing programs that is running at: {user_times_string} for: {channels_string} -----", end="")
for channel in user_channels:
data_source[channel].print_times(user_times)


def print_program_categories(data_source: dict, user_channels: list, user_categories: list) -> None:
user_categories_string = ", ".join(user_categories)

print(f"\n----- Searching for categories: {user_categories_string} -----", end="")
for channel in user_channels:
data_source[channel].print_categories(user_categories)


def print_program_searches(data_source: dict, user_channels: list, user_searches: list) -> None:
user_search_string = tv.Format.user_search(", ".join(user_searches))

print(f"\n----- Searching for keywords: {user_search_string} -----", end="")
for channel in user_channels:
data_source[channel].print_searches(user_searches)


def print_program_all(data_source: dict, user_channels: list) -> None:
user_channels_string = ", ".join(user_channels).upper()

print(f"\n----- Showing all programs for: {user_channels_string} -----", end="")
for channel in user_channels:
data_source[channel].print_all_programs()


def change_defaults(args, data: dict):
if args.default_channels:
tv.Config.change_defaults_user_channels(args.default_channels)
default_channels_string = ", ".join(args.default_channels).upper()
print(f"Changed default channel(s) to: {default_channels_string}")

if args.default_space_seperator:
tv.Config.change_space_seperator(args.default_space_seperator)
print(f"Changed space seperator to: {args.default_space_seperator}")

if args.justify_length:
tv.Config.change_justify_length(args.justify_length)
print(f"Changed justify length to: {args.justify_length}")

if not args.channel:
args.channel = tv.Config.get_defaults_user_channels()
default_channels_string = ", ".join(args.channel).upper()
print(f"No channel(s) chosen: using default channels ({default_channels_string})")
elif args.channel[0].lower() == "all":
args.channel = [channel for channel in data.keys()]


def print_programs(args, data):
if args.now:
print_currently_running(data, args.channel)

if args.time:
print_program_times(data, args.channel, args.time)

if args.category:
print_program_categories(data, args.channel, args.category)

if args.search:
print_program_searches(data, args.channel, args.search)

if args.all:
print_program_all(data, args.channel)


def main():
args = tv.argparse_setup()

api_data = tv.API.get_data(args.day)
tvguide = tv.TvGuide()

tvguide.parse_arguments(args)

my_data = tv.API.format_data(api_data, args.verbose)
api_data = tv.ApiManager.get_data(tvguide.relative_date)

change_defaults(args, my_data)
tvguide.parse_api_data(api_data)

print_programs(args, my_data)
tvguide.print_programs()


if __name__ == "__main__":
try:
main()
except KeyError:
print("Check channel name or this scraper can't use this channel")
except KeyboardInterrupt:
print("Stopped by user")
2 changes: 2 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[tool.black]
line-length = 127
3 changes: 2 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
requests==2.24.0
requests>=2.24.0
requests-cache>=0.9.6
31 changes: 0 additions & 31 deletions tvguide/API.py

This file was deleted.

61 changes: 0 additions & 61 deletions tvguide/Config.py

This file was deleted.

5 changes: 3 additions & 2 deletions tvguide/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from .format import Format
from .argument import argparse_setup
from .API import API
from .Config import Config
from .api import ApiManager
from .config import ConfigManager
from .tvguide import TvGuide
23 changes: 23 additions & 0 deletions tvguide/api.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import requests
import requests_cache

from tvguide.const import API_LINK, REQUEST_HEADER, REQUEST_COOKIES
from tvguide.format import Format
from tvguide.tv import Channel

requests_cache.install_cache(cache_name="api_cache", expire_after=120, backend="filesystem")


class ApiManager:
@staticmethod
def get_link(relative_date: int) -> str:
date = Format.get_specified_date(relative_date)
return API_LINK.replace("{date}", date)

@staticmethod
def get_data(relative_day: int) -> dict:
"""Get formatted data from API"""
api_link = ApiManager.get_link(relative_day)
response = requests.get(api_link, headers=REQUEST_HEADER, cookies=REQUEST_COOKIES)

return response.json()
Loading